def _postprocess(self, data): outputs = {} # 输入参数 image = data['image'] input_size = data['input_size'] # 模型识别结果 image_data = image_preporcess(image.copy(), [input_size, input_size]) predict = self.model_object.predict(image_data) # 结果绘制到图 results = detect(predict, image.shape[:2], input_size, data['min_score']) image, recognizer = draw_bbox(image, results, data['show_box_label']) # 预测次数+1 print('预测次数:', self.count) self.count += 1 # 输出数据,show_image是否输出识别处理的base64图片 outputs['recognizer_data'] = recognizer if data['show_image']: itb64 = image_to_base64(image) outputs['predicted_image'] = itb64 return outputs
def pre_bboxes(self, path1, path2=None): files_path = glob.glob('{}/*jpg'.format(path1)) i = 0 total_best_bboxes = [] for image_path in files_path: img = cv2.imread(image_path) save_img_name = image_path[-5:-14:-1][::-1] bboxes_pr = self.predict(img) origial_bboxes = [] for pr_bboxes in bboxes_pr: # print("pr_bboxes",pr_bboxes) bboxes1 = pr_bboxes.tolist() origial_bboxes.append(bboxes1) origial_bboxes = torch.tensor(origial_bboxes) # print("origial_bboxes",origial_bboxes,type(origial_bboxes)) best_bboxes = utils.py_nms(origial_bboxes, 0.1) best_bboxes = best_bboxes.tolist() save_img = utils.draw_bbox(img, best_bboxes, show_label=True) if path2 != None: save_image_path = os.path.join(path2, save_img_name + '.jpg') cv2.imwrite(save_image_path, save_img) print("保存成功") best_bboxes.sort() # print("best_bboxes",len(best_bboxes),best_bboxes) total_best_bboxes.append(best_bboxes) return total_best_bboxes
def test_image(image_path, model_path): input_size = 416 original_image = cv2.imread(image_path) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size]) image_data = image_data[np.newaxis, ...].astype(np.float32) model = yolov3.build_for_test() # 加载tf model:model.load_weights(model_path);加载darknet model: utils.load_weights(model, model_path) utils.load_weights(model, model_path) model.summary() start_time = time.time() pred_bbox = model.predict(image_data) print('pred_bbox>>>>>>>>>>>>>>>>>', pred_bbox) end_time = time.time() print("time: %.2f ms" %(1000*(end_time-start_time))) pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox] pred_bbox = tf.concat(pred_bbox, axis=0) # 将416×416下的bbox坐标转换为原图上的坐标并删除部分无效box bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') # 构建原图和bbox画出坐标框 image = utils.draw_bbox(original_image, bboxes) image = Image.fromarray(image) image.show()
def predict(self, image_path): original_image = cv2.imread(image_path) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [self.input_size, self.input_size]) image_data = image_data[np.newaxis, ...] with tf.Session(graph=self.graph) as sess: pred_sbbox, pred_mbbox, pred_lbbox = self.sess.run( [ self.return_tensors[1], self.return_tensors[2], self.return_tensors[3] ], feed_dict={self.return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + self.num_classes)), np.reshape(pred_mbbox, (-1, 5 + self.num_classes)), np.reshape(pred_lbbox, (-1, 5 + self.num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, self.input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') image, label = utils.draw_bbox(original_image, bboxes) # image = Image.fromarray(image) # print(image) cv2.imwrite('static/images/test.jpg', image) # image.show() return label
def evaluate(self): predicted_dir_path = './mAP/predicted' ground_truth_dir_path = './mAP/ground-truth' if os.path.exists(predicted_dir_path): shutil.rmtree(predicted_dir_path) if os.path.exists(ground_truth_dir_path): shutil.rmtree(ground_truth_dir_path) if os.path.exists(self.write_image_path): shutil.rmtree(self.write_image_path) os.mkdir(predicted_dir_path) os.mkdir(ground_truth_dir_path) os.mkdir(self.write_image_path) with open(self.annotation_path, 'r') as annotation_file: for num, line in enumerate(annotation_file): annotation = line.strip().split() image_path = annotation[0] image_name = image_path.split('\\')[-1] im1_file = os.path.join(r"C:\Users\gary\Desktop\b09","test","JPEGImages", "rgb",image_name) img_rgb = cv2.imread(im1_file) im2_file = os.path.join(r"C:\Users\gary\Desktop\b09","test","JPEGImages", 'lwir',image_name) img_lwir = cv2.imread(im2_file) # image = cv2.imread(im1_file) bbox_data_gt = np.array([list(map(float, box.split(','))) for box in annotation[1:]]) if len(bbox_data_gt) == 0: bboxes_gt=[] classes_gt=[] else: bboxes_gt, classes_gt = bbox_data_gt[:, :4], bbox_data_gt[:, 4] ground_truth_path = os.path.join(ground_truth_dir_path, str(num) + '.txt') print('=> ground truth of %s:' % image_name) num_bbox_gt = len(bboxes_gt) with open(ground_truth_path, 'w') as f: for i in range(num_bbox_gt): class_name = self.classes[classes_gt[i]] xmin, ymin, xmax, ymax = list(map(str, bboxes_gt[i])) bbox_mess = ' '.join([class_name, xmin, ymin, xmax, ymax]) + '\n' f.write(bbox_mess) print('\t' + str(bbox_mess).strip()) print('=> predict result of %s:' % image_name) predict_result_path = os.path.join(predicted_dir_path, str(num) + '.txt') bboxes_pr = self.predict(img_rgb,img_lwir) if self.write_image: img_rgb = utils.draw_bbox(img_rgb, bboxes_pr, show_label=self.show_label) cv2.imwrite(self.write_image_path+image_name, img_rgb) with open(predict_result_path, 'w') as f: for bbox in bboxes_pr: coor = np.array(bbox[:4], dtype=np.int32) score = bbox[4] class_ind = int(bbox[5]) class_name = self.classes[class_ind] score = '%.4f' % score xmin, ymin, xmax, ymax = list(map(str, coor)) bbox_mess = ' '.join([class_name, score, xmin, ymin, xmax, ymax]) + '\n' f.write(bbox_mess) print('\t' + str(bbox_mess).strip())
def process(image_path, targetFolder, sess, return_tensors): head, tail = os.path.split(image_path) localFileName = tail original_image = cv2.imread(image_path) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size]) image_data = image_data[np.newaxis, ...] pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, score_threshold) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(original_image, bboxes) image = Image.fromarray(image) exportName = "out_" + localFileName filepath = targetFolder + "/" + exportName image.save(filepath)
def main(img_path): img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_input = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE)) img_input = img_input / 255. img_input = img_input[np.newaxis, ...].astype(np.float32) img_input = tf.constant(img_input) pred_bbox = infer(img_input) for key, value in pred_bbox.items(): boxes = value[:, :, 0:4] pred_conf = value[:, :, 4:] boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])), max_output_size_per_class=50, max_total_size=50, iou_threshold=IOU_THRESHOLD, score_threshold=SCORE_THRESHOLD) pred_bbox = [ boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy() ] result = utils.draw_bbox(img, pred_bbox) result = cv2.cvtColor(np.array(result), cv2.COLOR_RGB2BGR) cv2.imwrite('result.png', result)
def yolo ( img ): img_input = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE) , ) img_input = img_input / 255 # cv2.imshow('asd', img_input) img_input = img_input[np.newaxis, ...].astype(np.float32) # yolov4 기반 학습된 모델을 바탕으로 객체 후보군 선정 img_input = tf.constant(img_input) pred_bbox = infer(img_input) # non_max_suppression을 활용한 선정된 BOX 종합. 각 임계점에 맞게 탐지된 객체 BOX 선정 for k, v in pred_bbox.items(): boxes = v[:, :, 0:4] pred_conf = v[:, :, 4:] boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape( boxes, (tf.shape(boxes)[0], -1, 1, 4) ), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1]) ), max_output_size_per_class=10000, max_total_size=200, iou_threshold=IOU_THRESHOLD, score_threshold=SCORE_THRESHOLD ) # 선정된 box 저장하기 pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()] result, center_dots = utils.draw_bbox(img, pred_bbox ) return result , np.asarray(center_dots) , pred_bbox
def predict(image_path): original_image = cv2.imread(image_path) # 读取图片 original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size]) image_data = image_data[np.newaxis, ...] return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements) with tf.Session(graph=graph) as sess: pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.35) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(original_image, bboxes) image = Image.fromarray(image) image.show() image.save(output_path)
def detect_dir(self, origin_dir, save_dir): """ 检测整个目录 """ if not os.path.exists(origin_dir): print("can not find the images dir, path:", origin_dir) raise ValueError if not os.path.exists(save_dir): os.mkdir(save_dir) # 遍历 images_path = glob.glob(os.path.join(origin_dir, "*.??g")) if (len(images_path) == 0): print("directory haven't images.") return for path in images_path: image = cv2.imread(path) # 检测 if (image is not None and len(image.shape) == 3): bboxes_pr = self.predict(image) else: continue # 输出 print(bboxes_pr) # 绘制 image = utils.draw_bbox(image, bboxes_pr) # 保存 path = os.path.basename(path) save_path = os.path.join(save_dir, path) cv2.imwrite(save_path, image)
def evaluate(self,image_path,output_path): #predicted_dir_path = './mAP/predicted' #ground_truth_dir_path = './mAP/ground-truth' #if os.path.exists(predicted_dir_path): shutil.rmtree(predicted_dir_path) #if os.path.exists(ground_truth_dir_path): shutil.rmtree(ground_truth_dir_path) #if os.path.exists(self.write_image_path): shutil.rmtree(self.write_image_path) #os.mkdir(predicted_dir_path) #os.mkdir(ground_truth_dir_path) #os.mkdir(self.write_image_path) #with open(self.annotation_path, 'r') as annotation_file: # for num, line in enumerate(annotation_file): # annotation = line.strip().split() # image_path = annotation[0] # image_name = image_path.split('/')[-1] image = cv2.imread(image_path) bboxes_pr = self.predict(image) if self.write_image: image = utils.draw_bbox(image, bboxes_pr, show_label=self.show_label) cv2.imwrite(output_path, image) with open("./flow_result.txt",'w') as f: for bbox in bboxes_pr: coor = np.array(bbox[:4], dtype=np.int32) score = bbox[4] class_ind = int(bbox[5]) class_name = self.classes[class_ind] score = '%.4f' % score xmin, ymin, xmax, ymax = list(map(str, coor)) print([class_name, score, xmin, ymin, xmax, ymax]) f.write(class_name+" "+str(score)+"\n")
def detect(model, img_path, name, input_size): image_path = img_path original_image = cv2.imread(image_path) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size]) image_data = cv2.resize(original_image, (input_size, input_size)) image_data = image_data / 255. # image_data = image_data[np.newaxis, ...].astype(np.float32) images_data = [] for i in range(1): images_data.append(image_data) images_data = np.asarray(images_data).astype(np.float32) '''if FLAGS.framework == 'tflite': interpreter = tf.lite.Interpreter(model_path=FLAGS.weights) interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() print(input_details) print(output_details) interpreter.set_tensor(input_details[0]['index'], images_data) interpreter.invoke() pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))] if FLAGS.model == 'yolov3' and FLAGS.tiny == True: boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25, input_shape=tf.constant([input_size, input_size])) else: boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size])) else: saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING]) infer = saved_model_loaded.signatures['serving_default']''' batch_data = tf.constant(images_data) pred_bbox = model(batch_data) for key, value in pred_bbox.items(): boxes = value[:, :, 0:4] pred_conf = value[:, :, 4:] boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])), max_output_size_per_class=50, max_total_size=50, iou_threshold=FLAGS.iou, score_threshold=FLAGS.score ) pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()] image = utils.draw_bbox(original_image, pred_bbox) # image = utils.draw_bbox(image_data*255, pred_bbox) image = Image.fromarray(image.astype(np.uint8)) # image.show() image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB) name = name.split(".")[0] + '_d.jpg' output_path = '/'.join([FLAGS.output, name]) cv2.imwrite(output_path, image)
def evaluate(self, image_path): image = cv2.imread(image_path) image_name = image_path.split('\\')[-1].split('/')[-1] filename = image_name.split('.')[-2] predict_image_path = './output/' + image_name predict_result_path = './output/' + filename + '.txt' bboxes_pr = self.predict(image) image = utils.draw_bbox(image, bboxes_pr, show_label=self.show_label) cv2.imwrite(predict_image_path, image) with open(predict_result_path, 'w') as f: for bbox in bboxes_pr: coor = np.array(bbox[:4], dtype=np.int32) score = bbox[4] class_ind = int(bbox[5]) class_name = self.classes[class_ind] score = '%.4f' % score xmin, ymin, xmax, ymax = list(map(str, coor)) bbox_mess = ' '.join( [class_name, score, xmin, ymin, xmax, ymax]) + '\n' f.write(bbox_mess) print('\t' + str(bbox_mess).strip())
def pred_dir(score=0.25): config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS) input_size = FLAGS.size image_dir_path = FLAGS.image print("loading Model ...") saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING]) infer = saved_model_loaded.signatures['serving_default'] imgs = os.listdir(image_dir_path) out_list = [] for img in imgs: if img.split(".")[-1] == 'jpg' or img.split(".")[-1] == 'png': original_image = cv2.imread(image_dir_path + "/" + img) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size]) image_data = cv2.resize(original_image, (input_size, input_size)) image_data = image_data / 255. # image_data = image_data[np.newaxis, ...].astype(np.float32) images_data = [] for i in range(1): images_data.append(image_data) images_data = np.asarray(images_data).astype(np.float32) batch_data = tf.constant(images_data) pred_bbox = infer(batch_data) for key, value in pred_bbox.items(): boxes = value[:, :, 0:4] pred_conf = value[:, :, 4:] boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])), max_output_size_per_class=50, max_total_size=50, iou_threshold=FLAGS.iou, score_threshold=score) pred_bbox = [ boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy() ] image, exist_classes = utils.draw_bbox(original_image, pred_bbox) # image = utils.draw_bbox(image_data*255, pred_bbox) image = Image.fromarray(image.astype(np.uint8)) image.show() image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB) cv2.imwrite("./pred_result/" + img, image) print(img, exist_classes) out_list.append([img, exist_classes])
def test_epoch(D2_model,dectect_epoch_path): with open(cfg.TEST.ANNOT_PATH, 'r') as annotation_file: for num, line in enumerate(annotation_file): annotation = line.strip().split() image_path = annotation[0] image_name = image_path.split('/')[-1] predict_result_path = os.path.join(predicted_epoch_path, str(image_name) + '.txt') original_image = cv2.imread(image_path) image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) # Predict Process ## image_letter, ratio, (dw, dh) = utils.letterbox(image) image_letter = utils.test_image_preprocess(np.copy(image), [INPUT_SIZE, INPUT_SIZE]) image_data = image_letter[np.newaxis, ...].astype(np.float32) batch_data = tf.constant(image_data) bbox_tensors = [] prob_tensors = [] pred_result = D2_model(batch_data,training=False) G_im = pred_result[-1][0] for i in range(ANCHORS.shape[0]): fm = pred_result[i * 2] if i == 0: output_tensors = decode(fm, FLAGS.size // 8, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE) elif i == 1: output_tensors = decode(fm, FLAGS.size // 16, NUM_CLASS, STRIDES, ANCHORS, 1, XYSCALE) elif i==2: output_tensors = decode(fm, FLAGS.size // 32, NUM_CLASS, STRIDES, ANCHORS, 2, XYSCALE) bbox_tensors.append(output_tensors[0]) prob_tensors.append(output_tensors[1]) pred_bbox = tf.concat(bbox_tensors, axis=1) pred_prob = tf.concat(prob_tensors, axis=1) boxes, pred_conf = filter_boxes(pred_bbox, pred_prob, score_threshold=FLAGS.score_thres, input_shape=tf.constant([FLAGS.size, FLAGS.size])) pred_bbox = tf.concat([boxes, pred_conf], axis=-1) boxes = pred_bbox[:, :, 0:4] pred_conf = pred_bbox[:, :, 4:] boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])), max_output_size_per_class=1, max_total_size=1, iou_threshold=FLAGS.iou, score_threshold=FLAGS.score ) boxes, scores, classes, valid_detections = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()] if num % 1 ==0: G_im = pred_result[-1][0] G_im = G_im * 255 G_im = np.array(G_im).astype(np.int32) image_result = utils.draw_bbox(np.copy(G_im), [boxes, scores, classes, valid_detections]) image_result = image_result[:,:,::-1] filepath = dectect_epoch_path+"/"+ str(image_name) cv2.imwrite(filepath, image_result, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
def detect_images(model, image_path, box=None, output_path="", id=0, write_file=True, show=False): """Object classification of the given image. Run the yolo model on the given image. With post process including nms. Save the output image to file or show the image if specified. Args: model: The yolo model to be used. image_path: path to the image. box: bounding box coordinates. Should be a list like: [x1, y1, x2, y2]. output_path: path to write the output image. id: index of bounding box for a given frame. show: whether to show the image for display. """ original_image = cv2.imread(image_path) if box: original_image = original_image[box[1]:box[3], box[0]:box[2]] original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data, old_image_size, new_image_size = utils.image_preprocess( np.copy(original_image)) image_data = image_data[np.newaxis, ...].astype(np.float32) # pred_bbox = model.predict(image_data) pred_bbox = model.predict_on_batch(image_data) pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox] pred_bbox = tf.concat(pred_bbox, axis=0) bboxes = utils.postprocess_boxes(pred_bbox, old_image_size, new_image_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(original_image, bboxes) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if id: i = output_path.rfind('.') output_path = output_path[:i] + '_' + str(id) + output_path[i:] if output_path != '' and write_file: i = output_path.rfind('/') output_directory = output_path[:i] if not os.path.exists(output_directory): os.makedirs(output_directory) cv2.imwrite(output_path, image) if show: # Show the image cv2.imshow("predicted image", image) # Load and hold the image cv2.waitKey(0) # To close the window after the required kill value was provided cv2.destroyAllWindows()
def mul_image(watch_dir="./docs/images", output_path='./output'): imageDir = os.path.abspath(watch_dir) imageList = glob.glob(os.path.join(imageDir, '*.jpg')) # print(imageList) graph = tf.Graph() pb_file = "./yolov3_coco_v3.pb" return_elements = [ "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0" ] return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements) with tf.Session(graph=graph) as sess: for item in imageList: image_path = item # print('item',item) end = "/" name = item[item.rfind(end):] # print(name) num_classes = 80 input_size = 608 out = output_path + name original_image = cv2.imread(image_path) # original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size]) image_data = image_data[np.newaxis, ...] pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.45) # print('bboxes:',bboxes) # bboxes: [[301.13088989 118.44700623 346.95623779 172.39486694 0.97461057 0]...] bboxes = utils.nms(bboxes, 0.45, method='nms') # print('bboxes:',bboxes) # bboxes: [array([105.31238556, 54.51167679, 282.53552246, 147.27146912, 0.99279714, 0. ])] image = utils.draw_bbox(original_image, bboxes) cv2.imwrite(out, image)
def gen(): for i in range(5): count = 0 cap = cv2.VideoCapture("full_video.mp4") start_frame_number = 1000 cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame_number) while True: cap.set(cv2.CAP_PROP_POS_MSEC, (count * 125)) ret, frame = cap.read() count += 1 if not ret: break frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) frame_size = frame.shape[:2] image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...] image_data = image_data.astype(np.float32) prev_time = time.time() request.inputs['input'].CopyFrom( tf.contrib.util.make_tensor_proto( image_data, shape=[1, input_size, input_size, 3])) result_future = stub.Predict.future(request, 10.25) pred_sbbox = np.asarray( result_future.result().outputs['pred_sbbox'].float_val) pred_mbbox = np.asarray( result_future.result().outputs['pred_mbbox'].float_val) pred_lbbox = np.asarray( result_future.result().outputs['pred_lbbox'].float_val) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(frame, bboxes) curr_time = time.time() exec_time = curr_time - prev_time print(exec_time) result = np.asarray(image) frame = cv2.cvtColor(result, cv2.COLOR_RGB2BGR) #result = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #format should be bgr ret, jpeg = cv2.imencode('.jpg', frame) frame = jpeg.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
def video_without_saving(): classes = utils.read_class_names(cfg.YOLO.CLASSES) num_classes = len(classes) return_elements = [ "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0" ] pb_file = "./yolov3_coco.pb" video_path = "docs/images/racoon.mp4" video_path = 0 input_size = 416 graph = tf.Graph() return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements) with tf.Session(graph=graph) as sess: vid = cv2.VideoCapture(video_path) while True: return_value, frame = vid.read() if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) else: raise ValueError("No image!") frame_size = frame.shape[:2] image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...] prev_time = time.time() pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(frame, bboxes) curr_time = time.time() exec_time = curr_time - prev_time result = np.asarray(image) info = "time: %.2f ms" % (1000 * exec_time) cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE) result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.imshow("result", result) if cv2.waitKey(1) & 0xFF == ord('q'): break
def show(pred_bbox): pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox] pred_bbox = tf.concat(pred_bbox, axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(original_image, bboxes, show_label=True) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.imshow('sample', image) cv2.waitKey(0)
def detection(vid): with tf.Session(graph=graph) as sess: return_value, frame = vid.read() if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) else: raise ValueError("No image!") frame_size = frame.shape[:2] image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...] prev_time = time.time() pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') image, detected = utils.draw_bbox(frame, bboxes) detected = np.asarray(detected) # print("------- frame i ---------") class_count = [] for i in range(len(obj_classes)): # 80 obj_count = 0 for j in range(len(detected)): if int(detected[j][5]) == i: obj_count += 1 class_count = np.append(class_count, obj_count) curr_time = time.time() exec_time = curr_time - prev_time result = np.asarray(image) info = "time: %.2f ms" % (1000 * exec_time) # cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE) result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) return result, class_count
def get_object(video_url, threshold=0.45): input_layer = tf.keras.layers.Input([input_size, input_size, 3]) feature_maps = YOLOv3(input_layer) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) utils.load_weights(model, "yolov3_union_10000.weights") model.summary() vid = cv2.VideoCapture(video_url) while True: return_value, frame = vid.read() if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) else: return "No image" frame_size = frame.shape[:2] image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...].astype(np.float32) prev_time = time.time() pred_bbox = model.predict_on_batch(image_data) curr_time = time.time() exec_time = curr_time - prev_time pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox] pred_bbox = tf.concat(pred_bbox, axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, 0.3) bboxes = utils.nms(bboxes, threshold, method='nms') image = utils.draw_bbox(frame, bboxes) result = np.asarray(image) info = "time: %.2f ms" % (1000 * exec_time) cv2.putText(result, text=info, org=(50, 70), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 0, 0), thickness=2) result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) retval, buffer = cv2.imencode(".jpeg", image) yield ((b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n'))
def main(): HOST = '127.0.0.1' PORT = 9003 ADDR = (HOST, PORT) BUFF_SIZE = 1024 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect(ADDR) cap = cv2.VideoCapture(0) while cap.isOpened(): ret, img = cap.read() if not ret: break img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_input = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE)) img_input = img_input / 255. img_input = img_input[np.newaxis, ...].astype(np.float32) img_input = tf.constant(img_input) pred_bbox = infer(img_input) for key, value in pred_bbox.items(): boxes = value[:, :, 0:4] pred_conf = value[:, :, 4:] boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])), max_output_size_per_class=50, max_total_size=50, iou_threshold=IOU_THRESHOLD, score_threshold=SCORE_THRESHOLD) pred_bbox = [ boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy() ] result = utils.draw_bbox(img, pred_bbox, client_socket=clientSocket) result = cv2.cvtColor(np.array(result), cv2.COLOR_RGB2BGR) cv2.imshow('result', result) if cv2.waitKey(1) == ord('q'): clientSocket.close() break
def evaluate(self): time_total = 0.0 predicted_dir_path = './mAP/predicted' ground_truth_dir_path = './mAP/ground-truth' if os.path.exists(predicted_dir_path): shutil.rmtree(predicted_dir_path) if os.path.exists(ground_truth_dir_path): shutil.rmtree(ground_truth_dir_path) if os.path.exists(self.write_image_path): shutil.rmtree(self.write_image_path) os.mkdir(predicted_dir_path) os.mkdir(ground_truth_dir_path) os.mkdir(self.write_image_path) val_preds = [] with open(self.annotation_path, 'r') as annotation_file: image_idx = 0 for num, line in enumerate(annotation_file): st = time.time() annotation = line.strip().split() image_path = annotation[0] image_name = image_path.split('/')[-1] image = cv2.imread(image_path) start = time.time() bboxes_pr = self.predict(image) time_total += time.time() - start image_idx += 1 if self.write_image: image = utils.draw_bbox(image, bboxes_pr, show_label=self.show_label) cv2.imwrite(self.write_image_path + image_name, image) pred_boxcses_per_img = [] for bbox in bboxes_pr: coor = list(np.array(bbox[:4], dtype=np.int32)) score = bbox[4] class_ind = int(bbox[5]) boxcs = [] boxcs.extend(coor) boxcs.append(class_ind) boxcs.append(score) pred_boxcses_per_img.append(boxcs) val_preds.append(pred_boxcses_per_img) cost_T = time.time() - st # print('%d cost_T: %f' % (num, cost_T)) print(image_path) pkl_file = './pred_lst.pkl' fw = open(pkl_file, 'wb') pkl.dump(val_preds, fw, -1) fw.close()
def export_yolo_video(video_path, output_path): vid = cv2.VideoCapture(video_path) out = cv2.VideoWriter( str(Path(output_path, Path(video_path).stem + '_output.mp4')), cv2.VideoWriter_fourcc(*'mp4v'), float(vid.get(cv2.CAP_PROP_FPS)), (round(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), round(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))), ) outputs = [] print(Path(output_path, Path(video_path).stem + '_output.mp4')) print(Path(output_path, Path(video_path).stem + '_output.npz')) max_frames = vid.get(cv2.CAP_PROP_FRAME_COUNT) with tf.Session(graph=graph) as sess: for _ in trange(int(max_frames)): return_value, frame = vid.read() if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) else: raise ValueError("No image!") frame_size = frame.shape[:2] image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...] pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') outputs.append(bboxes) image = utils.draw_bbox(frame, bboxes) result = np.asarray(image) result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) out.write(result) vid.release() out.release() np.savez(str(Path(output_path, Path(video_path).stem + '_output.npz')), outputs)
def main(video_path): cap = cv2.VideoCapture(video_path) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'MP4V') out = cv2.VideoWriter('./data/output.mp4',fourcc, 30.0, (width,height)) while cap.isOpened(): start = time.time() ret, img = cap.read() if not ret: break img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_input = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE)) img_input = img_input / 255. img_input = img_input[np.newaxis, ...].astype(np.float32) img_input = tf.constant(img_input) pred_bbox = infer(img_input) for key, value in pred_bbox.items(): boxes = value[:, :, 0:4] pred_conf = value[:, :, 4:] boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])), max_output_size_per_class=50, max_total_size=50, iou_threshold=IOU_THRESHOLD, score_threshold=SCORE_THRESHOLD ) pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()] result = utils.draw_bbox(img, pred_bbox) result = cv2.cvtColor(np.array(result), cv2.COLOR_RGB2BGR) cv2.imshow('result', result) if cv2.waitKey(1) == ord('q'): break out.write(result) print("time : " , time.time() - start) cap.release() out.release() cv2.destroyAllWindows()
def do_video(self): vid = cv2.VideoCapture(self.video_path) while True: # frame 是 RGB 颜色空间 return_value, frame = vid.read() if return_value: # utils.image_preporcess 这个方法里面有 cv2.COLOR_BGR2RGB 方法 # 如果自己写的模型,可以调一下,也许不需要这里 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) pass else: raise ValueError("No image!") pass frame_size = frame.shape[:2] # 之前训练的时候,转了一次颜色空间 image_data = utils.image_preporcess( np.copy(frame), [self.input_size, self.input_size]) image_data = image_data[np.newaxis, ...] pred_start_time = datetime.now() pred_sbbox, pred_mbbox, pred_lbbox = self.sess.run( [ self.return_tensors[1], self.return_tensors[2], self.return_tensors[3] ], feed_dict={self.return_tensors[0]: image_data}) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + self.class_name_len)), np.reshape(pred_mbbox, (-1, 5 + self.class_name_len)), np.reshape(pred_lbbox, (-1, 5 + self.class_name_len)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, self.input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(frame, bboxes) pred_end_time = datetime.now() print("一帧耗时: {}".format(pred_end_time - pred_start_time)) cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE) result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.imshow("result", result) # 退出按键 if cv2.waitKey(1) & 0xFF == ord('q'): break pass pass
def frame_analyze(frame, tf, recogChar, iou, score, input_size, input_details, output_details, interpreter): # Convert Color, resize frame,... frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) frame_size = frame.shape[:2] image_data = cv2.resize(frame, (input_size, input_size)) image_data = image_data / 255. image_data = image_data[np.newaxis, ...].astype(np.float32) # using output detail from weight to detect interpreter.set_tensor(input_details[0]['index'], image_data) interpreter.invoke() pred = [ interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details)) ] boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant( [input_size, input_size])) boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression( boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)), scores=tf.reshape( pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])), max_output_size_per_class=50, max_total_size=50, iou_threshold=iou, score_threshold=score) # If object Detected, valid_detection is 1 else 0 # If 0 skip, else draw box, then detect the number if valid_detections.numpy()[0] != 0: # draw box pred_bbox = [ boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy() ] draw_box = utils.draw_bbox(frame, pred_bbox) image = draw_box[0] list_plate = draw_box[1] list_plate_no = plate_analyze(list_plate, recogChar) #list_plate_no = plate_analyze(list_plate) return (image, list_plate_no) else: return "no_plate"
def annotate_image(image_data, bbox_data): new_images = [] for im, boxes in zip(image_data, bbox_data): bboxes = np.expand_dims(boxes[:, :4], 0).astype(np.int32) classes = np.expand_dims(boxes[:, 4], 0) valid_detections = np.array([len(boxes)], dtype=np.int32) scores = np.array([[1.0] * len(boxes)], dtype=np.int32) bboxes_stack = [bboxes, scores, classes, valid_detections] annotated_im = utils.draw_bbox(im, bboxes_stack, is_cordinates_relative=False) new_images.append(annotated_im) return np.array(new_images)
def dev_step_draw(image_data, target, model, images): pred_bbox = model(image_data,False) pred_bbox = [pred_bbox[_*2+1] for _ in range(3)] pred_bbox_ = pred_bbox for i, image in enumerate(images): image_name = image.split('/')[-1].split('.')[0] pred_bbox = [tf.reshape(x[i], (-1, tf.shape(x)[-1])) for x in pred_bbox_] pred_bbox = tf.concat(pred_bbox, axis=0) bboxes = utils.postprocess_boxes(pred_bbox, [1000, 1000], 608, cfg.TEST.SCORE_THRESHOLD) bboxes = utils.nms(bboxes, cfg.TEST.IOU_THRESHOLD, method='nms') image = cv2.imread(image) image = utils.draw_bbox(image, bboxes) cv2.imwrite('./predict/%s_%d.jpg'%(image_name,i), image)