def main(): args = build_argparser().parse_args() graph = load_graph(args.model) config = load_module(args.config) image = cv2.imread(args.input_image) img = cv2.resize(image, (94, 24)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.float32(img) img = np.multiply(img, 1.0 / 255.0) input = graph.get_tensor_by_name("import/input:0") output = graph.get_tensor_by_name("import/d_predictions:0") with tf.Session(graph=graph) as sess: results = sess.run(output, feed_dict={input: [img]}) print(results) decoded_lp = decode_beams(results, config.r_vocab)[0] print(decoded_lp) img_to_display = display_license_plate(decoded_lp, image) cv2.imshow('License Plate', img_to_display) cv2.waitKey(0)
def accuracy(label, val, vocab, r_vocab, lpr_patterns): pred = decode_beams(val, r_vocab) label_len = len(label) acc, acc1 = 0, 0 num = 0 for i in range(label_len): if not lpr_pattern_check(label[i].decode('utf-8'), lpr_patterns): # GT label fails print('GT label fails: ' + label[i].decode('utf-8')) continue best = pred[i] #the edit distance show the number of steps that takes to change a string to the other edd = edit_distance(encode(label[i].decode('utf-8'), vocab), encode(best, vocab)) #they return two accuracies if edd <= 1: acc1 += 1 if label[i].decode('utf-8') == best: acc += 1 else: if label[i].decode('utf-8') not in pred[i]: print('Check GT label: ' + label[i].decode('utf-8')) print(label[i].decode('utf-8') + ' -- ' + best + ' Edit Distance: ' + str(edd)) num += 1 return float(acc), float(acc1), num
def main(): # config = load_module(args.config) # image = cv2.imread(args.input_image) # graph = load_graph('weights/graph.pb.frozen') # config = load_module('chinese_lp/config.py') # dic=config.r_vocab # import json # with open('chinese_lp/charmap.json','r') as f: # r_vocab=json.load(f) # img_file='data/demo/16.jpg' # image = cv2.imread('data/demo/4.jpg') args = build_argparser().parse_args() graph = load_graph(args.model) import json with open('chinese_lp/charmap.json', 'r') as f: r_vocab = json.load(f) from detect.predict import Yolo_test as Detector D=Detector() image=D.predict_from_file(args.input_image) if image is None: print('No plate detected.') return img = cv2.resize(image, (94, 24)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.float32(img) img = np.multiply(img, 1.0/255.0) input = graph.get_tensor_by_name("import/input:0") output = graph.get_tensor_by_name("import/d_predictions:0") with tf.Session(graph=graph) as sess: results = sess.run(output, feed_dict={input: [img]}) # print(results) decoded_lp = decode_beams(results, r_vocab=r_vocab)[0] print(decoded_lp) # img_to_display = display_license_plate(decoded_lp, image) # cv2.imshow('License Plate', img_to_display) # cv2.waitKey(0) return decoded_lp
def predict(self,img): graph=self.graph input = graph.get_tensor_by_name("import/input:0") output = graph.get_tensor_by_name("import/d_predictions:0") img = cv2.resize(img, (94, 24)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.float32(img) img = np.multiply(img, 1.0 / 255.0) # with tf.Session(graph=graph) as sess: results = self.sess.run(output, feed_dict={input: [img]}) decoded_lp = decode_beams(results, r_vocab=self.r_vocab)[0] return decoded_lp
def accuracy(label, val, vocab, r_vocab): pred = decode_beams(val, r_vocab) label_len = len(label) acc, acc1 = 0, 0 num = 0 for i in range(label_len): if not lpr_pattern_check(label[i].decode('utf-8')): # GT label fails print('GT label fails: ' + label[i].decode('utf-8')) continue best = pred[i] edd = edit_distance(encode(label[i].decode('utf-8'), vocab), encode(best, vocab)) if edd <= 1: acc1 += 1 if label[i].decode('utf-8') == best: acc += 1 else: if label[i].decode('utf-8') not in pred[i]: print('Check GT label: ' + label[i].decode('utf-8')) print(label[i].decode('utf-8') + ' -- ' + best + ' Edit Distance: ' + str(edd)) num += 1 return float(acc), float(acc1), num
def infer(config): if hasattr(config.infer, 'random_seed'): np.random.seed(config.infer.random_seed) tf.set_random_seed(config.infer.random_seed) random.seed(config.infer.random_seed) if hasattr(config.infer.execution, 'CUDA_VISIBLE_DEVICES'): os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ[ "CUDA_VISIBLE_DEVICES"] = config.train.execution.CUDA_VISIBLE_DEVICES height, width, channels_num = config.input_shape rnn_cells_num = config.rnn_cells_num graph = tf.Graph() with graph.as_default(): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=False): inp_data, filenames = data_input( height, width, channels_num, config.infer.file_list_path, batch_size=config.infer.batch_size) prob = inference(rnn_cells_num, inp_data, config.num_classes) prob = tf.transpose(prob, (1, 0, 2)) # prepare for CTC data_length = tf.fill( [tf.shape(prob)[1]], tf.shape(prob)[0]) # input seq length, batch size result = tf.nn.ctc_greedy_decoder(prob, data_length, merge_repeated=True) predictions = tf.to_int32(result[0][0]) d_predictions = tf.sparse_to_dense(predictions.indices, [ tf.shape(inp_data, out_type=tf.int64)[0], config.max_lp_length ], predictions.values, default_value=-1, name='d_predictions') init = tf.initialize_all_variables() saver = tf.train.Saver(write_version=tf.train.SaverDef.V2) # session conf = tf.ConfigProto() if hasattr(config.eval.execution, 'per_process_gpu_memory_fraction'): conf.gpu_options.per_process_gpu_memory_fraction = config.train.execution.per_process_gpu_memory_fraction if hasattr(config.eval.execution, 'allow_growth'): conf.gpu_options.allow_growth = config.train.execution.allow_growth sess = tf.Session(graph=graph, config=conf) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) sess.run(init) latest_checkpoint = config.infer.checkpoint if config.infer.checkpoint == '': latest_checkpoint = tf.train.latest_checkpoint(config.model_dir) saver.restore(sess, latest_checkpoint) infer_size = dataset_size(config.infer.file_list_path) steps = int(infer_size / config.infer.batch_size) if int(infer_size / config.infer.batch_size) else 1 ii = 0 for _ in range(steps): vals, batch_filenames = sess.run([d_predictions, filenames]) #print(batch_filenames) pred = decode_beams(vals, config.r_vocab) for i, filename in enumerate(batch_filenames): filename = filename.decode('utf-8') img = cv2.imread(filename) size = cv2.getTextSize(pred[i], cv2.FONT_HERSHEY_SIMPLEX, 0.55, 2) text_width = size[0][0] text_height = size[0][1] img_he, img_wi, _ = img.shape img = cv2.copyMakeBorder(img, 0, text_height + 10, 0, 0 if text_width < img_wi else text_width - img_wi, cv2.BORDER_CONSTANT, value=(255, 255, 255)) cv2.putText(img, pred[i], (0, img_he + text_height + 5), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 0), 2) #cv2.imshow('License Plate', img) ii = ii + 1 nameImg = 'result_' + str(ii) + '.png' savePath = "results" pathImg = savePath + "/" + nameImg print("Result saved at: {}".format(pathImg)) cv2.imwrite(os.path.join(savePath, nameImg), img) key = cv2.waitKey(1) if key == 27: break coord.request_stop() coord.join(threads) sess.close()
with tf.Graph().as_default() as graph: tf.import_graph_def(graph_def) return graph if __name__ == "__main__": path = "weight/graph.pb.frozen" image_path = "data/000005.png" image_path = "E:/deepLearning/Synthetic_Chinese_License_Plates/crops/000191.png" config_path = "chinese_lp/config.py" graph = load_graph(path) config = load_module(config_path) image = cv2.imread(image_path) img = cv2.resize(image, (94, 24)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.float32(img) img = np.multiply(img, 1.0 / 255.0) input = graph.get_tensor_by_name("import/input:0") output = graph.get_tensor_by_name("import/d_predictions:0") with tf.Session(graph=graph) as sess: results = sess.run(output, feed_dict={input: [img]}) print(results) print(config.r_vocab) pred = decode_beams(results, config.r_vocab) print(pred)