def display_results(): result_txt = output_path with open(result_txt, 'r') as f: lines = f.readlines() lines.sort(key=lambda x: int(x.split(' ')[0].split('/')[-1][:-4])) # for i in lines: # print(i.strip().split(' ')[0]) img_path_last = lines[0].strip().split(' ')[0] # print(img_path_last) img_last = cv2.imread(img_path_last) for i in range(len(lines)): line = lines[i].strip().split(' ') img_path = line[0] x0, y0, x1, y1 = float(line[1]), float(line[2]), float(line[3]), float(line[4]) label_index = int(line[5]) color_index = lambda x: x if x < 3 else 0 img = cv2.imread(img_path) if img_path != img_path_last: cv2.namedWindow('Result', 0) cv2.resizeWindow('Result', 1800, 1200) cv2.imshow('Result', img_last) cv2.waitKey(delay=500) img_path_last = img_path img_last = img plot_one_box(img_last, [x0, y0, x1, y1], label_index=label_index, label=args.classes_all[label_index], color=color_table[color_index(label_index)])
def inference(self, input_image): boxes_, scores_, labels_ = self.__sess.run( [self.boxes, self.scores, self.labels], feed_dict={self.input_data: input_image}) # rescale the coordinates to the original image boxes_[:, 0] *= (width_ori / float(args.new_size[0])) boxes_[:, 2] *= (width_ori / float(args.new_size[0])) boxes_[:, 1] *= (height_ori / float(args.new_size[1])) boxes_[:, 3] *= (height_ori / float(args.new_size[1])) boxes_[:, 0] = np.ceil(boxes_[:, 0]) boxes_[:, 2] = np.ceil(boxes_[:, 2]) boxes_[:, 1] = np.ceil(boxes_[:, 1]) boxes_[:, 3] = np.ceil(boxes_[:, 3]) print("box coords:") print(boxes_) # f = open('C:/Users/Mr Lin/Desktop/YOLOv3_TensorFlow-master/YOLOv3_TensorFlow-master/data/test.txt', 'r+') # f.write(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]], color=color_table[labels_[i]]) # cv2.imshow('Detection result', img_ori) cv2.imwrite('detection_result.jpg', img_ori)
def demo(input_image): img_ori = cv2.imread(input_image) if resize: 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, :] - 127.5 boxes_, scores_, labels_ = sess.run( [boxes, scores, labels], feed_dict={input_data: img}) # rescale the coordinates to the original image if 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(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('result', img_ori) cv2.waitKey(0)
def drawMap(img, featureMap): width = img.shape[1] height = img.shape[0] n_cols = 10 n_rows = 10 size_w = width // n_cols size_h = height // n_rows rows = height // size_h cols = width // size_w for i in range(rows): for j in range(cols): if featureMap[0][i * rows + j] >= 0.5: x0 = j * size_w x1 = j * size_w + size_w y0 = i * size_h y1 = i * size_h + size_h plot_one_box(img, [x0, y0, x1, y1], label=args.classes[0] + ', {:.2f}%'.format(1 * 100), color=[34, 139, 34])
def show_image(img_ori, boxes_, scores_, classes, num_class, labels_, width_ori, height_ori, new_size): # rescale the coordinates to the original image color_table = get_color_table(num_class) boxes_[:, 0] *= (width_ori / float(new_size[0])) boxes_[:, 2] *= (width_ori / float(new_size[0])) boxes_[:, 1] *= (height_ori / float(new_size[1])) boxes_[:, 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] scores=scores_[i] plot_one_box(img_ori, [x0, y0, x1, y1], label=classes[labels_[i]]+':'+str(scores)[:6], color=color_table[labels_[i]]) cv2.imshow('Detection result', img_ori) # cv2.imwrite('detection_result.jpg', img_ori) cv2.waitKey(30)
# boxes_[j][1]+=person_up # boxes_[j][3]+=person_up smoke_boxes.append(boxes_[j]) smoke_scores.append(scores_[j]) else: print('*'*30) print('No existing Smoke!') else: print('No existing Smoke!') classes = {0:'smoke'} color_table = get_color_table(1) for i in range(len(smoke_boxes)): x0, y0, x1, y1 = smoke_boxes[i] print('change_box:', smoke_boxes[i]) plot_one_box(img_ori, [x0, y0, x1, y1], label='smoke' + ', {:.2f}%'.format(smoke_scores[i] * 100), color=color_table[0]) cv2.imwrite('final_result.jpg', img_ori) # try: # import matplotlib.pyplot as plt # # fig = plt.figure() # a = fig.add_subplot(2, 2, 1) # a.set_title('Result') # #plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # img1 = cv2.cvtColor(image,cv2.COLOR_BGR2RGB) # cv2.imwrite('./save/img1.jpg',img1) # # bgimg = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2RGB) # bgimg = cv2.resize(bgimg, (e.heatMat.shape[1], e.heatMat.shape[0]), interpolation=cv2.INTER_AREA) # cv2.imwrite('./save/bgimg.jpg', bgimg)
for m in img_list: print(m) img_dir = os.path.join(args.input_image_dir, m) img_ori = cv2.imread(img_dir) 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. starttime = datetime.datetime.now() boxes_ = sess.run(boxes, feed_dict={input_data: img}) print(np.shape(boxes_)) endtime = datetime.datetime.now() last_result = postprocess_doctor_yang(boxes_, 608, img_ori.shape[:2]) print("sess cost time is ", endtime - starttime) print("last result is ", last_result) print("box coords:") print('*' * 30) for i, box in enumerate(last_result): print(box) x0, y0, x1, y1, score, label = box plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[int(label)] + ', {:.2f}%'.format(score * 100), color=color_table[int(label)]) cv2.namedWindow('Detection result', 0) cv2.imshow('Detection result', img_ori) # cv2.imwrite('/home/pcl/tf_work/YOLOv3_TensorFlow/results/' + m, img_ori) cv2.waitKey(0) cv2.destroyAllWindows()
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)
print("box coords:") print(boxes_) print('*' * 30) print("scores:") print(scores_) print('*' * 30) print("labels:") print(labels_) if len(boxes_) != 0: dist, angle = getDistanceAndAngle(boxes_[np.argmax(scores_)], width_ori, height_ori) x0, y0, x1, y1 = boxes_[np.argmax(scores_)] plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[np.argmax(scores_)]] + ', {:.2f}%'.format(scores_[np.argmax(scores_)] * 100), color=color_table[labels_[np.argmax(scores_)]], distance=dist, angle=angle) res_name = input_image.split('/')[-1] cv2.imwrite(os.path.join('results', res_name), img_ori) # 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('detection_result.jpg', img_ori) # cv2.waitKey(0)
def test_display_one_img(img_path): img_ori = cv2.imread(img_path) 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 sess_yolo.as_default(): with graph_yolo.as_default(): boxes_, scores_, labels_ = sess_yolo.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])) for j in range(len(boxes_)): x0, y0, x1, y1 = boxes_[j] x0 = np.maximum(x0, 0) y0 = np.maximum(y0, 0) x1 = np.maximum(x1, 0) y1 = np.maximum(y1, 0) label_index = labels_[j] # Crop the detected traffic signs if x1 - x0 > 10 and y1 - y0 > 10 and labels_[j] == 0: img_ori_ = cv2.cvtColor(img_ori, cv2.COLOR_BGR2RGB).astype(np.float32) img_cropped = img_ori_[int(y0):int(y1), int(x0):int(x1)] if img_cropped.any(): tf.reset_default_graph() new_graph = tf.Graph() with new_graph.as_default(): with tf.Session(graph=new_graph) as new_sess: siamese_model = SiameseNet() siamese_model.load_weights( '/home/tracy/PycharmProjects/SiameseNet/checkpoint/RGBscaled/best/my_model' ) img1, img2 = dataloader(img_cropped) label_pred, label_score, _ = siamese_model.prediction( img1, img2) label_pred_, label_score_ = new_sess.run( [label_pred, label_score]) # with sess_siam.as_default(): # with sess_siam.graph.as_default(): # img1, img2 = dataloader(img_cropped) # label_pred, label_score, _ = siamese_model.prediction(img1, img2) # label_pred_, label_score_ = sess_siam.run([label_pred, label_score]) # cv2.imwrite('/home/tracy/YOLOv3_TensorFlow/temp/' + str(i) + '_' + str(j) + '.jpg', img_cropped) # print("Writting %s"%img) # test_one_img('/home/tracy/data/TrafficSign_test/Images1/' + img) # print('Done writing %s'%img) # Choose the one label with highest score pred_labels = np.nonzero(label_pred_) pred_scores = label_score_[pred_labels] # print("pred_scores: ", pred_scores) if len(pred_scores) > 0: label_index = np.argmax(pred_scores) label_index = pred_labels[0][label_index] + 2 # labels_[j] = label_index plot_one_box(img_ori, [x0, y0, x1, y1], label_index=label_index, label=args.classes_all[label_index] + ', {:.2f}%'.format(scores_[j] * 100), color=color_table[labels_[j]]) cv2.namedWindow('Detection result', 0) cv2.resizeWindow('Detection result', 2400, 1800) cv2.imshow('Detection result', img_ori) cv2.imwrite('detection_result.jpg', img_ori) cv2.waitKey(0)
def main(): with tf.Session() as sess: # (Cloud & Edges) Full model information, construct a graph of the model, need to be called on cloud and on edge 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) 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 _, _, _ = gpu_nms(pred_boxes, pred_scores, args.num_class, max_boxes=30, score_thresh=0.4, iou_thresh=0.5) # On Cloud # (Cloud) Model splitter model_splitter(sess, "./data/darknet_weights/yolov3_revised.ckpt") # image reader or video processor img_ori = cv2.imread(args.input_image) # Preprocess height_ori, width_ori = img_ori.shape[:2] img = preprocess(img_ori) # On Edge 1 # (Edge1) Load parameters of the partial model 1 partial_reconstructor(sess, './partial_model_00.ckpt', 'inference_1') # (Edge1) Inference 1 feed_1 = { get_tensor('input_data'): img } # input tensor as key , input value as value out_1 = [ 'inference_1/feature_output_1', 'inference_1/feature_output_2', 'inference_1/feature_output_3' ] # names of the output tensors result_1 = get_features( sess, feed_1, out_1) # get intermediate features from inference 1 # Feature map compression simulation for i in range(len(result_1)): result_1[i][result_1[i] < 0] = 0 result_1[i] = result_1[i] / 25 * 255 result_1[i] = result_1[i].astype(np.uint8) np.savez_compressed('result/messi_inter_feat_quantized', a=result_1[0], b=result_1[1], c=result_1[2]) for i in range(len(result_1)): result_1[i] = result_1[i] / 255 * 25 result_1[i] = result_1[i].astype(np.float32) # On Edge 2 # (Edge2) Load parameters of the parital model 2 partial_reconstructor(sess, './partial_model_01.ckpt', 'inference_2') # (Edge2) Inference 2 with post process feed_2 = { get_tensor('inference_2/feature_input_1'): result_1[0], get_tensor('inference_2/feature_input_2'): result_1[1], get_tensor('inference_2/feature_input_3'): result_1[2] } out_2 = ['result/boxes', 'result/score', 'result/label'] result_2 = get_features(sess, feed_2, out_2) boxes_, scores_, labels_ = result_2 # Show the result image boxes_[:, 0] *= (width_ori / float(args.new_size[0])) boxes_[:, 2] *= (width_ori / float(args.new_size[0])) boxes_[:, 1] *= (height_ori / float(args.new_size[1])) boxes_[:, 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]], color=color_table[labels_[i]]) cv2.imshow('Detection result', img_ori) cv2.imwrite('result/messi_quantized.jpg', img_ori) cv2.waitKey(0)
async def detection(self, img_ori, mode, detection_marker): if self.letterbox_resizes: img, resize_ratio, dw, dh = letterbox_resize( img_ori, self.new_size[0], self.new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(self.new_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. start = time.time() boxes_, scores_, labels_ = self.sess.run( [self.boxes, self.scores, self.labels], feed_dict={self.input_data: img}) end = time.time() print(end - start) # rescale the coordinates to the original image if self.letterbox_resizes: 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(self.new_size[0])) boxes_[:, [1, 3]] *= (height_ori / float(self.new_size[1])) # sort -- tracker for each person dets = [] if len(boxes_) > 0: for i in range(len(boxes_)): x, y, w, h = boxes_[i] dets.append([x, y, x + w, y + h, scores_[i]]) # np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)}) dets = np.asarray(dets) tracks = self.tracker.update(dets) new_boxes = [] indexIDs = [] previous = self.memory.copy() self.memory = {} for track in tracks: new_boxes.append([track[0], track[1], track[2], track[3]]) indexIDs.append(int(track[4])) self.memory[indexIDs[-1]] = new_boxes[-1] if len(new_boxes) > 0: i = 0 for box in new_boxes: x = int(box[0]) y = int(box[1]) w = int(box[2]) h = int(box[3]) color = [ int(c) for c in self.COLORS[indexIDs[i] % len(self.COLORS)] ] if indexIDs[i] in previous: previous_box = previous[indexIDs[i]] (x2, y2) = (int(previous_box[0]), int(previous_box[1])) (w2, h2) = (int(previous_box[2]), int(previous_box[3])) p0 = (int(x + 10), int(y + 100)) p1 = (int(x2 + 10), int(y2 + 100)) cv2.line(img_ori, p0, p1, color, 3) # tracker line if intersect(p0, p1, (detection_marker.X1, detection_marker.Y1), (detection_marker.X2, detection_marker.Y2)): self.counter += 1 if mode == 'PH': if self.classes[ labels_[i]] == 'PHV' or self.classes[ labels_[i]] == 'PH': pass else: self.violation += 1 elif mode == 'PV': if self.classes[ labels_[i]] == 'PHV' or self.classes[ labels_[i]] == 'PV': pass else: self.violation += 1 elif self.classes[labels_[i]] != mode: self.violation += 1 i += 1 cv2.line(img_ori, (detection_marker.X1, detection_marker.Y1), (detection_marker.X2, detection_marker.Y2), (0, 255, 255), 3) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box(img_ori, [x0, y0, x1, y1], label=self.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=self.color_table[labels_[i]]) if mode == 'PH': if self.classes[labels_[i]] == 'PHV' or self.classes[ labels_[i]] == 'PH': pass else: cv2.putText(img_ori, 'Please wear: a helmet', (550, 40), 0, 1, (0, 0, 255), 2) elif mode == 'PV': if self.classes[labels_[i]] == 'PHV' or self.classes[ labels_[i]] == 'PV': pass else: cv2.putText(img_ori, 'Please wear: a safety vest', (550, 40), 0, 1, (0, 0, 255), 2) elif mode == 'PLC' and self.classes[labels_[i]] != 'PLC': cv2.putText(img_ori, 'Please wear: a lab coat', (550, 40), 0, 1, (0, 0, 255), 2) elif mode == 'PHV' and self.classes[labels_[i]] != 'PHV': cv2.putText(img_ori, 'Please wear: a helmet and a safety vest', (550, 40), 0, 1, (0, 0, 255), 2) elif self.classes[labels_[i]] != mode: cv2.putText(img_ori, 'Please wear: ' + str(mode), (550, 40), 0, 1, (0, 0, 255), 2) # print({'TotalViolation': self.violation,'TotalPeople':self.counter}) # cv2.putText(img_ori, mode+' Mode', (300, 40), 0, # fontScale=1, color=(0, 255, 0), thickness=2) # cv2.putText(img_ori, 'People Count: '+ str(self.counter), (40, 620), 0, # 1, (255,255,255), 2) # cv2.putText(img_ori, 'Violation Count: '+ str(self.violation), (40, 660), 0, # 1, (255,255,255), 2) return { 'TotalViolation': self.violation, 'TotalPeople': self.counter }, img_ori
start_time = time.time() boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) end_time = time.time() # rescale the coordinates to the original image boxes_[:, 0] *= (width_ori / float(args.new_size[0])) boxes_[:, 2] *= (width_ori / float(args.new_size[0])) boxes_[:, 1] *= (height_ori / float(args.new_size[1])) boxes_[:, 3] *= (height_ori / float(args.new_size[1])) #print("111") 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]], color=color_table[labels_[i]]) 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'): break vid.release()
# for i in range(len(boxes_)): # print(np.around(boxes_[i],2)) # print('*' * 30) # print("scores:") # for i in range(len(boxes_)): # print('{:.3f}'.format(scores_[i])) # print('*' * 30) # print("labels:") # for l in labels_: # print(str(l)) #写入txt文件 # fp = open('test/txt/test_000369.txt', 'w') # for i in range(len(boxes_)): # box = np.around(boxes_[i], 2) # score = '{:.3f}'.format(scores_[i]) # name = args.classes[labels_[i]] # k=str(name)+' '+str(score)+' '+str(box) # fp.write(k) # fp.write('\n') # fp.close() # 绘制并展示,保存最后的结果 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]), color=color_table[labels_[i]]) cv2.imshow('result', img_ori) #cv2.imwrite('test/result/test.jpg', img_ori) cv2.waitKey(0)
def test_display_one_img(img_path): print(img_path) img_ori = cv2.imread(img_path) print(img_ori.shape) 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 sess_yolo.as_default(): with graph_yolo.as_default(): boxes_, scores_, labels_ = sess_yolo.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])) for j in range(len(boxes_)): x0, y0, x1, y1 = boxes_[j] x0 = np.maximum(x0, 0) y0 = np.maximum(y0, 0) x1 = np.maximum(x1, 0) y1 = np.maximum(y1, 0) label_index = labels_[j] # Crop the detected traffic signs if x1 - x0 > 10 and y1 - y0 > 10 and labels_[j] == 0: # img_ori_ = cv2.cvtColor(img_ori, cv2.COLOR_BGR2RGB).astype(np.float32) img_cropped = img_ori[int(y0):int(y1), int(x0):int(x1)] if img_cropped.shape[0] < 10 or img_cropped.shape[1] < 10: continue # cv2.imwrite('D:/Data/TrafficSigns/test/test_{}.png'.format(j), img_cropped) img_cropped = cv2.resize(img_cropped, (params.image_size, params.image_size)) img_cropped = cv2.cvtColor(img_cropped, cv2.COLOR_BGR2RGB) img_cropped = img_cropped / 255.0 # print(img_cropped) # np.savetxt('D:/Data/test_result/img.txt', img_cropped, fmt='%f', delimiter=',') if img_cropped.any(): # tf.reset_default_graph() # new_graph = tf.Graph() with graph_triplet.as_default(): with sess_triplet.as_default(): image_input = test_input_fn(img_cropped, params) image_input = sess_triplet.run(image_input) label_index = sess_triplet.run( predict_labels, feed_dict={inputs: image_input}) label_index = label_index[0] + 3 print(label_index) # with open('D:/Data/test_result/outputs.txt', 'w') as ff: # ff.writelines(ff) # np.savetxt('D:/Data/test_result/outputs.txt', out, fmt='%f', delimiter=',') plot_one_box(img_ori, [x0, y0, x1, y1], label_index=label_index, label=args.classes_all[label_index] + ', {:.2f}%'.format(scores_[j] * 100), color=color_table[labels_[j]]) cv2.namedWindow('Detection result', 0) cv2.resizeWindow('Detection result', 2400, 1800) cv2.imshow('Detection result', img_ori) cv2.imwrite('detection_result.jpg', img_ori) cv2.waitKey(0)
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
if True: #len(boxes_) != 0: # find car for i in range(len(labels_)): if labels_[i] == 0: if scores_[i] > maxVal: maxVal = scores_[i] maxIndex = i if maxIndex != -1: print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') dist, angle = getDistanceAndAngle(boxes_[maxIndex], width_ori, height_ori) x0, y0, x1, y1 = boxes_[maxIndex] plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[maxIndex]] + ', {:.2f}%'.format(scores_[maxIndex] * 100), color=(189, 101, 0), distance=dist, angle=angle) res_name = input_image.split('/')[-1] #cv2.imwrite(os.path.join('results',res_name), img_ori) # for i in range(len(boxes_)): # if 0 != labels_[i]: # 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('detection_result.jpg', img_ori) # cv2.waitKey(0) cv2.imwrite(os.path.join('resultsSegm', res_name), img_ori)
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))
bbox_3d = compute_projection(points, transform, intrinsics) corners2D_pr = np.transpose(bbox_3d) # # print(corners2D_pr) try: img_ori = draw_demo_img_corners(img_ori, corners2D_pr, (0, 0, 255), nV=8, thickness=16) except: print("Something Went Wrong") # 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("Print Boxes", boxes_) 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=(0, 255, 0), line_thickness=16) if args.save_video: videoWriter.write(img_ori) vid.release() if args.save_video: videoWriter.release()
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)
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 boxes_[:, 0] *= (width_ori / float(args.new_size[0])) boxes_[:, 2] *= (width_ori / float(args.new_size[0])) boxes_[:, 1] *= (height_ori / float(args.new_size[1])) boxes_[:, 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_) #color_table[labels_[i]] 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]], color=(0, 0, 255)) cv2.imshow('Detection result', img_ori) cv2.imwrite('detection_result.jpg', img_ori) cv2.waitKey(0)
def main(_): tellotrack = TelloCV() mission = 0 find_object = 74 bottle = 39 left_count = 0 right_count = 0 tello_is_high = False height_count = 0 move_up = 0 with tf.Graph().as_default(): width, height = args.new_size[0], args.new_size[1] # print(tellotrack.takeoff_time) # tellotrack.take_off() # time.sleep(3) with tf.Session() as sess: input_data = tf.placeholder(tf.float32, [1, width, height, 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.7, nms_thresh=0.7) saver = tf.train.Saver() saver.restore(sess, args.restore_path) tellotrack.take_off() time.sleep(3) # tellotrack.drone.move_up(50) # time.sleep(3) flag = False landing_flag = True while True: img_ori = tellotrack.process_frame() img = cv2.resize(img_ori, (width, height)) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. start = time.time() boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) print(boxes_) end = time.time() boxes_[:, [0, 2]] *= (img_ori.shape[1] / float(width)) boxes_[:, [1, 3]] *= (img_ori.shape[0] / float(height)) if mission == 0: # find clock and move left print(mission, " Start") for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] if labels_[i] == find_object: # 56 chair 11 stopsign 0 person 74 clock print("-------------------------------------FIND-----") plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) # move left and find mission pad, landing. if int(x1 - x0) > 200: tellotrack.move_left() time.sleep(5) tellotrack.drone.move_down(30) mission = 1 else: # getting closer to object tellotrack.go() time.sleep(5) if find_object not in labels_: tellotrack.go() time.sleep(5) elif mission == 1: # find bottle and landing print(mission, " Start") for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] if labels_[i] == bottle: print("-------------------------------------FIND Bottle-----") plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) # x1-x0 > 70 -> 120cm # x1-x0 > 50 -> 200cm if int(x1 - x0) > 50: mid_x = (x1 + x0) / 2 done = tellotrack.track_x(mid_x,left_count, right_count) time.sleep(5) print(done) if done: tellotrack.landing() time.sleep(5) tellotrack.take_off() time.sleep(5) # if landing_flag: # window high # tellotrack.drone.move_up(50) # window low # tellotrack.drone.move_down(50) if left_count > 0 : for i in range(0,left_count): tellotrack.drone.move_right(20) time.sleep(3) elif right_count > 0 : for i in range(0,right_count): tellotrack.drone.move_left(20) time.sleep(3) if flag is False: mission += 1 else: mission = 4 else: tellotrack.drone.move_forward(30) time.sleep(3) if bottle not in labels_: tellotrack.drone.move_forward(30) time.sleep(3) elif mission == 2: # find clock and tracking, go through the window print(mission, " Start") length = [] index = [] for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] if labels_[i] == find_object: # 56 chair 11 stopsign 0 person 74 clock print("-------------------------------------FIND-----") plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) length.append(x1 - x0) index.append(i) # move left and find mission pad, landing elif tello_is_high is False : tellotrack.drone.move_up(50) time.sleep(3) height_count += 1 tello_is_high = True if len(length) > 0: max_length = max(length) max_index = index[length.index(max_length)] x0, y0, x1, y1 = boxes_[max_index] if int(x1 - x0) > 150: center_x = (x0 + x1) / 2 center_y = (y0 + y1) / 2 done = tellotrack.track_mid(center_x, center_y) time.sleep(3) if done: mission = 3 tellotrack.go_fast() time.sleep(5) if flag is True : tellotrack.drone.move_up(50) time.sleep(3) else: tellotrack.drone.move_forward(30) time.sleep(3) else: tellotrack.drone.move_forward(30) time.sleep(3) elif mission == 3: # find clock and rotate clockwise or counter-clockwise print(mission, " Start") for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] if labels_[i] == find_object: # 56 chair 11 stopsign 0 person 74 clock print("-------------------------------------FIND-----") plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) # move left and find mission pad, landing. if int(x1 - x0) > 200: # clock size 180 : 160 cm if flag is False: tellotrack.drone.rotate_clockwise(90) time.sleep(5) # if window 2 low # tellotrack.drone.move_down(50) # time.sleep(5) # if window 2 high #tellotrack.drone.move_up(100) #time.sleep(5) if tello_is_high is True : for i in range (0,height_count): tellotrack.drone.move_down(50) time.sleep(3) tello_is_high = False mission = 2 flag = True break else: tellotrack.drone.rotate_counter_clockwise(90) time.sleep(5) # if window too high, change 50 -> others tellotrack.drone.move_down(50) # if window too low # tellotrack.drone.move_up(50) time.sleep(5) mission = 1 break else: tellotrack.drone.move_forward(60) time.sleep(3) if find_object not in labels_: tellotrack.drone.move_forward(60) time.sleep(3) if len(boxes_) == 0: if flag is False: tellotrack.drone.move_left(30) time.sleep(3) else: tellotrack.drone.move_forward(40) time.sleep(3) elif mission == 4: # finish print(mission, " Start") tellotrack.drone.move_forward(200) time.sleep(5) tellotrack.landing() time.sleep(3) exit() cv2.imshow('YOLO', img_ori) k = cv2.waitKey(1) if k == 1048603 or k == 27: break # esc to quit if k == 1048688: cv2.waitKey(0) # 'p' to pause if k == ord('h'): tellotrack.tracking = True # 'h' to use tracking tellotrack.track_cmd = "" print("Time: " + str(end - start)) del img del img_ori
def img_detect(input_args): """ 图片检测 :param input_args: :return: """ img_ori = cv2.imread(input_args.input_image) # opencv 打开 if input_args.use_letterbox_resize: img, resize_ratio, dw, dh = letterbox_resize(img_ori, pred_args.new_size[0], pred_args.new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(pred_args.new_size)) # img 转RGB, 转float, 归一化 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, np.float32) img = img[np.newaxis, :] / 255. sess = tf.Session() input_data = tf.placeholder( tf.float32, [1, pred_args.new_size[1], pred_args.new_size[0], 3], name='input_data' ) with tf.variable_scope('yolov3'): yolo_model = yolov3(pred_args.num_class, pred_args.anchors) 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, pred_args.num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45) saver = tf.train.Saver() saver.restore(sess, pred_args.weight_path) boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img}) # 还原坐标到原图 if input_args.use_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(pred_args.new_size[0])) boxes_[:, [1, 3]] *= (height_ori / float(pred_args.new_size[1])) print('box coords:', boxes_, '\n' + '*' * 30) print('scores:', scores_, '\n' + '*' * 30) print('labels:', labels_) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box( img_ori, [x0, y0, x1, y1], label=pred_args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=pred_args.color_table[labels_[i]] ) cv2.imshow('Detection result', img_ori) cv2.imwrite(pred_args.output_image, img_ori) cv2.waitKey(0) sess.close()
cv2.imwrite( '/home/tracy/YOLOv3_TensorFlow/temp/' + str(i) + '_' + str(j) + '.jpg', img_cropped) # Choose the one label with highest score pred_labels = np.nonzero(label_pred_) pred_scores = label_score_[pred_labels] # print("pred_scores: ", pred_scores) if len(pred_scores) > 0: label_index = np.argmax(pred_scores) label_index = pred_labels[0][label_index] + 2 # labels_[j] = label_index plot_one_box(img_ori, [x0, y0, x1, y1], label_index=label_index, label=args.classes_all[label_index] + ', {:.2f}%'.format(scores_[j] * 100), color=color_table[labels_[j]]) 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) # cv2.waitKey(delay=300) if args.save_video: videoWriter.write(img_ori) if cv2.waitKey(1) & 0xFF == ord('q'): break
def video_detect(input_args): vid = cv2.VideoCapture(input_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)) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') video_writer = cv2.VideoWriter(pred_args.output_video, fourcc, video_fps, (video_width, video_height)) with tf.Session() as sess: input_data = tf.placeholder(tf.float32, [1, pred_args.new_size[1], pred_args.new_size[0], 3], name='input_data') yolo_model = yolov3(pred_args.num_class, pred_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, pred_args.num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45 ) saver = tf.train.Saver() saver.restore(sess, pred_args.weight_path) for i in range(video_frame_cnt): ret, img_ori = vid.read() if input_args.use_letterbox_resize: img, resize_ratio, dw, dh = letterbox_resize(img_ori, pred_args.new_size[0], pred_args.new_size[1]) else: height_ori, width_ori = img_ori.shape[:2] img = cv2.resize(img_ori, tuple(pred_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}) end_time = time.time() if input_args.use_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(pred_args.new_size[0])) boxes_[:, [1, 3]] *= (height_ori / float(pred_args.new_size[1])) for i in range(len(boxes_)): x0, y0, x1, y1 = boxes_[i] plot_one_box(img_ori, [x0, y0, x1, y1], label=pred_args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=pred_args.color_table[labels_[i]]) 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('Detection result', img_ori) video_writer.write(img_ori) if cv2.waitKey(1) & 0xFF == ord('q'): break vid.release() video_writer.release()
face_results = np.stack( [face_xmin, face_ymin, face_xmax, face_ymax], axis=-1) ratio = match_face(face_results, people_boxes) index = np.argmax(ratio, axis=-1) age[index] = age_labels gender[index] = gender_labels index2 = index for i in range(len(people_boxes)): x0, y0, x1, y1 = people_boxes[i] # height[i] = cal((x0, y0), (x0, y1)) height[i] = random.randint(160, 180) plot_one_box(img_ori, [x0, y0, x1, y1], age[i], gender[i], height[i], label='People', color=(0, 0, 255)) # Save the data into txt with open('./save_data/data.txt', 'a') as f: for i in range(len(people_boxes)): if len(index1) == 0 or index1[i] == -1: save_count += 1 if age[i] == -1: write_age = '' else: write_age = str(age[i]) if gender[i] == -1: write_gender = '' else:
import cv2 from utils.plot_utils import get_color_table, plot_one_box from utils.misc_utils import parse_anchors, read_class_names model = YoloV3('./data/darknet_weights/yolov3_frozen_graph_batch.pb') classes = read_class_names("./data/coco.names") color_table = get_color_table(80) files = glob.glob('./data/demo_data/*.jpg') images = [] vis_images = [] for file in files: image = cv2.imread(file) image = cv2.resize(image, (640, 640)) vis_images.append(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255. #important! images.append(image) images = np.array(images) # inference boxes_, labels_, scores_, num_dect_ = model.run(images) # visualize for idx, image in enumerate(vis_images): for i in range(len(boxes_[idx])): x0, y0, x1, y1 = boxes_[idx][i] plot_one_box(image, [x0, y0, x1, y1], label=classes[labels_[idx][i]] + ', {:.2f}%'.format(scores_[idx][i] * 100), color=color_table[labels_[idx][i]]) out_name = os.path.join('./data/demo_data/results', 'batch_output_' + os.path.basename(files[idx])) cv2.imwrite(out_name, image)
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('detection_result.jpg', img_ori) cv2.waitKey(0) cv2.destroyAllWindows()
def detect_in_video(video_path): # VideoWriter is the responsible of creating a copy of the video # used for the detections but with the detections overlays. Keep in # mind the frame size has to be the same as original video. # out = cv2.VideoWriter('../temp/' + 'WIN_20191218_11_03_57_Pro.mp4', cv2.VideoWriter_fourcc( # 'M', 'J', 'P', 'G'), 10, (1280, 720)) if is_yolo: print('yolo!') configuration = tf.ConfigProto(device_count={"GPU": 0}) sess = tf.Session(config=configuration) 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=1, score_thresh=0.2, nms_thresh=0.45) saver = tf.train.Saver() saver.restore(sess, restore_path) else: detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') configuration = tf.ConfigProto(device_count={"GPU": 0}) sess = tf.Session(config=configuration, graph=detection_graph) # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object # was detected. detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class # label. detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) frame_statistics = [] frame_id = 1 is_skip_frame = True frame_skip_count = 0 # Создать директорию с кадрами для заданного видео video_base_name = os.path.basename(video_path) video_name = os.path.splitext(video_base_name)[0] video_dir = join(os.path.dirname(video_path), video_name) images_dir = "images" video_images_dir = join(video_dir, images_dir) if not os.path.exists(video_images_dir): os.makedirs(video_images_dir) else: # Удалить все кадры из целевой директории remove_files_in_dir(video_images_dir) video_images_dir_rat = join(video_images_dir, 'rat') video_images_dir_mouse = join(video_images_dir, 'mouse') os.makedirs(video_images_dir_rat, exist_ok=True) os.makedirs(video_images_dir_mouse, exist_ok=True) remove_files_in_dir(video_images_dir_rat) remove_files_in_dir(video_images_dir_mouse) # Загрузка видео cap = cv2.VideoCapture(video_path) video_frame_cnt = int(cap.get(7)) video_width = int(cap.get(3)) video_height = int(cap.get(4)) video_fps = int(cap.get(5)) # Узнать разрешение видео video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Указать разрешение картинок cur_dir = os.getcwd() os.chdir(video_images_dir) while cap.isOpened(): # Read the frame ret, frame = cap.read() if frame is not None: # Recolor the frame. By default, OpenCV uses BGR color space. # This short blog post explains this better: # https://www.learnopencv.com/why-does-opencv-use-bgr-color-format/ # color_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if not is_skip_frame: if is_yolo: print('yoloo!!') if is_letterbox_resize: img, resize_ratio, dw, dh = letterbox_resize( frame, new_size[0], new_size[1]) else: height_ori, width_ori = frame.shape[:2] img = cv2.resize(frame, tuple(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}) end_time = time.time() # rescale the coordinates to the original image if is_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(new_size[0])) boxes_[:, [1, 3]] *= (height_ori / float(new_size[1])) for i in range(len(boxes_)): if scores_[i] == max(scores_): x0, y0, x1, y1 = boxes_[i] plot_one_box(frame, [x0, y0, x1, y1], label=classes_yolo[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]]) rodent_confidence = scores_[i] rodent_class_id = labels_[i] + 1 rodent_class_name = classes_yolo[labels_[i]] if rodent_confidence >= .20: frame_statistics.append({ 'frame_id': frame_id, 'confidence': rodent_confidence, 'rodent_class_id': rodent_class_id, 'rodent_class_name': rodent_class_name, }) # Сохранить кадр frame_name = rodent_class_name + '/image' + str( frame_id) + '.jpg' cv2.imwrite(frame_name, frame) # Сохранить xml-файл #scores = np.squeeze(scores[0]) #bbox_coords = boxes[0] #writer = Writer('.', video_width, video_height) #writer.addObject(rodent_class_name, bbox_coords[1] * video_width, #bbox_coords[0] * video_height, bbox_coords[3] * video_width, #bbox_coords[2] * video_height) #writer.save('image' + str(frame_id) + '.xml') #else: # Сохранить кадр #frame_name = 'image' + str(frame_id) + '.jpg' #cv2.imwrite(frame_name, frame) cv2.putText(frame, '{:.2f}ms'.format( (end_time - start_time) * 1000), (40, 40), 0, fontScale=1, color=(0, 255, 0), thickness=2) else: image_np_expanded = np.expand_dims(frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run( [ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: image_np_expanded}) # Visualization of the results of a detection. # note: perform the detections using a higher threshold vis_util.visualize_boxes_and_labels_on_image_array( frame, np.squeeze(boxes[0]), np.squeeze(classes[0]).astype(np.int32), np.squeeze(scores[0]), category_index, use_normalized_coordinates=True, line_thickness=8, max_boxes_to_draw=1, min_score_thresh=.20) # rodent_confidence = np.squeeze(scores[0])[0] # rodent_class_id = np.squeeze(classes[0]).astype(np.int32)[0] # rodent_class_name = category_index[rodent_class_id]['name'] # if rodent_confidence > .20: # frame_statistics.append({'frame_id': frame_id, # 'confidence': rodent_confidence, # 'rodent_class_id': rodent_class_id, # 'rodent_class_name': rodent_class_name, # }) # # # Сохранить кадр # frame_name = rodent_class_name + '/image' + str(frame_id) + '.jpg' # cv2.imwrite(frame_name, frame) # # # Сохранить xml-файл # scores = np.squeeze(scores[0]) # for i in range(min(1, np.squeeze(boxes[0]).shape[0])): # if scores is None or scores[i] > .20: # boxes = tuple(boxes[i].tolist()) # # bbox_coords = boxes[0] # writer = Writer('.', video_width, video_height) # writer.addObject(rodent_class_name, bbox_coords[1] * video_width, # bbox_coords[0] * video_height, bbox_coords[3] * video_width, # bbox_coords[2] * video_height) # writer.save('image' + str(frame_id) + '.xml') # else: # # Сохранить кадр # frame_name = 'image' + str(frame_id) + '.jpg' # cv2.imwrite(frame_name, frame) cv2.imshow('frame', cv2.resize(frame, (800, 600))) output_rgb = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # out.write(output_rgb # Пропустить кадр, если необходимо if is_skip_frame: while 1: key = cv2.waitKey(1) if key == 32: # Нажата клавиша "space" frame_skip_count += 1 print("Вы пропустили " + str(frame_skip_count) + " кадр") break elif key == 113 or key == 233: # Нажата клавиша 'q' ('й') is_skip_frame = False break if cv2.waitKey(1) & 0xFF == ord('q'): break frame_id += 1 # out.release() os.chdir(cur_dir) cap.release() cv2.destroyAllWindows() statistics = { 'frame_count': frame_id, # Количество кадров 'frame_skip_count': frame_skip_count, # Количество пропущенных кадров 'frame_rodent_count': 0, # Количество кадров с грызуном 'frame_rat_count': 0, # Количество кадров с крысой 'frame_mouse_count': 0, # Количество кадров с мышью 'sum_confidence_rat': 0, # Сумма вероятностей крысы на видео 'sum_confidence_mouse': 0, # Сумма вероятностей мыши на видео 'mean_confidence_rat': 0, # Средняя вероятность крысы на видео 'mean_confidence_mouse': 0 # Средняя вероятность мыши на видео } for frame_statistic in frame_statistics: if frame_statistic['rodent_class_name'] == 'rat': statistics['frame_rodent_count'] += 1 statistics['frame_rat_count'] += 1 statistics['sum_confidence_rat'] += frame_statistic['confidence'] statistics['mean_confidence_rat'] = statistics[ 'sum_confidence_rat'] / statistics['frame_rat_count'] elif frame_statistic['rodent_class_name'] == 'mouse': statistics['frame_rodent_count'] += 1 statistics['frame_mouse_count'] += 1 statistics['sum_confidence_mouse'] += frame_statistic['confidence'] statistics['mean_confidence_mouse'] = statistics[ 'sum_confidence_mouse'] / statistics['frame_mouse_count'] print('----->>> Результаты обнаружения <<<-----') print('Количество кадров: ' + str(statistics['frame_count'])) print('Количество пропущенных кадров: ' + str(statistics['frame_skip_count'])) print('Количество кадров с грызуном: ' + str(statistics['frame_rodent_count'])) print('Количество кадров с крысой: ' + str(statistics['frame_rat_count'])) print('Количество кадров с мышью: ' + str(statistics['frame_mouse_count'])) print('Средняя вероятность крысы на видео: ' + str(statistics['mean_confidence_rat'])) print('Средняя вероятность мыши на видео: ' + str(statistics['mean_confidence_mouse']))