def __init__(self): self.counter = 0 # number of people self.violation = 0 # number of violations # The path of the anchor txt file. anchor_path = "./data/yolo_anchors.txt" # Resize the input image with `new_size`, size format: [width, height] self.new_size = [416, 416] # Whether to use the letterbox resize. self.letterbox_resizes = True # The path of the class names class_name_path = "./data/coco.names" # The path of the weights to restore restore_path = "./checkpoint/best_model_Epoch_75_step_29487_mAP_0.8609_loss_5.4903_lr_1e-05" # Whether to save the video detection results. self.save_video = True self.anchors = parse_anchors(anchor_path) self.classes = read_class_names(class_name_path) self.num_class = len(self.classes) self.color_table = get_color_table( self.num_class) # color for each label self.tracker = Sort() self.memory = {} self.COLORS = np.random.randint(0, 255, size=(200, 3), dtype="uint8") # tracker color self.sess = tf.Session() self.input_data = tf.compat.v1.placeholder( tf.float32, [1, self.new_size[1], self.new_size[0], 3], name='input_data') yolo_model = yolov3(self.num_class, self.anchors) with tf.compat.v1.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(self.input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict( pred_feature_maps) pred_scores = pred_confs * pred_probs self.boxes, self.scores, self.labels = gpu_nms(pred_boxes, pred_scores, self.num_class, max_boxes=200, score_thresh=0.6, nms_thresh=0.01) saver = tf.compat.v1.train.Saver() saver.restore(self.sess, restore_path)
def __init__(self, class_name_path="./data/class.names", acc_beta=0.8, clip_rewards=0.0 ): # some path self.class_name_path = class_name_path # some numbers self.use_voc_07_metric = False # some params self.classes = read_class_names(self.class_name_path) self.class_num = len(self.classes) # reward params self.clip_rewards = clip_rewards self.beta = acc_beta self.beta_bias = acc_beta self.moving_res_mAP = 0.0
def __init__(self): self.new_size = cfg.img_size self.resize = True self.class_name_path = './data/mydata.names' self.restore_path = cfg.restore_path self.classes = read_class_names(self.class_name_path) self.num_class = len(self.classes) self.color_table = get_color_table(self.num_class) self.sess = tf.Session() self.init_params()
def freeze_graph_test(): image_path = './1.jpg' anchor_path = "./data/yolo_anchors.txt" input_size = [416, 416] class_name_path = "./data/coco.names" pb_path= "./data/darknet_weights/yolov3.pb" score_threshold = 0.5 iou_threshold = 0.5 anchors = parse_anchors(anchor_path) classes = read_class_names(class_name_path) num_class = len(classes) # 读取图像数据 img_ori = cv2.imread(image_path) img_resized = cv2.resize(img_ori, tuple(input_size)) img_resized = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB) img_resized = np.asarray(img_resized, np.float32) img_resized = img_resized[np.newaxis, :] / 255. feature_map = get_feature_map(img_resized, pb_path) pb_test_tf(img_ori, feature_map, classes, num_class, anchors, score_threshold, iou_threshold)
def __init(self): self.anchor_path = "./data/yolo_anchors.txt" self.new_size = [416, 416] self.class_name_path = "./data/my_data/data.names" self.restore_path = "./checkpoint/model-step_17500_loss_0.003654_lr_0.0004995866" self.anchors = parse_anchors(self.anchor_path) self.classes = read_class_names(self.class_name_path) self.num_class = len(self.classes) self.color_tabel = get_color_table(self.num_class) self.img_ori = cv2.imread(self.input_image) self.weight_ori, self.width_ori = self.img_ori.shape[:2] self.img = cv2.resize(img_ori, tuple(self.new_size)) self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB) self.img = np.asarray(self.img, np.float32) self.img = self.img[np.newaxis, :] / 255. #config = tf.ConfigProto() #config.gpu_options.allow_growth = True self.__sess = tf.Session() self.__sess.run(tf.global_variables_initializer()) yolo_model = yolov3(self.num_class, self.anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict( pred_feature_maps) pred_scores = pred_confs * pred_probs self.boxes, self.scores, self.labels = gpu_nms(pred_boxes, pred_scores, args.num_class, max_boxes=100, score_thresh=0.4, iou_thresh=0.5) self.__saver = tf.train.Saver() self.__saver.restore(self.__sess, self.restore_path) self.input_data = tf.placeholder( tf.float32, [1, self.new_size[1], self.new_size[0], 3], name='input_data')
def yolodet(anchor_path, image_path, new_size, letterbox, class_name_path, restore_path): anchors = parse_anchors(anchor_path) classes = read_class_names(class_name_path) num_class = len(classes) color_table = get_color_table(num_class) img_ori = cv2.imread(image_path) if letterbox: img, resize_ratio, dw, dh = letterbox_resize(img_ori, new_size[0], new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(new_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. with tf.Session() as sess: input_data = tf.placeholder(tf.float32, [1, new_size[1], new_size[0], 3], name='input_data') yolo_model = yolov3(num_class, anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45) saver = tf.train.Saver() saver.restore(sess, restore_path) boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) # rescale the coordinates to the original image if letterbox: boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio else: boxes_[:, [0, 2]] *= (width_ori/float(new_size[0])) boxes_[:, [1, 3]] *= (height_ori/float(new_size[1])) # print("box coords:") # print(boxes_) # print('*' * 30) # print("scores:") # print(scores_) # print('*' * 30) # print("labels:") # print(labels_) # # for i in range(len(boxes_)): # x0, y0, x1, y1 = boxes_[i] # plot_one_box(img_ori, [x0, y0, x1, y1], label=classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) # cv2.imshow('Detection result', img_ori) # cv2.imwrite('detection_result.jpg', img_ori) # cv2.waitKey(0) tf.reset_default_graph() return boxes_, scores_, labels_
# from tensorflow.keras import backend as K from keras import models from sklearn import cluster, metrics from utils.misc_utils import parse_anchors, read_class_names from model import yolov3 from kmeans_yolo.model_kmeans import sparse_yolov3 from sliming_yolo.model_sliming import sliming_yolov3 # from ridurre import base_filter_pruning # from ridurre import base_filter_pruning root = '/home/pcl/tf_work/my_github/yolov3_prune' # root = os.getcwd() anchor_path = root + '/data/yolo_anchors.txt' # The path of the anchor txt file. class_name_path = root + '/data//voc.names' # The path of the class names. anchors = parse_anchors(anchor_path) num_class = len(read_class_names(class_name_path)) class BasePruning: _FUZZ_EPSILON = 1e-5 def __init__(self, pruning_factor: float, prune_iter_cnt: int, checkpoint_dir: str,): self._pruning_factor = pruning_factor self._tmp_model_file_name = tempfile.NamedTemporaryFile().name self._prune_iter_cnt = prune_iter_cnt self._channel_number_bins = None self._pruning_factors_for_channel_bins = None self._original_number_of_filters = -1 self._checkpoint_dir = checkpoint_dir self._maximum_prune_iterations = 0 # TODO: select a subset of layers to prune
import tensorflow as tf import numpy as np import argparse import cv2 import glob import matplotlib.pyplot as plt from utils.misc_utils import parse_anchors, read_class_names from utils.nms_utils import batch_nms from utils.plot_utils import get_color_table, plot_one_box from utils.data_aug import letterbox_resize from model import yolov3 anchors = parse_anchors("./data/yolo_anchors.txt") classes = read_class_names("./data/coco.names") num_class = len(classes) color_table = get_color_table(num_class) with tf.Session() as sess: # build graph input_data = tf.placeholder(tf.float32, [None, None, None, 3], name='input') yolo_model = yolov3(num_class, anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps) pred_scores = pred_confs * pred_probs boxes, scores, labels, num_dects = batch_nms(pred_boxes, pred_scores, max_boxes=20, score_thresh=0.5,
def recognize(jpg_path, pb_file_path): anchors = parse_anchors("./data/yolo_anchors.txt") classes = read_class_names("./data/coco.names") num_class = len(classes) color_table = get_color_table(num_class) img_ori = cv2.imread(jpg_path) height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple([IMAGE_SIZE, IMAGE_SIZE])) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. with tf.Graph().as_default(): output_graph_def = tf.GraphDef() with open(pb_file_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(output_graph_def, name="") tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True with tf.Session(config=tf_config) as sess: init = tf.global_variables_initializer() sess.run(init) input_name = "Placeholder" output_name1 = "yolov3/yolov3_head/feature_map_1" output_name2 = "yolov3/yolov3_head/feature_map_2" output_name3 = "yolov3/yolov3_head/feature_map_3" output_names = [output_name1, output_name2, output_name3] yolo_model = yolov3(num_class, anchors) input_data = tf.placeholder(tf.float32, [1, IMAGE_SIZE, IMAGE_SIZE, 3], name='input_data') trt_graph = trt.create_inference_graph( input_graph_def=output_graph_def, outputs=output_names, max_batch_size=1, max_workspace_size_bytes=1 << 25, precision_mode='FP16', minimum_segment_size=5) with open('./data/yolov3_trt.pb', 'wb') as f: f.write(trt_graph.SerializeToString()) tf.import_graph_def(trt_graph, name='') tf_input = sess.graph.get_tensor_by_name(input_name + ':0') feature_map_1 = sess.graph.get_tensor_by_name(output_name1 + ":0") feature_map_2 = sess.graph.get_tensor_by_name(output_name2 + ":0") feature_map_3 = sess.graph.get_tensor_by_name(output_name3 + ":0") features = feature_map_1, feature_map_2, feature_map_3 # tf_scores = tf_sess.graph.get_tensor_by_name('detection_scores:0') # tf_boxes = tf_sess.graph.get_tensor_by_name('detection_boxes:0') # tf_classes = tf_sess.graph.get_tensor_by_name('detection_classes:0') # tf_num_detections = tf_sess.graph.get_tensor_by_name('num_detections:0') # features = sess.run(features, feed_dict={tf_input:np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3])}) # feature1, feature2, feature3 = features # feature1 = tf.convert_to_tensor(feature1) # feature2 = tf.convert_to_tensor(feature2) # feature3 = tf.convert_to_tensor(feature3) # features = feature1, feature2, feature3 yolo_model.pb_forward(input_data) pred_boxes, pred_confs, pred_probs = yolo_model.predict(features) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class, max_boxes=30, score_thresh=0.4, iou_thresh=0.5) boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) # rescale the coordinates to the original image boxes_[:, 0] *= (width_ori / float(IMAGE_SIZE)) boxes_[:, 2] *= (width_ori / float(IMAGE_SIZE)) boxes_[:, 1] *= (height_ori / float(IMAGE_SIZE)) boxes_[:, 3] *= (height_ori / float(IMAGE_SIZE)) print("box coords:") print(boxes_) print('*' * 30) print("scores:") print(scores_) print('*' * 30) print("labels:") print(labels_) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box(img_ori, [x0, y0, x1, y1], label=classes[labels_[i]], color=color_table[labels_[i]]) # cv2.imshow('Detection result', img_ori) cv2.imwrite('detection_result.jpg', img_ori)
def Run(self, img_name, half=True): if self.sess == None: import tensorflow as tf from model import yolov3 from utils.misc_utils import parse_anchors, read_class_names from utils.nms_utils import gpu_nms self.anchors = parse_anchors('./model/yolo_anchors.txt') self.classes = read_class_names('./model/coco.names') self.num_class = len(self.classes) self.sess = tf.Session() self.input_data = tf.placeholder( tf.float32, [1, self.new_size[1], self.new_size[0], 3], name='input_data') self.yolo_model = yolov3(self.num_class, self.anchors) with tf.variable_scope('yolov3'): pred_feature_maps = self.yolo_model.forward( self.input_data, False) self.pred_boxes, self.pred_confs, self.pred_probs, self.map4 = self.yolo_model.predict( pred_feature_maps) self.pred_scores = self.pred_confs * self.pred_probs self.boxes, self.scores, self.labels = gpu_nms(self.pred_boxes, self.pred_scores, self.num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45) self.saver = tf.train.Saver() self.saver.restore(self.sess, self.MODEL_NAME) # self.sess.run(tf.global_variables_initializer()) print('Tensorflow intialized') # print('getting boxes') boxes, scores, feature_map = self.GetBoundingBox(img_name, half) # print('got boxes:',boxes) if len(boxes) != 0: dist, angle = self.getDistanceAndAngle(boxes[np.argmax(scores)], self.width_ori, self.height_ori) self.lastNDistances.append(dist) self.lastNAngles.append(angle) alpha = self.alpha if len(self.lastNDistances) > 1 else 1 self.exponentialMovingAverageDist = alpha * dist + ( 1 - alpha) * self.exponentialMovingAverageDist self.exponentialMovingAverageAngle = alpha * angle + ( 1 - alpha) * self.exponentialMovingAverageAngle angle, _ = self.segmentation.FindPossibleAngle( boxes[np.argmax(scores)], angle, feature_map, self.width_ori, self.height_ori) else: print('No box found') dist, angle = self.Extrapolate() angle, _ = self.segmentation.FindPossibleAngle( boxes, angle, feature_map, self.width_ori, self.height_ori) self.KeepLastN() angle = self.LimitAngles(angle) dist = self.LimitDistance(dist) return dist, angle
def recognize(jpg_path, pb_file_path): anchors = parse_anchors("./data/yolo_anchors.txt") classes = read_class_names("./data/coco.names") num_class = len(classes) color_table = get_color_table(num_class) img_ori = cv2.imread(jpg_path) height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple([IMAGE_SIZE, IMAGE_SIZE])) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) # img = img[np.newaxis, :] / 255. # img_resized = np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3]) with tf.Graph().as_default(): tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True with tf.Session(config=tf_config) as sess: print("Load TRT_Graph File ...") with open(pb_file_path, "rb") as f: output_graph_def = tf.GraphDef() output_graph_def.ParseFromString(f.read()) print("Finished") input_name = "import/Placeholder" output_name1 = "import/yolov3/yolov3_head/feature_map_1" output_name2 = "import/yolov3/yolov3_head/feature_map_2" output_name3 = "import/yolov3/yolov3_head/feature_map_3" output_names = [output_name1, output_name2, output_name3] yolo_model = yolov3(num_class, anchors) print("Import TRT Graph ...") output_node = tf.import_graph_def( output_graph_def, return_elements=[ "yolov3/yolov3_head/feature_map_1", "yolov3/yolov3_head/feature_map_2", "yolov3/yolov3_head/feature_map_3" ]) print("Finished") # for op in tf.get_default_graph().as_graph_def().node: # print(op.name) tf_input = sess.graph.get_tensor_by_name(input_name + ':0') feature_map_1 = sess.graph.get_tensor_by_name(output_name1 + ":0") feature_map_2 = sess.graph.get_tensor_by_name(output_name2 + ":0") feature_map_3 = sess.graph.get_tensor_by_name(output_name3 + ":0") features = feature_map_1, feature_map_2, feature_map_3 sess.run(output_node, feed_dict={tf_input: img[None, ...]}) print("1111111") yolo_model.pb_forward(tf_input) pred_boxes, pred_confs, pred_probs = yolo_model.predict(features) pred_scores = pred_confs * pred_probs print("Detection ......") boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class, max_boxes=30, score_thresh=0.4, iou_thresh=0.5) boxes_, scores_, labels_ = sess.run( [boxes, scores, labels], feed_dict={tf_input: img[None, ...]}) # rescale the coordinates to the original image boxes_[:, 0] *= (width_ori / float(IMAGE_SIZE)) boxes_[:, 2] *= (width_ori / float(IMAGE_SIZE)) boxes_[:, 1] *= (height_ori / float(IMAGE_SIZE)) boxes_[:, 3] *= (height_ori / float(IMAGE_SIZE)) print("box coords:") print(boxes_) print('*' * 30) print("scores:") print(scores_) print('*' * 30) print("labels:") print(labels_) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box(img_ori, [x0, y0, x1, y1], label=classes[labels_[i]], color=color_table[labels_[i]]) # cv2.imshow('Detection result', img_ori) cv2.imwrite('detection_result.jpg', img_ori)
from agegender.agegender_demo import detect_face_age_gender, match_face, match_people from ui import * from get_height import cal from model import yolov3 lines_gender = ['female', 'male'] input_video = "./data/demo_data/test.mp4" anchor_path = "./data/yolo_anchors.txt" new_size = [416, 416] class_name_path = "./data/coco.names" restore_path = "./data/darknet_weights/yolov3.ckpt" anchors = parse_anchors(anchor_path) classes = read_class_names(class_name_path) num_class = len(classes) color_table = get_color_table(num_class) vid = cv2.VideoCapture(input_video) video_frame_cnt = int(vid.get(7)) video_width = int(vid.get(3)) video_height = int(vid.get(4)) video_fps = int(vid.get(5)) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') videoWriter = cv2.VideoWriter('video_result.mp4', fourcc, video_fps, (video_width, video_height)) with tf.Session() as sess:
default=True, help="Whether to use the letterbox resize.") parser.add_argument("--class_name_path", type=str, default="./data/my_data/voc.names", help="The path of the class names.") parser.add_argument( "--restore_path", type=str, default= "/media/ubutnu/fc1a3be7-9b03-427e-9cc9-c4b242cefbff/YOLOv3_TensorFlow/checkpoint/model-epoch_90_step_175083_loss_0.4213_lr_1e-05", help="The path of the weights to restore.") args = parser.parse_args() args.anchors = parse_anchors(args.anchor_path) args.classes = read_class_names(args.class_name_path) args.num_class = len(args.classes) color_table = get_color_table(args.num_class) img_ori = cv2.imread(args.input_image) if args.letterbox_resize: img, resize_ratio, dw, dh = letterbox_resize(img_ori, args.new_size[0], args.new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(args.new_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255.
def single_image_test(imgname): parser = argparse.ArgumentParser( description="YOLO-V3 test single image test procedure.") parser.add_argument("--input_image", type=str, default="./static/uploads/beforeimg/" + imgname, help="The path of the input image.") parser.add_argument("--anchor_path", type=str, default="./data/yolo_anchors.txt", help="The path of the anchor txt file.") parser.add_argument( "--new_size", nargs='*', type=int, default=[416, 416], help= "Resize the input image with `new_size`, size format: [width, height]") parser.add_argument("--letterbox_resize", type=lambda x: (str(x).lower() == 'true'), default=True, help="Whether to use the letterbox resize.") parser.add_argument("--class_name_path", type=str, default="./data/coco.names", help="The path of the class names.") parser.add_argument("--restore_path", type=str, default="./data/darknet_weights/yolov3.ckpt", help="The path of the weights to restore.") args = parser.parse_args() args.anchors = parse_anchors(args.anchor_path) args.classes = read_class_names(args.class_name_path) args.num_class = len(args.classes) color_table = get_color_table(args.num_class) img_ori = cv2.imread(args.input_image) if args.letterbox_resize: img, resize_ratio, dw, dh = letterbox_resize(img_ori, args.new_size[0], args.new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(args.new_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. with tf.Session() as sess: input_data = tf.placeholder(tf.float32, [1, args.new_size[1], args.new_size[0], 3], name='input_data') yolo_model = yolov3(args.num_class, args.anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict( pred_feature_maps) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, args.num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45) saver = tf.train.Saver() saver.restore(sess, args.restore_path) boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) # rescale the coordinates to the original image if args.letterbox_resize: boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio else: boxes_[:, [0, 2]] *= (width_ori / float(args.new_size[0])) boxes_[:, [1, 3]] *= (height_ori / float(args.new_size[1])) print("box coords:") print(boxes_) print('*' * 30) print("scores:") print(scores_) print('*' * 30) print("labels:") print(labels_) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) #cv2.imshow('Detection result', img_ori) cv2.imwrite('static/uploads/afterimg/' + imgname, img_ori) #cv2.waitKey(0) doc = [] doc.append("发现:") item = ["安全帽", "未带安全帽的人"] if (len(labels_) == 0): doc.append("什么都没有发现。") else: for i in range(len(labels_)): doc.append(item[labels_[i]] + ",范围:" + str(boxes_[i]) + ",可能性为:" + str(scores_[i])) return doc
import tensorflow as tf import numpy as np import argparse import cv2 import glob import matplotlib.pyplot as plt from utils.misc_utils import parse_anchors, read_class_names #from utils.nms_utils import batch_nms from utils.nms_utils import gpu_nms from utils.plot_utils import get_color_table, plot_one_box #from utils.data_aug import letterbox_resize from model import yolov3 anchors = parse_anchors("./data/smoke_anchor.txt") classes = read_class_names("./data/my_data/data.names") num_class = len(classes) color_table = get_color_table(num_class) with tf.Session() as sess: # build graph input_data = tf.placeholder(tf.float32, [1, None, None, 3], name='Placeholder') yolo_model = yolov3(num_class, anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class) #boxes, scores, labels, num_dects = batch_nms(pred_boxes, pred_scores, max_boxes=20, score_thresh=0.5, nms_thresh=0.5)
import tensorflow as tf from model import yolov3 from utils.misc_utils import parse_anchors, read_class_names from utils.nms_utils import gpu_nms #from utils.data_aug import letterbox_resize MODEL_NAME = './model/best_model_Epoch_28_step_1565_mAP_0.9963_loss_0.4472_lr_0.0001' new_size = [416, 416] anchors = parse_anchors('model/yolo_anchors.txt') classes = read_class_names('model/coco.names') num_class = len(classes) height_ori, width_ori = 0, 0 #letterbox_resize = True sess = tf.Session() #with tf.Session() as sess: input_data = tf.placeholder(tf.float32, [1, new_size[1], new_size[0], 3], name='input_data') yolo_model = yolov3(num_class, anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class, max_boxes=200,
def recognize(jpg_path, pb_file_path): anchors = parse_anchors("./data/yolo_anchors.txt") classes = read_class_names("./data/coco.names") num_class = len(classes) color_table = get_color_table(num_class) img_ori = cv2.imread(jpg_path) height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple([IMAGE_SIZE, IMAGE_SIZE])) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. with tf.Graph().as_default(): output_graph_def = tf.GraphDef() print("Load Frozen_Graph File ...") with open(pb_file_path, "rb") as f: output_graph_def.ParseFromString(f.read()) tf.import_graph_def(output_graph_def, name="") print("Finished") # GPU_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) config = tf.ConfigProto() # gpu_options=GPU_options) config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: # Define Input and Outputs input_x = sess.graph.get_tensor_by_name("Placeholder:0") feature_map_1 = sess.graph.get_tensor_by_name( "yolov3/yolov3_head/feature_map_1:0") feature_map_2 = sess.graph.get_tensor_by_name( "yolov3/yolov3_head/feature_map_2:0") feature_map_3 = sess.graph.get_tensor_by_name( "yolov3/yolov3_head/feature_map_3:0") features = feature_map_1, feature_map_2, feature_map_3 # yolo config yolo_model = yolov3(num_class, anchors) yolo_model.pb_forward(input_x) # # use frozen_graph to inference # print "RUN Graph ..." # features = sess.run(features, feed_dict={input_x:np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3])}) # print "Finished" # feature1, feature2, feature3 = features # feature1 = tf.convert_to_tensor(feature1) # feature2 = tf.convert_to_tensor(feature2) # feature3 = tf.convert_to_tensor(feature3) # features = feature1, feature2, feature3 print "Predicting ..." pred_boxes, pred_confs, pred_probs = yolo_model.predict(features) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class, max_boxes=30, score_thresh=0.4, iou_thresh=0.5) t0 = time.time() boxes_, scores_, labels_ = sess.run( [boxes, scores, labels], feed_dict={ input_x: np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3]) }) t1 = time.time() print "Finished" # rescale the coordinates to the original image boxes_[:, 0] *= (width_ori / float(IMAGE_SIZE)) boxes_[:, 2] *= (width_ori / float(IMAGE_SIZE)) boxes_[:, 1] *= (height_ori / float(IMAGE_SIZE)) boxes_[:, 3] *= (height_ori / float(IMAGE_SIZE)) print("box coords:") print(boxes_) print('*' * 30) print("scores:") print(scores_) print('*' * 30) print("labels:") print(labels_) print("runtime:") print(t1 - t0) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box(img_ori, [x0, y0, x1, y1], label=classes[labels_[i]], color=color_table[labels_[i]]) #cv2.imshow('Detection result', img_ori) cv2.imwrite('pb_result.jpg', img_ori) #cv2.waitKey(0) num_samples = 50 t0 = time.time() for i in range(num_samples): boxes_, scores_, labels_ = sess.run( [boxes, scores, labels], feed_dict={ input_x: np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3]) }) t1 = time.time() print('Average runtime: %f seconds' % (float(t1 - t0) / num_samples))
parser.add_argument("--warm_up_lr", type=float, default=5e-5, help="Warm up learning rate.") parser.add_argument("--warm_up_epoch", type=int, default=5, help="Warm up training epoches.") flag = parser.parse_args() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) # args params flag.anchors = parse_anchors(flag.anchor_path) flag.classes = read_class_names(flag.class_name_path) flag.class_num = len(flag.classes) flag.train_img_cnt = len(open(flag.train_file, 'r').readlines()) flag.val_img_cnt = len(open(flag.val_file, 'r').readlines()) flag.train_batch_num = int(np.ceil( float(flag.train_img_cnt) / flag.batch_size)) flag.val_batch_num = int(np.ceil(float(flag.val_img_cnt) / flag.batch_size)) # setting loggers logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename=flag.progress_log_path, filemode='w') # setting placeholders
from utils.misc_utils import parse_anchors, read_class_names from utils.nms_utils import gpu_nms from utils.plot_utils import get_color_table, plot_one_box from utils.data_aug import letterbox_resize from model import yolov3 print(tf.version.VERSION) # category_index = [ # {'id': 0, 'name': 'rat'}, # {'id': 1, 'name': 'mouse'} # }] is_yolo = True if is_yolo: anchors = parse_anchors('../data/yolo_anchors.txt') classes_yolo = read_class_names('../data/data.names') num_class = len(classes_yolo) color_table = get_color_table(2) new_size = [416, 416] restore_path = 'checkpoint/model-epoch_30_step_25853_loss_0.1543_lr_0.0001' PATH_TO_LABELS = os.path.join('../data', 'data.names') is_letterbox_resize = True PATH_TO_LABELS = os.path.join('../data', 'labelmap.pbtxt') else: # Path to frozen detection graph. This is the actual model that is used
"D:/Pycharm/Projects/YOLOv3_TensorFlow/data/my_data/traffic_sign_all.names", help="The path of the class names.") parser.add_argument( "--restore_path", type=str, default= "D:/Pycharm/Projects/YOLOv3_TensorFlow/checkpoint/model-epoch_490_step_66284_loss_0.3861_lr_1e-05", help="The path of the weights to restore.") parser.add_argument("--save_video", type=lambda x: (str(x).lower() == 'true'), default=False, help="Whether to save the video detection results.") args = parser.parse_args() args.anchors = parse_anchors(args.anchor_path) args.classes = read_class_names(args.class_name_path) args.classes_all = read_class_names(args.class_name_path_all) args.num_class = len(args.classes) color_table = get_color_table(args.num_class) vid = cv2.VideoCapture(args.input_video) video_frame_cnt = int(vid.get(7)) video_width = int(vid.get(3)) video_height = int(vid.get(4)) video_fps = int(vid.get(5)) if args.save_video: fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') videoWriter = cv2.VideoWriter('video_result.mp4', fourcc, video_fps, (video_width, video_height))
def estimatePose(): parser = argparse.ArgumentParser( description="YOLO-V3 video test procedure.") # parser.add_argument("input_video", type=str, # help="The path of the input video.") parser.add_argument("--anchor_path", type=str, default="./data/yolo_anchors.txt", help="The path of the anchor txt file.") parser.add_argument( "--new_size", nargs='*', type=int, default=[416, 416], help= "Resize the input image with `new_size`, size format: [width, height]") parser.add_argument("--letterbox_resize", type=lambda x: (str(x).lower() == 'true'), default=True, help="Whether to use the letterbox resize.") parser.add_argument("--class_name_path", type=str, default="./data/my_data/YOLOPose.names", help="The path of the class names.") parser.add_argument("--restore_path", type=str, default="./data/pose_weights/lunge_best", help="The path of the weights to restore.") parser.add_argument("--save_video", type=lambda x: (str(x).lower() == 'true'), default=True, help="Whether to save the video detection results.") args = parser.parse_args() args.anchors = parse_anchors(args.anchor_path) args.classes = read_class_names(args.class_name_path) args.num_class = len(args.classes) color_table = get_color_table(args.num_class) # vid = cv2.VideoCapture(args.input_video) vid = cv2.VideoCapture('./data/demo/lunge_03.mp4') # vid = cv2.VideoCapture(r'C:\Users\soma\SMART_Referee\SMART_Referee_DL\data\lunge\video\lunge_03.mp4') video_frame_cnt = int(vid.get(7)) video_width = int(vid.get(3)) video_height = int(vid.get(4)) video_fps = int(vid.get(5)) trainer_pose = pd.read_csv('./data/ground_truth/output_right.csv', header=None) trainer_pose = trainer_pose.loc[:, [ 0, 1, 2, 3, 4, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 ]] pca_df = trainer_pose.loc[:, [ 1, 2, 3, 4, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 ]] pca_df.loc[:, [c for c in pca_df.columns if c % 2 == 1]] = pca_df.loc[:, [c for c in pca_df.columns if c % 2 == 1]] * video_width / 416 pca_df.loc[:, [c for c in pca_df.columns if c % 2 == 0]] = pca_df.loc[:, [c for c in pca_df.columns if c % 2 == 0]] * video_height / 416 pca_df = pca_df.astype(int) pca_df = pca_df.replace(0, np.nan) pca_df = pca_df.dropna() pca_df.describe() pca = PCA(n_components=1) pca.fit(pca_df) size = [video_width, video_height] list_p = [] waist_err = 0 critical_point = 0 past_idx = 0 startTrig = 0 cntdown = 90 t = 0 TRLEN = len(trainer_pose) modify_ankle = pca_df.iloc[0, :].values base_rect = [(int(video_width / 4), int(video_height / 10)), (int(video_width * 3 / 4), int(video_height * 19 / 20))] c_knee = c_waist = c_speed = 0 if args.save_video: fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') videoWriter = cv2.VideoWriter('video_result.mp4', fourcc, video_fps, (video_width, video_height)) with tf.Session() as sess: input_data = tf.placeholder(tf.float32, [1, args.new_size[1], args.new_size[0], 3], name='input_data') yolo_model = yolov3(args.num_class, args.anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict( pred_feature_maps) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, args.num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45) saver = tf.train.Saver() saver.restore(sess, args.restore_path) for i in range(video_frame_cnt): ret, img_ori = vid.read() if args.letterbox_resize: img, resize_ratio, dw, dh = letterbox_resize( img_ori, args.new_size[0], args.new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(args.new_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. start_time = time.time() boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) # rescale the coordinates to the original image if args.letterbox_resize: boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio else: boxes_[:, [0, 2]] *= (width_ori / float(args.new_size[0])) boxes_[:, [1, 3]] *= (height_ori / float(args.new_size[1])) people_pose = get_people_pose(boxes_, labels_, base_rect) # list-dict people_pose = np.array([p[1] for p in people_pose[0] ]).flatten() # dict-tuple -> list people_pose = people_pose[[ 0, 1, 2, 3, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 ]] # Start Trigger if startTrig == 2: pass elif startTrig == 0: # start # 기준 박스 cv2.rectangle(img_ori, base_rect[0], base_rect[1], (0, 0, 255), 2) if isInBox(people_pose, base_rect[0], base_rect[1]): # t_resize_pose = resize_pose(people_pose, trainer_pose.iloc[0, 1:].values) t_resize_pose = resize_pose(people_pose, pca_df.iloc[0, :].values) img_ori = draw_ground_truth(img_ori, t_resize_pose) # img_ori = draw_ground_truth(img_ori, pca_df.iloc[0, :].values) startTrig = isStart(people_pose, trainer_pose.iloc[0, 1:].values, size) cv2.imshow('image', img_ori) if cv2.waitKey(1) & 0xFF == ord('q'): break continue else: print("박스안에 들어와주세요!!") continue elif startTrig == 1: img_ori = draw_ground_truth(img_ori, pca_df.iloc[0, :].values) cv2.putText(img_ori, str(int(cntdown / 30)), (100, 300), cv2.FONT_HERSHEY_SIMPLEX, 10, (255, 0, 0), 10) cv2.imshow('image', img_ori) cntdown -= 1 if cntdown == 0: startTrig = 2 if cv2.waitKey(1) & 0xFF == ord('q'): break continue '''check ankle : 편차 40이상 발생시 전에 값 으로 업데이트''' people_pose = check_ankle(list_p, people_pose, modify_ankle, size) # f = open('user.csv', 'a', encoding='utf-8', newline='') # wr = csv.writer(f) # wr.writerow(people_pose) # ground truth 그리기 list_p.append(people_pose) img_ori = draw_ground_truth(img_ori, pca_df.iloc[t, :].values) if check_waist(people_pose): waist_err += 1 if waist_err is 60: # waist_err는 60번 틀리면 피드백함 feedback_waist() c_waist += 1 waist_err = 0 if trainer_pose.iloc[t, 0] == 1: # t는 특정 시점 + i frame critical_point += 1 if critical_point % 2 == 0: my_pose = makeMypose_df(list_p) c_speed = check_speed( my_pose, trainer_pose.iloc[past_idx:t + 1, 1:], pca, c_speed) c_knee = check_knee(people_pose, c_knee) modify_ankle = list_p[-1] list_p = [] past_idx = t t += 1 if t == TRLEN: break # img_ori = draw_body(img_ori, boxes_, labels_) # for i in range(len(boxes_)): # x0, y0, x1, y1 = boxes_[i] # plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) # 사용자 자세 그리기 # img_ori = draw_truth(img_ori, people_pose) end_time = time.time() cv2.putText(img_ori, '{:.2f}ms'.format((end_time - start_time) * 1000), (40, 40), 0, fontScale=1, color=(0, 255, 0), thickness=2) cv2.imshow('image', img_ori) if args.save_video: videoWriter.write(img_ori) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.destroyAllWindows() break vid.release() cv2.destroyAllWindows() if args.save_video: videoWriter.release() f = open('./data/score/result.csv', 'a', encoding='utf-8', newline='') wr = csv.writer(f) d = datetime.today().strftime("%Y/%m/%d") t = datetime.today().strftime("%H:%M:%S") wr.writerow([d, t, c_knee, c_waist, c_speed])
def yolodet(image_path, anchor_path=rootpath + "/yolo/data/yolo_anchors.txt", new_size=[416, 416], letterbox=True, class_name_path=rootpath + "/yolo/data/coco.names", restore_path=rootpath + "/yolo/data/best_model"): anchors = parse_anchors(anchor_path) classes = read_class_names(class_name_path) num_class = len(classes) color_table = get_color_table(num_class) img_ori = cv2.imread(image_path) if letterbox: img, resize_ratio, dw, dh = letterbox_resize(img_ori, new_size[0], new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(new_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. with tf.Session() as sess: input_data = tf.placeholder(tf.float32, [1, new_size[1], new_size[0], 3], name='input_data') yolo_model = yolov3(num_class, anchors) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(input_data, False) pred_boxes, pred_confs, pred_probs = yolo_model.predict( pred_feature_maps) pred_scores = pred_confs * pred_probs boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45) saver = tf.train.Saver() saver.restore(sess, restore_path) boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) # rescale the coordinates to the original image if letterbox: boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio else: boxes_[:, [0, 2]] *= (width_ori / float(new_size[0])) boxes_[:, [1, 3]] *= (height_ori / float(new_size[1])) tf.reset_default_graph() #transform detections into 1 line (#1class,#1conf,#1xmin,#1ymin,#1max,#1ymax,#2class,#2conf,...) boxes = [] for i in range(np.shape(boxes_)[0]): boxes.append(labels_[i]) boxes.append(scores_[i]) boxes.extend(boxes_[i, :]) return boxes
keypoint[i] = center #cv2.circle(npimg, center, 3, common.CocoColors[i], thickness=3, lineType=8, shift=0) print('keypoint:', keypoint) keypoint.setdefault(6) keypoint.setdefault(16) if keypoint[6] and keypoint[16]: print('keypoint[6]:',keypoint[6]) print('keypoint[16]:',keypoint[16]) cilp_img = npimg[keypoint[16][1]:keypoint[6][1], keypoint[16][0]:keypoint[6][0]] img_name = 'cilp_img_'+str(count)+ '.jpg' cv2.imwrite(img_name, cilp_img) #os.system('python /home/dp/shuju/linhaihua3/Smoke/Smoke/YOLOv3_TensorFlow-master/test_single_image.py clip_img_.jpg') model = YoloV3('./smoke1_frozen_graph_batch.pb') boxes_, labels_, scores_ = model.run(img_name) print('boxes:',boxes_) classes = read_class_names("/home/dp/shuju/linhaihua3/Smoke/Smoke/YOLOv3_TensorFlow-master/data/my_data/data.names") color_table = get_color_table(80) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box(cilp_img, [x0, y0, x1, y1], label=classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) # out_name = os.path.join('./data/demo_data/results', 'batch_output_' + os.path.basename(files[idx])) out_name = os.path.join('batch_output_' + str(count)+'.jpg') cv2.imwrite(out_name,cilp_img) count+=1 # try: # import matplotlib.pyplot as plt # # fig = plt.figure() # a = fig.add_subplot(2, 2, 1)
if label not in class_num_dic: class_num_dic[label] = 1 else: class_num_dic[label] += 1 logger.info_ai( meg="prepare data over, get all class name from data file", get_ins={"class_num_dic": class_num_dic}) lr_decay_freq = int( config_common.model_set["lr_decay_freq_epoch"] * train_num / (len(gpu_device) * config_common.model_set["batch_size"])) # args params anchors = parse_anchors(config_common.data_set["anchor_path"]) * 2.5 classes = read_class_names(config_common.data_set["class_name_path"]) classes_list = list(classes.values()) logger.info_ai(meg="train class name get from class name file", get_ins={"classes_list": classes_list}) #把不在训练类别里面的类别从类别个数字典中删除 for key in list(class_num_dic.keys()): if key not in classes_list: class_num_dic.pop(key) logger.info_ai( meg="************data calss name:%s not in classes list" % key) class_num_dic = { key: 1 / (value + 1) for key, value in class_num_dic.items() }
# -*- coding:utf-8 -*- from utils.misc_utils import parse_anchors, read_class_names from utils.plot_utils import get_color_table """ 检测配置 """ detect_object = 'img' # 默认检测对象 input_image = './data/demo_data/person.jpg' # 默认图片路径 input_video = './data/demo_data/video_demo.mp4' # 默认视频路径 output_image = './data/demo_data/result/result.jpg' # 保存图片路径 output_video = './data/demo_data/result/result.mp4' # 保存视频路径 anchor_path = './data/yolo_anchors.txt' # anchor 文件路径 anchors = parse_anchors(anchor_path) # anchor内容 weight_path = './data/darknet_weights/yolov3.ckpt' # weights路径 class_name_path = './data/coco.names' # 类别文件路径 classes = read_class_names(class_name_path) # 类别文件list num_class = len(classes) # 类别数量 new_size = [416, 416] # 图片改变后的大小 use_letterbox_resize = True # 是否使用letterbox color_table = get_color_table(num_class) # 根据类别数生成颜色列表