def main(): yolo = YOLO() inputs = tf.compat.v1.placeholder(dtype=tf.float32, shape=[1, None, None, 3]) feature_y1, feature_y2, feature_y3 = yolo.forward(inputs, class_num, isTrain=False) pre_boxes, pre_score, pre_label = get_predict_result(feature_y1, feature_y2, feature_y3, anchors[2], anchors[1], anchors[0], width, height, class_num, score_thresh=score_thresh, iou_thresh=iou_thresh, max_box=max_box) init = tf.compat.v1.global_variables_initializer() saver = tf.train.Saver() with tf.compat.v1.Session() as sess: sess.run(init) ckpt = tf.compat.v1.train.get_checkpoint_state(model_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) Log.add_log("message: load ckpt model:'"+str(ckpt.model_checkpoint_path)+"'") else: Log.add_log("message:can not find ckpt model") # exit(1) # assert(0) # dictionary of name of corresponding id word_dict = tools.get_word_dict(name_file) # dictionary of per names color_table = tools.get_color_table(class_num) for name in os.listdir(val_dir): img_name = path.join(val_dir, name) if not path.isfile(img_name): print("'%s' is not file" %img_name) continue img, nw, nh, img_ori, show_img = read_img(img_name, width, height) if img is None: Log.add_log("message:'"+str(img)+"' is None") continue start = time.perf_counter() boxes, score, label = sess.run([pre_boxes, pre_score, pre_label], feed_dict={inputs:img}) end = time.perf_counter() print("%s\t, time:%f s" %(img_name, end-start)) img_ori = tools.draw_img(img_ori, boxes, score, label, word_dict, color_table) cv2.imshow('img', img_ori) cv2.waitKey(0) if save_img: write_img(img_ori, name) pass
def main(): anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 yolo = YOLO(80, anchors) inputs = tf.compat.v1.placeholder(dtype=tf.float32, shape=[1, None, None, 3]) feature_y1, feature_y2, feature_y3 = yolo.forward(inputs, isTrain=False) pre_boxes, pre_score, pre_label = yolo.get_predict_result(feature_y1, feature_y2, feature_y3, 80, score_thresh=config.val_score_thresh, iou_thresh=config.iou_thresh, max_box=config.max_box) # 初始化 init = tf.compat.v1.global_variables_initializer() saver = tf.train.Saver() with tf.compat.v1.Session() as sess: sess.run(init) ckpt = tf.compat.v1.train.get_checkpoint_state("./yolo_weights") if ckpt and ckpt.model_checkpoint_path: print("restore: ", ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) else: exit(1) # 名字字典 word_dict = tools.get_word_dict("./data/coco.names") # 色表 color_table = tools.get_color_table(80) width = 608 height = 608 val_dir = "./coco_test_img" for name in os.listdir(val_dir): img_name = path.join(val_dir, name) if not path.isfile(img_name): print("'%s'不是图片" %img_name) continue start = time.perf_counter() img, img_ori = read_img(img_name, width, height) if img is None: continue boxes, score, label = sess.run([pre_boxes, pre_score, pre_label], feed_dict={inputs:img}) end = time.perf_counter() print("%s\t, time:%f s" %(img_name, end-start)) img_ori = tools.draw_img(img_ori, boxes, score, label, word_dict, color_table) cv2.imshow('img', img_ori) cv2.waitKey(0) save_img(img_ori, name)
def main(): yolo = YOLO(config.class_num, config.anchors) inputs = tf.compat.v1.placeholder(dtype=tf.float32, shape=[1, None, None, 3]) feature_y1, feature_y2, feature_y3 = yolo.forward(inputs, isTrain=False) pre_boxes, pre_score, pre_label = yolo.get_predict_result(feature_y1, feature_y2, feature_y3, config.class_num, score_thresh=config.val_score_thresh, iou_thresh=config.iou_thresh, max_box=config.max_box) # 初始化 init = tf.compat.v1.global_variables_initializer() saver = tf.train.Saver() with tf.compat.v1.Session() as sess: sess.run(init) ckpt = tf.compat.v1.train.get_checkpoint_state(config.model_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) Log.add_log("message:存在 ckpt 模型:'"+str(ckpt.model_checkpoint_path)+"'") else: Log.add_log("message:不存在 ckpt 模型") # exit(1) # 名字字典 word_dict = tools.get_word_dict(config.name_file) # 色表 color_table = tools.get_color_table(config.class_num) width = config.width height = config.height for name in os.listdir(config.val_dir): img_name = path.join(config.val_dir, name) if not path.isfile(img_name): print("'%s'不是图片" %img_name) continue start = time.perf_counter() img, img_ori = read_img(img_name, width, height) if img is None: Log.add_log("message:'"+str(img)+"'图片读取错误") boxes, score, label = sess.run([pre_boxes, pre_score, pre_label], feed_dict={inputs:img}) end = time.perf_counter() print("%s\t, time:%f s" %(img_name, end-start)) img_ori = tools.draw_img(img_ori, boxes, score, label, word_dict, color_table) cv2.imshow('img', img_ori) cv2.waitKey(0) if config.save_img: save_img(img_ori, name)
def main(): class_num = 80 width = 608 height = 608 score_thresh = 0.5 iou_thresh = 0.213 max_box = 50 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 anchors = np.asarray(anchors).astype(np.float32).reshape([-1, 3, 2]) model_dir = "./yolo_weights" name_file = "./data/coco.names" val_dir = "./coco_test_img" yolo = YOLO() inputs = tf.compat.v1.placeholder(dtype=tf.float32, shape=[1, None, None, 3]) feature_y1, feature_y2, feature_y3 = yolo.forward(inputs, class_num, isTrain=False) pre_boxes, pre_score, pre_label = get_predict_result( feature_y1, feature_y2, feature_y3, anchors[2], anchors[1], anchors[0], width, height, class_num, score_thresh=score_thresh, iou_thresh=iou_thresh, max_box=max_box) init = tf.compat.v1.global_variables_initializer() saver = tf.train.Saver() with tf.compat.v1.Session() as sess: sess.run(init) ckpt = tf.compat.v1.train.get_checkpoint_state(model_dir) if ckpt and ckpt.model_checkpoint_path: print("restore model from ", ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) else: print("can not find ckpt model") assert (0) # id to names word_dict = tools.get_word_dict(name_file) # color of corresponding names color_table = tools.get_color_table(class_num) for name in os.listdir(val_dir): img_name = path.join(val_dir, name) if not path.isfile(img_name): print("'%s' is not a file" % img_name) continue start = time.perf_counter() img, img_ori = read_img(img_name, width, height) if img is None: continue boxes, score, label = sess.run([pre_boxes, pre_score, pre_label], feed_dict={inputs: img}) end = time.perf_counter() print("%s\t, time:%f s" % (img_name, end - start)) img_ori = tools.draw_img(img_ori, boxes, score, label, word_dict, color_table) cv2.imshow('img', img_ori) cv2.waitKey(0) save_img(img_ori, name)
def main(): yolo = YOLO(config.class_num, config.anchors) inputs = tf.compat.v1.placeholder(dtype=tf.float32, shape=[1, None, None, 3]) feature_y1, feature_y2, feature_y3 = yolo.forward(inputs, isTrain=False) pre_boxes, pre_score, pre_label = yolo.get_predict_result( feature_y1, feature_y2, feature_y3, config.class_num, score_thresh=config.val_score_thresh, iou_thresh=config.iou_thresh, max_box=config.max_box) init = tf.compat.v1.global_variables_initializer() saver = tf.train.Saver() with tf.compat.v1.Session() as sess: sess.run(init) ckpt = tf.compat.v1.train.get_checkpoint_state(config.model_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) Log.add_log("message: load ckpt model:'" + str(ckpt.model_checkpoint_path) + "'") else: Log.add_log("message: can not fint ckpt model") # exit(1) # dictionary of name of corresponding id word_dict = tools.get_word_dict(config.name_file) # dictionary of per names color_table = tools.get_color_table(config.class_num) width = config.width height = config.height for name in os.listdir(config.val_dir): img_name = path.join(config.val_dir, name) if not path.isfile(img_name): print("'%s' is not file" % img_name) continue start = time.perf_counter() img, nw, nh, img_ori, show_img = read_img(img_name, width, height) if img is None: Log.add_log("message:'" + str(img) + "' is None") boxes, score, label = sess.run([pre_boxes, pre_score, pre_label], feed_dict={inputs: img}) end = time.perf_counter() print("%s\t, time:%f s" % (img_name, end - start)) # show_img = tools.draw_img(show_img, boxes, score, label, word_dict, color_table) # cv2.imshow('img', show_img) # cv2.waitKey(0) if config.keep_img_shape: # 纠正坐标 dw = (width - nw) / 2 dh = (height - nh) / 2 for i in range(len(boxes)): boxes[i][0] = (boxes[i][0] * width - dw) / nw boxes[i][1] = (boxes[i][1] * height - dh) / nh boxes[i][2] = (boxes[i][2] * width - dw) / nw boxes[i][3] = (boxes[i][3] * height - dh) / nh img_ori = tools.draw_img(img_ori, boxes, score, label, word_dict, color_table) cv2.imshow('img_ori', img_ori) cv2.waitKey(0) if config.save_img: save_img(img_ori, name) pass
sess.graph.as_default() tf.import_graph_def(graph_def, name="") sess.run(tf.global_variables_initializer()) # inputs inputs = sess.graph.get_tensor_by_name('Placeholder:0') # output # 'concat_9', 'concat_10', 'concat_11' pre_boxes = sess.graph.get_tensor_by_name('concat_9:0') pre_score = sess.graph.get_tensor_by_name('concat_10:0') # pre_label = sess.graph.get_tensor_by_name('concat_11:0') # width, height = 608, 608 # word_dict = tools.get_word_dict(config.voc_names) # for VOC word_dict = tools.get_word_dict(names_file) # for COCO color_table = tools.get_color_table(class_num) for name in os.listdir(test_imgs_folder): img_name = os.path.join(test_imgs_folder, name) if not os.path.isfile(img_name): continue img, nw, nh, img_ori, show_img = read_img(img_name, width, height) if img is None: print("message:'"+str(img)+"' picture read error") boxes, score, label = sess.run([pre_boxes, pre_score, pre_label], feed_dict={inputs:img}) img_ori = tools.draw_img(img_ori, boxes, score, label, word_dict, color_table) cv2.imshow('img', img_ori) cv2.waitKey(0)
def __init__(self,model_path,GPU_ratio=0.2): #----var JB_flow = 0 class_num = 80 height = 608#416, 608 width = 608#416, 608 score_thresh = 0.5 iou_thresh = 0.213 max_box = 50 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 anchors = np.asarray(anchors).astype(np.float32).reshape([-1, 3, 2]) name_file = "./coco.names" node_dict = {"input": "Placeholder:0", "pre_boxes": "concat_9:0", "pre_score": "concat_10:0", "pre_label": "concat_11:0", } #----model extension check if model_path[-2:] == 'pb': sess, tf_dict = model_restore_from_pb(model_path, node_dict,GPU_ratio=GPU_ratio) tf_input = tf_dict['input'] tf_pre_boxes = tf_dict["pre_boxes"] tf_pre_score = tf_dict['pre_score'] tf_pre_label = tf_dict['pre_label'] else: width = int(model_path.split("\\")[-1].split(".")[0].split("_")[-1]) # 416, 608 height = width # 416, 608 yolo = YOLO() tf_input = tf.compat.v1.placeholder(dtype=tf.float32, shape=[1, None, None, 3]) feature_y1, feature_y2, feature_y3 = yolo.forward(tf_input, class_num, isTrain=False) tf_pre_boxes, tf_pre_score, tf_pre_label = get_predict_result(feature_y1, feature_y2, feature_y3, anchors[2], anchors[1], anchors[0], width, height, class_num, score_thresh=score_thresh, iou_thresh=iou_thresh, max_box=max_box) init = tf.compat.v1.global_variables_initializer() saver = tf.train.Saver() #----GPU ratio setting config = tf.ConfigProto(log_device_placement=True, # 印出目前的運算是使用CPU或GPU allow_soft_placement=True, # 當設備不存在時允許tf選擇一个存在且可用的設備來繼續執行程式 ) if GPU_ratio is None: config.gpu_options.allow_growth = True # 依照程式執行所需要的資料來自動調整 else: config.gpu_options.per_process_gpu_memory_fraction = GPU_ratio # 手動限制GPU資源的使用 sess = tf.compat.v1.Session(config=config) sess.run(init) saver.restore(sess, model_path[:-5]) print("JB> Height: {}, width: {}".format(height, width)) #----label to class name label_dict = tools.get_word_dict(name_file) #----color of corresponding names color_table = tools.get_color_table(class_num) #----local var to global self.JB_flow = JB_flow # show flow self.width = width self.height = height self.tf_input = tf_input self.pre_boxes = tf_pre_boxes self.pre_score = tf_pre_score self.pre_label = tf_pre_label self.sess = sess self.label_dict = label_dict self.color_table = color_table
def main(): anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 #608 anchors #anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 yolo = YOLO(80, anchors, width=416, height=416) inputs = tf.compat.v1.placeholder(dtype=tf.float32, shape=[1, None, None, 3]) feature_y1, feature_y2, feature_y3 = yolo.forward(inputs, isTrain=False) pre_boxes, pre_score, pre_label = yolo.get_predict_result( feature_y1, feature_y2, feature_y3, 80, score_thresh=config.val_score_thresh, iou_thresh=config.iou_thresh, max_box=config.max_box) init = tf.compat.v1.global_variables_initializer() saver = tf.train.Saver() with tf.compat.v1.Session() as sess: sess.run(init) ckpt = tf.compat.v1.train.get_checkpoint_state("./yolo_weights") if ckpt and ckpt.model_checkpoint_path: print("restore: ", ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) else: exit(1) # id to names word_dict = tools.get_word_dict("./data/coco.names") # color of corresponding names color_table = tools.get_color_table(80) width = 416 height = 416 cap = cv2.VideoCapture(0) cap.set(6, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')) # cap.set(3, 1920) # cap.set(4, 1080) while True: start = time.perf_counter() _, frame = cap.read() img_rgb = cv2.resize(frame, (width, height)) img_rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB) img_in = img_rgb.reshape((1, width, height, 3)) / 255. #img, img_ori = read_img(img_name, width, height) boxes, score, label = sess.run([pre_boxes, pre_score, pre_label], feed_dict={inputs: img_in}) end = time.perf_counter() print("time:%f s" % (end - start)) frame = tools.draw_img(frame, boxes, score, label, word_dict, color_table) cv2.imshow('img', frame) if cv2.waitKey(1) & 0xFF == 27: break