From 6d75ad624f306a735ee5e9a268becb75fbfcabeb Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 17:51:33 +0000 Subject: [PATCH 1/9] Trim whitespace --- src/label_image.py | 8 ++++---- src/py/label_dir.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/label_image.py b/src/label_image.py index cc8ddac..fdb2d71 100755 --- a/src/label_image.py +++ b/src/label_image.py @@ -8,7 +8,7 @@ image_data = tf.gfile.FastGFile(image_path, 'rb').read() # Loads label file, strips off carriage return -label_lines = [line.rstrip() for line +label_lines = [line.rstrip() for line in tf.gfile.GFile("/tf_files/retrained_labels.txt")] # Unpersists graph from file @@ -20,13 +20,13 @@ with tf.Session() as sess: # Feed the image_data as input to the graph and get first prediction softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') - + predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) - + # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] - + for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] diff --git a/src/py/label_dir.py b/src/py/label_dir.py index eb4543c..846e835 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -18,7 +18,7 @@ # Loads label file, strips off carriage return -label_lines = [line.rstrip() for line +label_lines = [line.rstrip() for line in tf.gfile.GFile("/tf_files/retrained_labels.txt")] # Unpersists graph from file @@ -35,14 +35,14 @@ #except: # None #mkdir ('scanned') - + for imageFile in imgFiles: - image_data = tf.gfile.FastGFile(varPath+"/"+imageFile, 'rb').read() + image_data = tf.gfile.FastGFile(varPath+"/"+imageFile, 'rb').read() print (varPath+"/"+imageFile) predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) - + # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] firstElt = top_k[0]; From 9efc14f53744c0641aef5588bca7e34f5bdd91ee Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 17:53:28 +0000 Subject: [PATCH 2/9] Pythonic var names --- src/py/label_dir.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index 846e835..26e5735 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -12,9 +12,9 @@ from os import mkdir from shutil import copyfile from os.path import isfile, join -varPath = '/toScan' -destDir = "/scanned" -imgFiles = [f for f in listdir(varPath) if isfile(join(varPath, f))] +var_path = '/toScan' +dest_dir = "/scanned" +img_files = [f for f in listdir(var_path) if isfile(join(var_path, f))] # Loads label file, strips off carriage return @@ -31,15 +31,15 @@ # Feed the image_data as input to the graph and get first prediction softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') #try: - # shutil.rmtree(destDir) + # shutil.rmtree(dest_dir) #except: # None #mkdir ('scanned') - for imageFile in imgFiles: - image_data = tf.gfile.FastGFile(varPath+"/"+imageFile, 'rb').read() + for image_file in img_files: + image_data = tf.gfile.FastGFile(var_path+"/"+image_file, 'rb').read() - print (varPath+"/"+imageFile) + print (var_path+"/"+image_file) predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) @@ -47,9 +47,9 @@ top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] firstElt = top_k[0]; - newFileName = label_lines[firstElt] +"--"+ str(predictions[0][firstElt])[2:7]+".jpg" - print(newFileName) - copyfile(varPath+"/"+imageFile, destDir+"/"+newFileName) + new_file_name = label_lines[firstElt] +"--"+ str(predictions[0][firstElt])[2:7]+".jpg" + print(new_file_name) + copyfile(var_path+"/"+image_file, dest_dir+"/"+new_file_name) for node_id in top_k: human_string = label_lines[node_id] From c9b021e15a821b4c4b58766075b0c1b484196d29 Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 17:54:18 +0000 Subject: [PATCH 3/9] Use join for path concatenation --- src/py/label_dir.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index 26e5735..76d3e74 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -37,9 +37,9 @@ #mkdir ('scanned') for image_file in img_files: - image_data = tf.gfile.FastGFile(var_path+"/"+image_file, 'rb').read() + image_data = tf.gfile.FastGFile(join(var_path, image_file), 'rb').read() - print (var_path+"/"+image_file) + print join(var_path, image_file) predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) @@ -49,7 +49,7 @@ new_file_name = label_lines[firstElt] +"--"+ str(predictions[0][firstElt])[2:7]+".jpg" print(new_file_name) - copyfile(var_path+"/"+image_file, dest_dir+"/"+new_file_name) + copyfile(join(var_path, image_file), join(dest_dir, new_file_name)) for node_id in top_k: human_string = label_lines[node_id] From 5eee27db9a35179712137082bd5d0534e55e50ed Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 17:55:38 +0000 Subject: [PATCH 4/9] Use consistent quotes --- src/py/label_dir.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index 76d3e74..d8d88b6 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -13,16 +13,16 @@ from shutil import copyfile from os.path import isfile, join var_path = '/toScan' -dest_dir = "/scanned" +dest_dir = '/scanned' img_files = [f for f in listdir(var_path) if isfile(join(var_path, f))] # Loads label file, strips off carriage return label_lines = [line.rstrip() for line - in tf.gfile.GFile("/tf_files/retrained_labels.txt")] + in tf.gfile.GFile('/tf_files/retrained_labels.txt')] # Unpersists graph from file -with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f: +with tf.gfile.FastGFile('/tf_files/retrained_graph.pb', 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') @@ -47,7 +47,7 @@ top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] firstElt = top_k[0]; - new_file_name = label_lines[firstElt] +"--"+ str(predictions[0][firstElt])[2:7]+".jpg" + new_file_name = label_lines[firstElt] +'--'+ str(predictions[0][firstElt])[2:7]+'.jpg' print(new_file_name) copyfile(join(var_path, image_file), join(dest_dir, new_file_name)) From 55722cbc3098204ff892488f2fa6f3348ea2a41f Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 17:56:25 +0000 Subject: [PATCH 5/9] Use var for src_image_path --- src/py/label_dir.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index d8d88b6..d69c673 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -37,9 +37,10 @@ #mkdir ('scanned') for image_file in img_files: - image_data = tf.gfile.FastGFile(join(var_path, image_file), 'rb').read() + src_image_path = join(var_path, image_file) + image_data = tf.gfile.FastGFile(src_image_path, 'rb').read() - print join(var_path, image_file) + print src_image_path predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) @@ -49,7 +50,7 @@ new_file_name = label_lines[firstElt] +'--'+ str(predictions[0][firstElt])[2:7]+'.jpg' print(new_file_name) - copyfile(join(var_path, image_file), join(dest_dir, new_file_name)) + copyfile(src_image_path, join(dest_dir, new_file_name)) for node_id in top_k: human_string = label_lines[node_id] From 6eb289c5eb4dafd438305ef5694442eb927118e2 Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 17:59:16 +0000 Subject: [PATCH 6/9] Formatting, removed unused imports and comments --- src/py/label_dir.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index d69c673..95bd808 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -1,22 +1,12 @@ import tensorflow as tf -import sys - -# change this as you see fit -#image_path = sys.argv[1] - -# Read in the image_data -#image_data = tf.gfile.FastGFile(image_path, 'rb').read() -import os -import shutil -from os import listdir -from os import mkdir from shutil import copyfile +from os import listdir from os.path import isfile, join + var_path = '/toScan' dest_dir = '/scanned' img_files = [f for f in listdir(var_path) if isfile(join(var_path, f))] - # Loads label file, strips off carriage return label_lines = [line.rstrip() for line in tf.gfile.GFile('/tf_files/retrained_labels.txt')] @@ -30,11 +20,6 @@ with tf.Session() as sess: # Feed the image_data as input to the graph and get first prediction softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') - #try: - # shutil.rmtree(dest_dir) - #except: - # None - #mkdir ('scanned') for image_file in img_files: src_image_path = join(var_path, image_file) @@ -55,5 +40,4 @@ for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] - #print (node_id) print('%s (score = %.5f)' % (human_string, score)) From f55dc911b712a87993509cee09df33e1c015cfe5 Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 18:00:11 +0000 Subject: [PATCH 7/9] Consistent var naming --- src/py/label_dir.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index 95bd808..7b4cd8e 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -3,9 +3,9 @@ from os import listdir from os.path import isfile, join -var_path = '/toScan' +src_dir = '/toScan' dest_dir = '/scanned' -img_files = [f for f in listdir(var_path) if isfile(join(var_path, f))] +img_files = [f for f in listdir(src_dir) if isfile(join(src_dir, f))] # Loads label file, strips off carriage return label_lines = [line.rstrip() for line @@ -22,7 +22,7 @@ softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') for image_file in img_files: - src_image_path = join(var_path, image_file) + src_image_path = join(src_dir, image_file) image_data = tf.gfile.FastGFile(src_image_path, 'rb').read() print src_image_path From 488d079e867d6ef25b2baa00ba5b23de2b2d3763 Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 18:05:47 +0000 Subject: [PATCH 8/9] Move image into a dir for the label rather than copy and change name --- src/py/label_dir.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index 7b4cd8e..90fe103 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -1,7 +1,18 @@ +import errno import tensorflow as tf -from shutil import copyfile -from os import listdir -from os.path import isfile, join +from shutil import move +from os import listdir, makedirs +from os.path import isfile, join, isdir + +def mkdir_p(path): + try: + makedirs(path) + except OSError as exc: # Python >2.5 + if exc.errno == errno.EEXIST and isdir(path): + pass + else: + raise + src_dir = '/toScan' dest_dir = '/scanned' @@ -25,7 +36,7 @@ src_image_path = join(src_dir, image_file) image_data = tf.gfile.FastGFile(src_image_path, 'rb').read() - print src_image_path + print(src_image_path) predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) @@ -33,9 +44,14 @@ top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] firstElt = top_k[0]; - new_file_name = label_lines[firstElt] +'--'+ str(predictions[0][firstElt])[2:7]+'.jpg' - print(new_file_name) - copyfile(src_image_path, join(dest_dir, new_file_name)) + label_dest_folder = join(dest_dir, label_lines[firstElt]) + mkdir_p(label_dest_folder) + + final_file_path = join(label_dest_folder, image_file) + + print(final_file_path) + + move(src_image_path, final_file_path) for node_id in top_k: human_string = label_lines[node_id] From 84dd13d4acebe38469d32e34ddc3398a11fe9929 Mon Sep 17 00:00:00 2001 From: Ollie Relph Date: Tue, 10 Jan 2017 18:07:20 +0000 Subject: [PATCH 9/9] Make a dir for images with issues, move erroring images there --- src/py/label_dir.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/py/label_dir.py b/src/py/label_dir.py index 90fe103..bd742cc 100755 --- a/src/py/label_dir.py +++ b/src/py/label_dir.py @@ -17,6 +17,8 @@ def mkdir_p(path): src_dir = '/toScan' dest_dir = '/scanned' img_files = [f for f in listdir(src_dir) if isfile(join(src_dir, f))] +issue_dir = join(dest_dir, '_issues') +mkdir_p(issue_dir) # Loads label file, strips off carriage return label_lines = [line.rstrip() for line @@ -37,8 +39,14 @@ def mkdir_p(path): image_data = tf.gfile.FastGFile(src_image_path, 'rb').read() print(src_image_path) - predictions = sess.run(softmax_tensor, \ - {'DecodeJpeg/contents:0': image_data}) + + try: + + predictions = sess.run(softmax_tensor, \ + {'DecodeJpeg/contents:0': image_data}) + except Exception as e: + move(srcFilePath, join(issue_dir, imageFile)) + continue # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]