def testLabelImage(self): image_filename = ('../label_image/data/grace_hopper.jpg') # Load some default data label_path = os.path.join(tf.resource_loader.get_data_files_path(), 'data/labels.txt') labels = label_image.load_labels(label_path) self.assertEqual(len(labels), 3) image_path = os.path.join(tf.resource_loader.get_data_files_path(), image_filename) image = label_image.load_image(image_path) self.assertEqual(len(image), 61306) # Create trivial graph; note that the two nodes don't meet with tf.Graph().as_default(): jpeg = tf.constant(image) # Input node that doesn't lead anywhere. tf.image.decode_jpeg(jpeg, name='DecodeJpeg') # Output node, that always outputs a constant. tf.constant([[10, 30, 5]], name='final') # As label_image outputs via print, we assume that # if it returns, everything is OK. result = label_image.run_graph(image, labels, jpeg, 'final:0', 3) self.assertEqual(result, 0)
def predict(img): graph_file = 'output_graph.pb' label_file = 'output_labels.txt' graph = load_graph(graph_file) input_name = "import/input" output_name = "import/final_result" input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) labels = load_labels(label_file) with tf.Session(graph=graph) as sess: image = np.asarray(bytearray(img), dtype="uint8") frame = cv2.imdecode(image, cv2.IMREAD_COLOR) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # img = get_image() t,r = preprocess(frame, 224, 224) t1 = time.time() results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] Top1 = top_k[0] return labels[Top1], results[Top1]
def tf_predict(imageList, verbose=0, numPred=5, graphParams=None): #graphParams is a dictionary with imported graph object, filename for labels, and names of the input and output layers if not graphParams: graphParams = load_networks.load_tf_transfer() graph = graphParams['graph'] labels = label_image.load_labels(graphParams['label_path']) input_operation = graph.get_operation_by_name(graphParams['input_name']) output_operation = graph.get_operation_by_name(graphParams['output_name']) d = len(imageList) outputProbs = np.zeros((d, len(labels))) for image in range(d): with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: imageList[image]}) results = np.squeeze(results) outputProbs[image, :] = results if verbose: top_k = results.argsort()[-numPred:][::-1] for i in top_k: print('%s (score = %.5f)') % (labels[i], results[i]) #return full num_images x num_classes matrix of probabilities return outputProbs
def __init__(self, model_path, labels_path): # Load graph, labels, input and output sensors self.graph = label_image.load_graph(model_path) self.labels = label_image.load_labels(labels_path) self.red_idx = self.get_red_idx() self.input_operation = self.graph.get_operation_by_name(INPUT_LAYER) self.output_operation = self.graph.get_operation_by_name(OUTPUT_LAYER) # Create sessions self.sess = tf.Session(graph=self.graph)
def loadImg(self, fileName): print('Debug:: Selected picture: ' + fileName[0]) self.image.source = fileName[0] self.chosenFolder.text = fileName[0] + '\n' results = label_image.main(fileName[0]) top_k = results.argsort()[-5:][::-1] labels = label_image.load_labels("tf_files/labels.txt") for i in top_k: self.chosenFolder.text += str(labels[i])+': '+str(round(results[i]*100, 2))+'%\n' self.dismiss_popup()
def __init__(self): #TODO load classifier #rospy.loginfo('cwd:%s',os.getcwd()) self.model_file = "light_classification/retrained_mobilenet_1.0_224_005_8k_dp075.pb" self.graph = tf.Graph() self.graph_def = tf.GraphDef() self.labels = load_labels("light_classification/retrained_labels.txt") with open(self.model_file, "rb") as f: self.graph_def.ParseFromString(f.read()) with self.graph.as_default(): tf.import_graph_def(self.graph_def)
def main(stdscr): stdscr.clear() # Configure the camera camera = picamera.PiCamera() # configure the graph graph, session = load_graph(args.model) labels = load_labels(args.labels) while True: try: key = stdscr.getkey() stdscr.addstr('Detected key:\n') stdscr.addstr(str(key)) stdscr.addstr('\n') if str(key) == 'q': break if str(key) == 'c': stdscr.clear() camera.start_preview() time.sleep(2) camera.capture('test.jpg') camera.stop_preview() t = read_tensor_from_image_file('test.jpg', input_height=224, input_width=224, input_mean=128, input_std=128) # Start benchmarking input_name = "import/input" output_name = "import/final_result" input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) start = time.time() results = session.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) end = time.time() results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] stdscr.addstr( '\nEvaluation time (1-image): {:.3f}s\n'.format(end - start)) for i in top_k: stdscr.addstr( str(labels[i]) + ': ' + str(results[i]) + '\n') except Exception as e: stdscr.addstr('Exception occured') stdscr.addstr(str(e)) pass
def testModel(pathToFrames, modelFile, labelFile, inputLayer, outputLayer): graph = label_image.load_graph(modelFile) correct = 0 total = 0 for label in os.listdir(pathToFrames): if label == '.DS_Store': continue cur_class = os.path.join(pathToFrames, label) classified = {} for frame in os.listdir(cur_class): if(label+"_0" in frame): cur_frame = os.path.join(cur_class, frame) # run model on this frame t = label_image.read_tensor_from_image_file(cur_frame) input_name = "import/" + inputLayer output_name = "import/" + outputLayer input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-3:][::-1] labels = label_image.load_labels(labelFile) # print(top_k) for i in top_k: if labels[i] in classified: classified[labels[i]] += 1 else: classified[labels[i]] = 1 break best = keywithmaxval(classified) label_clean = convertLabel(label) print(best) print(label_clean) if(best == label_clean): correct += 1 total += 1 print(total) print(correct) return correct*1.0/total
def __init__(self, graph_name="resources/output_graph_mobile.pb"): """ Initializes the tensorflow session and loads the classifier graph. :param graph_name: the graph name can be overridden to use a different model """ self.graph = load_graph(graph_name) self.input_operation = self.graph.get_operation_by_name( self.input_name) self.output_operation = self.graph.get_operation_by_name( self.output_name) self.session = tf.Session(graph=self.graph) self.labels = load_labels("resources/output_labels.txt") if not os.path.exists("/tmp/photos"): os.makedirs("/tmp/photos")
def main(stdscr): stdscr.clear() # Configure the camera camera = picamera.PiCamera() # configure the graph graph, session = load_graph(args.model) labels = load_labels(args.labels) while True: try: key = stdscr.getkey() stdscr.addstr('Detected key:\n') stdscr.addstr(str(key)) stdscr.addstr('\n') if str(key) == 'q': break if str(key) == 'c': stdscr.clear() camera.start_preview() time.sleep(2) camera.capture('test.jpg') camera.stop_preview() t = read_tensor_from_image_file('test.jpg', input_height=224, input_width=224, input_mean=128, input_std=128) # Start benchmarking input_name = "import/input" output_name = "import/final_result" input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) start = time.time() results = session.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) end=time.time() results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] stdscr.addstr('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start)) for i in top_k: stdscr.addstr(str(labels[i]) + ': ' + str(results[i]) + '\n') except Exception as e: stdscr.addstr('Exception occured') stdscr.addstr(str(e)) pass
def tf_label(graph, filename, label_file): t = label_image.read_tensor_from_image_file(filename, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = label_image.load_labels(label_file) results = list(zip(labels, results)) print(results) return results
def messy(): """ """ with picamera.PiCamera() as camera: camera.resolution = (3200, 2464) time.sleep(1) camera.capture("instant.jpg") print("I took a picture!") file_name = "instant.jpg" model_file = "rooms_82.pb" label_file = "rooms_82.txt" input_height = 299 input_width = 299 input_mean = 0 input_std = 255 input_layer = "Placeholder" output_layer = "final_result" graph = ml.load_graph(model_file) t = ml.read_tensor_from_image_file(file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) input_name = "import/" + input_layer output_name = "import/" + output_layer input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) with ml.tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = ml.load_labels(label_file) messy_end_label = 0 for i in top_k: print(labels[i], results[i]) messy_end_label += float(labels[i]) * float(results[i]) return messy_end_label
def loadImage(filepath, model_file='output_graph.pb', label_file='output_labels.txt', input_height=299, input_width=299, input_mean=0, input_std=255, input_layer='Placeholder', output_layer='final_result'): graph = label_image.load_graph(model_file) t = label_image.read_tensor_from_image_file(filepath, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) input_name = "import/" + input_layer output_name = "import/" + output_layer input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) bestGuess = results.argsort()[-1] labels = label_image.load_labels(label_file) actName = filepath.split('/')[-2] plt.cla() ax = plt.axes() ax.set_title('Predicted: ' + fish_lut[labels[bestGuess]] + '\nActual: ' + fish_lut[actName] + '\nConfidence: ' + str(results[bestGuess])) plt.imshow(mpimg.imread(filepath)) plt.draw()
def recognize(self, file_name, graph): t = read_tensor_from_image_file(file_name, input_height=INPUT_HEIGHT, input_width=INPUT_WIDTH, input_mean=INPUT_MEAN, input_std=INPUT_STD) input_name = "import/" + INPUT_LAYER output_name = "import/" + OUTPUT_LAYER input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) results = self.sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = load_labels(LABEL_FILE) # for i in top_k: # print(labels[i], results[i]) k = top_k[0] return labels[k]
def run_on_image(file_name): t = label_image.read_tensor_from_image_file( file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std, ) input_name = 'import/' + input_layer output_name = 'import/' + output_layer global graph input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = label_image.load_labels(label_file) return labels[top_k[0]]
def take_and_label_picture(): # ts = time.time() # timeStamp = datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M') image_file = 'static/lab.jpg' print('Taking picture and saving to ' + image_file) camera.take_picture(image_file, True) graph = label_image.load_graph(MODEL_FILE) t = label_image.read_tensor_from_image_file(image_file) input_operation = graph.get_operation_by_name('import/Placeholder') output_operation = graph.get_operation_by_name('import/final_result') with tf_session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = numpy.squeeze(results) labels = label_image.load_labels(LABEL_FILE) for i, label in enumerate(labels): result[label] = float(results[i]) print(result) message(result)
def read_single_letter(file_name): model_file = \ "output_graph.pb" label_file = "output_labels.txt" input_height = 224 input_width = 224 input_mean = 0 input_std = 255 input_layer = "input" output_layer = "final_result" graph = label_image.load_graph(model_file) t = label_image.read_tensor_from_image_file(file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) input_name = "import/" + input_layer output_name = "import/" + output_layer input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = label_image.load_labels(label_file) for i in top_k: a = labels[i] b = a.split(" ") print(b) return chr(int(b[1])) break
success, image = cap.read() if success: softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') subtitle_region = process_subtitle.get_subtitle_region(image, line['text']) image_crop = process_image.crop_image(image, subtitle_region) processed_image = process_image.process_image(image_crop, no_contours=True) image_data = cv2_image_to_tensorflow(processed_image) results = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = label_image.load_labels(label_file) answer = labels[top_k[0]] if answer == 'text': subtitle[i]['video_on_text'] = True print('find!') cv2.imwrite('result/text/%s_%s_%d.jpg' % (file_name, line['time'], count), processed_image) count += 1 break elif answer == 'non text': cv2.imwrite('result/non text/%s_%s_%d.jpg' % (file_name, line['time'], count), processed_image) count += 1 else: print('WARNING: cannot find image prediction') if subtitle != parse_subtitle_file('sample/sample1.vtt'):
#!/usr/bin/env python # usage: bash tf_classify_server.sh [PORT_NUMBER] from flask import Flask, request import tensorflow as tf import label_image as tf_classify import json app = Flask(__name__) FLAGS, unparsed = tf_classify.parser.parse_known_args() labels = tf_classify.load_labels(FLAGS.labels) tf_classify.load_graph(FLAGS.graph) sess = tf.Session() @app.route('/', methods=['POST']) def classify(): try: data = request.files.get('data').read() result = tf_classify.run_graph(data, labels, FLAGS.input_layer, FLAGS.output_layer, FLAGS.num_top_predictions, sess) return json.dumps(result), 200 except Exception as e: return repr(e), 500 app.run(host='0.0.0.0',port=12480)
import label_image import imageio import random import time import utils CAR_WORDS = ['car'] files = glob('../rob599_dataset_deploy/test/*/*_image.jpg') files.sort() graph = label_image.load_graph('./retrained_graph.pb') labels = label_image.load_labels('./retrained_labels.txt') fig1 = plt.figure(1, figsize=(16, 9)) fig3 = plt.figure(3, figsize=(10,10)) with open('./outfile.txt','w') as f: f.write('guid/image,N\n') plt.ion() for i in range(len(files)): if i%1 == 0: print("Trial ", i, 'out of ', len(files)) imgpath = files[i] img = plt.imread(imgpath)
"cropped", validation_split=0.2, subset="training", seed=456, image_size=(img_height, img_width), batch_size=batch_size) for image_batch, labels_batch in train_ds: print(image_batch.shape) print(labels_batch.shape) break print("=================================") class_names = np.array(train_ds.class_names) print(class_names) import label_image pre_label = label_image.load_labels("label/labels.txt") for i in range(0, len(class_names)): assert class_names[i] == pre_label[i] normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1. / 255) # train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y)) AUTOTUNE = tf.data.experimental.AUTOTUNE train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE) for image_batch, labels_batch in train_ds: print(image_batch) print(image_batch.shape) print(labels_batch.shape) break New = False model = None
float_caster = tf.cast(img, tf.float32) dims_expander = tf.expand_dims(float_caster, 0) resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width]) normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std]) return normalized if __name__ == '__main__': # init computational graph model_file = '{}/classifier/output_graph.pb'.format(os.environ['HOME']) graph = load_graph(model_file) input_name = "import/conv/Conv2D" output_name = "import/final_result" input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) labels = load_labels('{}/classifier/output_labels.txt'.format(os.environ['HOME'])) # to resize and normalize height = 720 width = 1280 input_height = 299 input_width = 299 input_mean = 128 input_std = 128 imgfiles = sorted(glob.glob('image/*.jpg')) st = time.time() n = len(imgfiles) with graph.as_default(): with tf.Session(graph=graph) as sess:
import label_image import os import csv home_dir = os.path.dirname(os.path.dirname(os.path.realpath('__file__'))) + '/' images = os.listdir(home_dir + '/data/test/') images_dir = [image for image in images] with open('test_lables.csv', 'w') as csvfile: labels = label_image.load_labels( label_file=home_dir + 'data/transfer_modeldata/output_labels.txt') labels.insert(0, 'id') print(labels) spamwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) spamwriter.writerow(labels) for image in images_dir: label_image.main( model_file=home_dir + 'data/transfer_modeldata/output_graph.pb', file_dir=home_dir + 'data/test/' + image, label_file=home_dir + 'data/transfer_modeldata/output_labels.txt', input_height=299, input_width=299, input_mean=0, input_std=255, input_layer='Mul',
def predict(): message = request.get_json(force=True) encoded = message["image"] decoded = base64.b64decode(encoded) img = Image.open(io.BytesIO(decoded)) destination = "images" if not os.path.exists(destination): os.makedirs(destination) now = datetime.datetime.now() rand_str = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8)) file_name = os.path.join(destination, str(now.strftime("%Y-%m-%d-%H-%M-%S-"))+rand_str+'.jpg') try: img.save(os.path.join(file_name), "JPEG", quality=80, optimize=True, progressive=True) except IOError: ImageFile.MAXBLOCK = img.size[0] * img.size[1] img.save(file_name, "JPEG", quality=80, optimize=True, progressive=True) t = label_image.read_tensor_from_image_file(file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) input_name = "import/" + input_layer output_name = "import/" + output_layer input_operation = graph.get_operation_by_name(input_name); output_operation = graph.get_operation_by_name(output_name); with tf.Session(graph=graph) as sess: start = time.time() results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) end=time.time() results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = label_image.load_labels(label_file) print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start)) for i in top_k: if max(results) == results[i]: res=results[i] lab=labels[i] print(labels[i], results[i],"this is max") else: print(labels[i], results[i],"sry is not max") response = { 'prediction': { 'prediction': lab, 'value' : str(res) } } return jsonify(response)
# input_mean=input_mean, # input_std=input_std # ) print(croped.shape) cv2.imshow("croped image", croped) t = croped #t = np.pad(croped,((0,0),(38,38),(0,0)),'constant') # t = t.reshape(1,224,224,3) t = t.reshape(1, 96, 96, 3) print(t.shape) input_name = "import/" + input_layer output_name = "import/" + output_layer input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = li.load_labels(label_file) for i in top_k: print(labels[i], results[i]) cv2.imshow("Window", clone) cv2.waitKey(1) time.sleep(0.25) cv2.destroyAllWindows()
# -*- coding: utf-8 -*- """ Created on Mon Jan 29 20:31:52 2018 @author: TOKIMASA """ import label_image image_data = label_image.load_image('alopias_superciliosus_01.jpg') labels = label_image.load_labels('retrained_labels_2.txt') label_image.load_graph('retrained_graph_2.pb') input_layer = 'DecodeJpeg/contents:0' output_layer = 'final_result:0' num_top_predictions = 5 label_dict = label_image.run_graph(image_data, labels, input_layer, output_layer, num_top_predictions) input_dict = {'image_name':'xxx','longitude':'-0.703107, -120.9375','ocean_name':''} input_dict_ = {'image_name':'xxx','longitude':'','ocean_name':'Pacific Ocean'} ocean_name = get_location(input_dict_) species_name = label_dict['species'] label_dict['ban_boolean'] = get_banboolean(species_name,ocean_name)
def check(self, fileToUpload, submit): try: res = "<html><body><h1>Test Result</h1>" res_var = None if fileToUpload: file_name = "uploads/1.jpg" img = open(file_name, 'wb') img.write(io.BytesIO(fileToUpload.file.read()).read()) img.close() model_file = "output_graph.pb" label_file = "output_labels.txt" input_height = 299 input_width = 299 input_mean = 0 input_std = 255 input_layer = "Mul" output_layer = "final_result" graph = li.load_graph(model_file) t = li.read_tensor_from_image_file( file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) input_name = "import/" + input_layer output_name = "import/" + output_layer input_operation = graph.get_operation_by_name(input_name) output_operation = graph.get_operation_by_name(output_name) with tf.Session(graph=graph) as sess: results = sess.run(output_operation.outputs[0], { input_operation.outputs[0]: t }) results = np.squeeze(results) top_k = results.argsort()[-5:][::-1] labels = li.load_labels(label_file) if results[top_k[0]] > 0.7: res += "<h3>You uploaded image of " + self.cm[str(labels[top_k[0]])] + ' ' + str(results[top_k[0]] * 100) + "%</h3>" res_var = '\n Probability of ' + self.cm[str(labels[top_k[0]])] + ' = ' + str(results[top_k[0]] * 100) + "%" else: res += "<h3>Sorry couldn't detect<br>Try with different image</h3>" try: data = self.get_top() res1 = ''.join([line for line in open('result1.html', 'r')]) res2 = ''.join([line for line in open('result2.html', 'r')]) resm = "<img src=\"uploads/1.jpg\" width=\"400\" >" + "<h1 class=\"mb-10\"> \n Our Result:</h1>" + "<p>" + res_var + "</p>" return data + res1 + resm + res2 except Exception as e1: print(e1) return res + "<a href='/'>Try another</a></body></html>" except Exception as e: print(e) return "<html><body><h1>Please try again (Corrupt or invalid Image)</h1></body></html>"