def model_reponse(self, data_string, original_image_size): channel = implementations.insecure_channel(self.host, int( self.port)) # 创建channel凭据 stub = prediction_service_pb2_grpc.PredictionServiceStub( channel._channel) # 利用.proto文件生成的类创建服务存根 request = predict_pb2.PredictRequest() # 请求类型 request.model_spec.name = self.model_name # 待评估模型的名称 request.model_spec.signature_name = 'serving_default' # 待评估模型的签名 request.inputs['images'].CopyFrom( tf.contrib.util.make_tensor_proto(data_string, shape=[1, 416, 416, 3])) # 输入数据格式转换 result = stub.Predict(request, 10.0) sbbox = np.array(list(result.outputs['out1'].float_val)) mbbox = np.array(list(result.outputs['out2'].float_val)) lbbox = np.array(list(result.outputs['out3'].float_val)) pred_bbox = np.concatenate([ np.reshape(sbbox, (-1, 85)), np.reshape(mbbox, (-1, 85)), np.reshape(lbbox, (-1, 85)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, 416, 0.3) bboxes = utils.nms(bboxes, 0.15, method='nms') return bboxes
def is_img(img_cv, color): j = 0 if len(img_cv) != 0: print("---1312--------------") for i in range (len(img_cv)): im_cv_r = cv2.resize(img_cv[i], (1300, 414)) gray = cv2.cvtColor(im_cv_r, cv2.COLOR_BGR2GRAY) equ = cv2.equalizeHist(gray) gaussian = cv2.GaussianBlur(gray, (3, 3), 0, 0, cv2.BORDER_DEFAULT) median = cv2.medianBlur(gaussian, 3) original_image = median 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, ...] data = json.dumps({"signature_name": "serving_default", "instances": image_data.tolist()}) headers = {"content-type": "application/json"} num_classes=65 json_response = requests.post( 'http://tf:port/v1/models/yolov3:predict', data=data, headers=headers) predictions = json.loads(json_response.text)['predictions'] pred_sbbox, pred_mbbox, pred_lbbox =predictions[0]['pred_sbbox'],predictions[0]['pred_mbbox'],predictions[0]['pred_lbbox'] 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.3) bboxes = utils.nms(bboxes, 0.45, method='nms') if np.array(bboxes).shape[0] > 6: image = utils.draw_bbox(im_cv_r, bboxes) # print(image) name = color +'im' + str(i) + '.jpg' path = os.path.join("./pre_out/", name) cv2.imwrite(path,image) print("-------------")
def detect(self, image_path, filters=None): print("=== detect {}".format(image_path)) #image_name = image_path.split('/')[-1] pos = image_path.rfind('/') p = image_path.rfind('\\') if (p > pos): pos = p image_name = image_path[pos + 1:] print("=== image_name {}".format(image_name)) image = cv2.imread(image_path) #image = Image.open(image_path) #image = image.convert("RGB") #image = np.asarray(Image.open(path).convert('RGB')) src_image = image.copy() image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) h, w, _ = image.shape image = np.array(image) image, scale = preprocess_image(image, image_size=self.image_size) # run network start = time.time() boxes, scores, labels = self.model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) elapsed = time.time() - start print("=== elapsed time {}".format(elapsed)) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) #print("=== boxes{}".format(boxes)) # select indices which have a score above the threshold indices = np.where(scores[:] > self.score_threshold)[0] #print("=== indices{}".format(indices)) # select those detections boxes = boxes[indices] labels = labels[indices] #print("=== boxes{}".format(boxes)) #print("=== labels {}".format(labels)) detected_objects = [] objects_stats = {} draw_boxes_with_filters(src_image, boxes, scores, labels, self.colors, self.classes, detected_objects, objects_stats, filters) self.SEP = ", " self.NL = "\n" self.save_detected_image(image_name, src_image, filters) self.save_detected_objects(image_name, detected_objects, filters) self.save_objects_stats(image_name, objects_stats, filters)
def infer(image: str, phi: int = 0, saved_model: str = './savedmodel', classes: dict = None, score_threshold: float = 0.3, nms_threshold: float = 0.5, device: str = 'gpu'): if device != 'gpu': os.environ['CUDA_VISIBLE_DEVICES'] = '-1' #Using CPU else: os.environ['CUDA_VISIBLE_DEVICES'] = '0' #Using GPU #For COCO dataset if classes == None: classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } #select resolution according to architecture image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] #To get different color for each class num_classes = len(classes.values()) colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] #load the model model = load_model(saved_model) #load and preprocess image img = cv2.imread(image) src_image = img.copy() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) h, w = img.shape[:2] img, scale = preprocess_image(img, image_size=image_size) #detect and post process start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(img, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze( labels) end = time.time() boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # print(f'infer time: {end-start}, fps: {1/(end-start)}') # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # indices = tf.image.non_max_suppression(boxes,scores,max_output_size=[100],iou_threshold = nms_threshold,score_threshold = score_threshold) boxes = boxes[indices] labels = labels[indices] #draw boxes on the original image draw_boxes(src_image, boxes, scores, labels, colors, classes) return src_image
def predict(self): np.set_printoptions(threshold=np.inf) image_path = './414162.jpg' image = np.array(cv2.imread(image_path)) image_shape = image.shape print("image_shape: ", image_shape) image = np.copy(image) image_data = utils.image_preprocess(image, [self.input_size, self.input_size]) image_data = image_data[np.newaxis, ...] pred_bbox = self.sess.run([self.pred_bbox], feed_dict={ self.input: image_data, self.training: False }) pred_bbox = np.array(pred_bbox[0]) pred_bbox = utils.postprocess_boxes(pred_bbox, image_shape, 416, 0.5) print("pred_bbox shape: ", pred_bbox.shape) pred_bbox = utils.nms(pred_bbox, 0.45) print("pred_bbox after: ", pred_bbox) image = utils.draw_bbox(image, pred_bbox, show_label=True) cv2.imwrite('./test.jpg', image)
def main(_argv): input_layer = tf.keras.layers.Input([FLAGS.size, FLAGS.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) # model.summary() utils.load_weights(model, FLAGS.weights) test_img = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img_size = test_img.shape[:2] test_img = tf.expand_dims(test_img, 0) test_img = utils.transform_images(test_img, FLAGS.size) pred_bbox = model.predict(test_img) pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox] pred_bbox = tf.concat(pred_bbox, axis=0) boxes = utils.postprocess_boxes(pred_bbox, img_size, FLAGS.size, 0.3) boxes = utils.nms(boxes, 0.45, method='nms') original_image = cv2.imread(FLAGS.image) img = utils.draw_outputs(original_image, boxes) cv2.imwrite(FLAGS.output, img)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = False model_path = 'checkpoints/flir_59_0.1409_0.6001.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 3 score_threshold = 0.01 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) video_path = r'G:\datasets\iray\cap\20200515_100016.avi' video_path = r'G:\datasets\iray\cap\20200515_100016.avi' video_path = r'G:\projects\20200513_102922.avi' cap = cv2.VideoCapture(video_path) while True: ret, image = cap.read() if not ret: break src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 # Architecture number D# weighted_bifpn = True model_path = 'efficientdet-d0.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] # _, model = efficientdet(phi=phi, # weighted_bifpn=weighted_bifpn, # num_classes=num_classes, # score_threshold=score_threshold) # model.load_weights(model_path, by_name=True) # tf.saved_model.save(model,'savedmodel') #Convert .h5 weights to .pb model model = load_model('savedmodel/') for image_path in glob.glob('test/2.jpg'): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0) cv2.imwrite('out.jpg', src_image)
def main(_argv): input_layer = tf.keras.layers.Input([FLAGS.size, FLAGS.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) # model.summary() utils.load_weights(model, FLAGS.weights) times = [] try: vid = cv2.VideoCapture(int(FLAGS.video)) except: vid = cv2.VideoCapture(FLAGS.video) width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(vid.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*FLAGS.output_format) out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height)) while True: _, img = vid.read() if img is None: logging.warning("Empty Frame") time.sleep(0.1) continue img_size = img.shape[:2] img_in = tf.expand_dims(img, 0) img_in = utils.transform_images(img_in, FLAGS.size) t1 = time.time() pred_bbox = model.predict(img_in) t2 = time.time() times.append(t2 - t1) times = times[-20:] pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox] pred_bbox = tf.concat(pred_bbox, axis=0) boxes = utils.postprocess_boxes(pred_bbox, img_size, FLAGS.size, 0.3) boxes = utils.nms(boxes, 0.45, method='nms') img = utils.draw_outputs(img, boxes) img = cv2.putText( img, "Time: {:.2f}ms".format(sum(times) / len(times) * 1000), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) if FLAGS.output: out.write(img) cv2.imshow('output', img) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows()
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = False model_path = 'checkpoints/deepfashion.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('deepfashion_13.json', 'r')).values() } num_classes = 13 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] #_, model = efficientdet(phi=phi, # weighted_bifpn=weighted_bifpn, # num_classes=num_classes, # score_threshold=score_threshold) models = EfficientDetModel(0) model = models.p_model model.load_weights(model_path, by_name=True) # 'datasets/VOC2007/JPEGImages/*.jpg' for image_path in glob.glob('data/sample_val/image/000002.jpg'): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels, masks = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) #cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imwrite('results/image.jpg', src_image) #cv2.imread('results/image.jpg') '''
def main(image_path): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 1 weighted_bifpn = True model_path = 'efficientdet-d1.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) #for image_path in glob.glob('datasets/VOC2007/JPEGImages/*.jpg'): try: image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0) except Exception as ex: print(ex)
def object_detect(input_path="./road.jpg", output_path='./demo.jpg'): img_size = 608 num_channels = 3 # image_path = "./docs/images/sample_computer.jpg" image_path = input_path # 调用图片,示例:"./docs/images/sample_computer.jpg" original_image = cv2.imread(image_path) # original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) image_data = utils.image_preporcess(np.copy(original_image), [img_size, img_size]) # 图片处理成608*608*3 # print(image_data.shape) plt.imshow(image_data) plt.show() yolov3_api = "http://localhost:8501/v1/models/yolov3:predict" # 刚刚产生的接口 image_data_yolo_list = image_data[np.newaxis, :].tolist() # 转化为多维数组矩阵 headers = {"Content-type": "application/json"} r = requests.post(yolov3_api, headers=headers, data=json.dumps({ "signature_name": "predict", "instances": image_data_yolo_list })).json() #post请求 # print('r',r) # 19, 19, 85 = 30685 # {'error': 'Input to reshape is a tensor with 18411 values, but the requested shape requires a multiple of 30685\n\t [[{{node pred_multi_scale/Reshape_2}}]]'} # 18411 的因子 [3, 17, 19, 51, 57, 323, 361, 969, 1083, 6137] output = np.array(r['predictions']) # print(output.shape) # (63, 19, 19, 85) reduction factor 注:衰减系数以及步长:32 608/32=19 85 = 80类+1可能性+4个坐标 # 416 x 416 则为 13*13 output = np.reshape( output, (-1, 85 )) # 这一步处理成 22743*85的维度(63*19*19 =22743, 85 = 80类+1可能性+4个坐标,根据自己数据集改) # print(output.shape) original_image_size = original_image.shape[:2] bboxes = utils.postprocess_boxes( output, original_image_size, img_size, 0.3) # 这一步是将所有可能的预测信息提取出来,主要是三类:类别,可能性,坐标值。 bboxes = utils.nms(bboxes, 0.45, method='nms') # 这一步是 将刚刚提取出来的信息进行筛选,返回最好的预测值,同样是三类。 image = utils.draw_bbox(original_image, bboxes) # 这一步是把结果画到新图上面。 image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) image = Image.fromarray(image) image.show() image.save(output_path) # 保存图片到本地
def detect_image(self, image_path=None, output_path=None, input_size=416, show=False, score_threshold=0.3, iou_threshold=0.45, rectangle_colors=''): if image_path is not None: original_image = cv2.imread(image_path) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) image_data = image_preprocess(np.copy(original_image), [input_size, input_size]) image_data = tf.expand_dims(image_data, 0) # it gives output in three different scale pred_bbox = self.tiny_YoloV3.predict(image_data) print(pred_bbox[0].shape) print(pred_bbox[1].shape) pred_bbox = [ tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox ] pred_bbox = tf.concat(pred_bbox, axis=0) # print(pred_bbox) bboxes = postprocess_boxes(pred_bbox, original_image, input_size, score_threshold) print(bboxes.shape) bboxes = nms(bboxes, iou_threshold, method='nms') print(bboxes[0].shape) print(len(bboxes)) image = draw_bbox(original_image, bboxes, CLASSES=self.CLASSES, rectangle_colors=rectangle_colors) # print(image.shape) if output_path is not None: 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() return image
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = True # model_path = 'efficientdet-d1.h5' # model_path = r"checkpoints\2020-06-23\csv_50_0.0431_0.9146.h5" model_path = r"checkpoints\2020-06-23\1\csv_44_0.0448_1.0142.h5" # class_path = 'coco_90.json' class_path = r"train_car_object\classes.json" image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = {value['id'] - 1: value['name'] for value in json.load(open(class_path, 'r')).values()} num_classes = 4 score_threshold = 0.5 colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) # for image_path in glob.glob('datasets/VOC2007/JPEGImages/*.jpg'): for image_path in glob.glob(r"E:\DATA\@car\car_photo\carplate_test\*.jpg"): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def get_yolov4(image): # inintial config ANCHORS = utils.get_anchors('./data/yolov4_anchors.txt', False) NUM_CLASS = len(utils.read_class_names('data/coco.names')) XYSCALE = [1.2, 1.1, 1.05] STRIDES = np.array([8, 16, 32]) input_size = 608 original_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = image_preporcess(np.copy(original_image), [input_size, input_size]) image_data = image_data[np.newaxis, ...].astype(np.float32) # send grpc request request.inputs['input_1'].CopyFrom( tf.make_tensor_proto(image_data, dtype=types_pb2.DT_FLOAT)) result_final = [] result_future = stub.Predict(request, 10) result_1 = result_future.outputs['tf_op_layer_concat_10'] result_final.append(np.reshape(np.array(result_1.ListFields()[2][1]), \ (1, 76, 76, 3, 6) )) result_2 = result_future.outputs['tf_op_layer_concat_11'] result_final.append(np.reshape(np.array(result_2.ListFields()[2][1]), \ (1, 38, 38, 3, 6) )) result_3 = result_future.outputs['tf_op_layer_concat_12'] result_final.append(np.reshape(np.array(result_3.ListFields()[2][1]), \ (1, 19, 19, 3, 6) )) pred_bbox = utils.postprocess_bbbox(result_final, ANCHORS, STRIDES, XYSCALE) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.45) bboxes = utils.nms(bboxes, 0.213, method='nms') #image = utils.draw_bbox(original_image, bboxes) #image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB) #cv2.imwrite('result.jpg', image) return bboxes
def predict(original_image): ''' :param original_image: [H, W, 3] :return: (xmin, ymin, xmax, ymax, score, class) ''' image = utils.image_preporcess(np.copy(original_image), [cfg.TEST.INPUT_SIZE, cfg.TEST.INPUT_SIZE]) image = image[np.newaxis, ...] image_ = np.transpose(image, [0, 3, 1, 2]) image_ = np.copy(image_, order='C') pred = test_job(image_, anchors[0], anchors[1])[0, ...] original_image_size = original_image.shape[0:2] bboxes = utils.postprocess_boxes(pred, original_image_size, cfg.TEST.INPUT_SIZE, cfg.TEST.SCORE_THRESHOLD) bboxes = utils.nms(bboxes, cfg.TEST.IOU_THRESHOLD, method='nms') return bboxes
def main(): K.set_learning_phase(0) config = tf.ConfigProto() config.gpu_options.allow_growth = True K.set_session(tf.Session(config=config)) os.environ['CUDA_VISIBLE_DEVICES'] = '0' model_path = '../train/models/ep25-loss0.1936-val_loss0.9421.h5' image_size = 896 classes = {0: 'sand'} properties = {0: 'yes', 1: 'no', 2: 'unrecognized'} num_classes = 1 num_properties = 3 score_threshold = 0.5 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] model, prediction_model = efficientdet(9, 1, 3, 64, 3, 3, 0.3, 0.5) prediction_model.load_weights(model_path, by_name=True) anchor_params = AnchorParameters() anchors = anchors_for_shape((image_size, image_size), pyramid_levels=[3, 4, 5, 6, 7], anchor_params=anchor_params) option = 'image' # image | video if option == 'image': root = '/Users/yanyan/data' save_path = os.path.join(root, 'neg') if not os.path.exists(save_path): os.makedirs(save_path) with open('../tools/data/langzhong.txt', 'r') as f: img_infos = f.readlines() for img_info in img_infos: img_name = img_info.strip('\n') img_path = os.path.join(root, img_name) image = cv2.imread(img_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() # # [boxes, scores, labels, pro_id] boxes, scores, labels, pro_id = prediction_model.predict_on_batch([ np.expand_dims(image, axis=0), np.expand_dims(anchors, axis=0) ]) boxes, scores, labels, pro_id = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels), np.squeeze(pro_id) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] pro_id = pro_id[indices] if boxes.shape[0] != 0: src_path = os.path.join(save_path, 'src_img') plot_path = os.path.join(save_path, 'plot_img') if not os.path.exists(src_path): os.makedirs(src_path) if not os.path.exists(plot_path): os.makedirs(plot_path) # print(src_path + f"/{''.join(img_name.split('/')[-1]).split('.')[0]}.jpg", src_image) # exit() # cv2.imwrite(src_path + f"/{''.join(img_name.split('/')[-1]).split('.')[0]}.jpg", src_image) draw_boxes(src_image, boxes, scores, labels, pro_id, colors, classes, properties) cv2.imwrite( plot_path + f"/{''.join(img_name.split('/')[-1]).split('.')[0]}.jpg", src_image) # cv2.namedWindow('image', cv2.WINDOW_NORMAL) # cv2.imshow('image', src_image) # cv2.waitKey(0) elif option == 'video': root = '/Users/yanyan/Desktop/test_video' save_path = os.path.join(os.path.expanduser('~/data'), 'crop_img') video_names = os.listdir(root) for video_name in video_names: video_path = os.path.join(root, video_name) cap = cv2.VideoCapture(video_path) if not cap.isOpened(): print("++++++++++++++++don't play++++++++++++++++++") else: total_frame = cap.get(cv2.CAP_PROP_FRAME_COUNT) print( f"++++++++++++++++total frame{total_frame}+++++++++++++++") frame_index = 0 while frame_index < total_frame: cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index) ret, image = cap.read() if ret: src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() # # [boxes, scores, labels, pro_id] boxes, scores, labels, pro_id = prediction_model.predict_on_batch( [ np.expand_dims(image, axis=0), np.expand_dims(anchors, axis=0) ]) boxes, scores, labels, pro_id = np.squeeze( boxes), np.squeeze(scores), np.squeeze( labels), np.squeeze(pro_id) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] pro_id = pro_id[indices] if boxes.shape[0] != 0: src_path = os.path.join(save_path, 'src_img') plot_path = os.path.join(save_path, 'plot_img') if not os.path.exists(src_path): os.makedirs(src_path) if not os.path.exists(plot_path): os.makedirs(plot_path) cv2.imwrite( src_path + f'/{video_name}_{frame_index}.jpg', src_image) draw_boxes(src_image, boxes, scores, labels, pro_id, colors, classes, properties) cv2.imwrite( plot_path + f'/{video_name}_{frame_index}.jpg', src_image) # cv2.namedWindow('image', cv2.WINDOW_NORMAL) # cv2.imshow('image', src_image) # cv2.waitKey(25) frame_index += 1 cap.release()
def main(opt): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = opt.phi weighted_bifpn = False model_path = opt.model image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # Dhaka-ai classes dhaka_ai_classes = get_class_names(opt.class_names) dhaka_ai_num_classes = len(dhaka_ai_classes) score_threshold = opt.conf_thres _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=dhaka_ai_num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) with open('submission_files/arafat_efficientdet-result_conf-{}_IOUthr-{}_{}_ac-0.0.csv'.format(opt.conf_thres, opt.iou_thres, time.strftime("%Y-%m-%d_%H-%M-%S")), mode='w') as result_file: fieldnames = ['image_id', 'class', 'score', 'xmin', 'ymin', 'xmax', 'ymax', 'width', 'height'] result_file_writer = csv.writer(result_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) result_file_writer.writerow(fieldnames) for image_path in glob.glob(os.path.join(opt.source_dir, "*.jpg")): image = cv2.imread(image_path) assert image is not None, "Image cat not be read, path: "+image_path # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) selected_indices = tf.image.non_max_suppression( boxes, scores, 120, iou_threshold=opt.iou_thres, score_threshold=opt.conf_thres) selected_boxes = tf.gather(boxes, selected_indices) selected_labels = tf.gather(labels, selected_indices) selected_boxes = tf.Session().run(selected_boxes) selected_labels = tf.Session().run(selected_labels) for b, l, s in zip(selected_boxes, selected_labels, scores): class_id = int(l) class_name = dhaka_ai_classes[class_id] xmin, ymin, xmax, ymax = list(map(int, b)) if xmax > w: xmax = w if xmin < 0: xmin = 0 if ymax > h: ymax = h if ymin < 0: ymin = 0 score = '{:.6f}'.format(s) check_badbox(image_path, h, w, xmin, ymin, xmax, ymax) result_file_writer.writerow([os.path.basename(image_path), class_name, score, xmin, ymin, xmax, ymax, image_size, image_size])
frame_size = frame.shape[:2] image_data = utils.image_preprocess(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.4) bboxes = utils.nms(bboxes, 0.3, method='nms') image = utils.draw_bbox(frame, bboxes) out.write(image) result = np.asarray(image) success, frame = vid.read() num_frame += 1 print("number of frame: ", num_frame) vid.release() out.release() print("end of program")
def detect_video(self, video_path, output_path=None, input_size=416, show=False, score_threshold=0.3, iou_threshold=0.45, rectangle_colors=''): times = [] vid = cv2.VideoCapture(video_path) # by default VideoCapture returns float instead of int width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(vid.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*'XVID') # output_path must be .mp4 out = cv2.VideoWriter(output_path, codec, fps, (width, height)) while True: _, img = vid.read() try: original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) except: break image_data = image_preprocess(np.copy(original_image), [input_size, input_size]) image_data = tf.expand_dims(image_data, 0) t1 = time.time() pred_bbox = self.tiny_YoloV3.predict(image_data) t2 = time.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 = postprocess_boxes(pred_bbox, original_image, input_size, score_threshold) bboxes = nms(bboxes, iou_threshold, method='nms') times.append(t2 - t1) times = times[-20:] ms = sum(times) / len(times) * 1000 fps = 1000 / ms print("Time: {:.2f}ms, {:.1f} FPS".format(ms, fps)) image = draw_bbox(original_image, bboxes, CLASSES=self.CLASSES, rectangle_colors=rectangle_colors) image = cv2.putText(image, "Time: {:.1f}FPS".format(fps), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) if output_path is not None: out.write(image) if show: cv2.imshow('output', image) if cv2.waitKey(25) & 0xFF == ord("q"): cv2.destroyAllWindows() break cv2.destroyAllWindows()
def capture(app, mail): save_path = "RealTimeTest/" GPU = '0' Model_path = 'efficientdet-d0.h5' phi = 0 json_dict = {} json_dict["boundingbox"] = [] os.environ['CUDA_VISIBLE_DEVICES'] = GPU weighted_bifpn = True model_path = Model_path image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = {value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values()} num_classes = 90 score_threshold = 0.3 colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) try: os.makedirs(save_path) except: print(save_path, "is aleardy exist") pass start = time.time() mon = {'top': 297, 'left': 224, 'width': 590, 'height': 450} sct = mss() image_num = 0 before_object_num = 0 while True: with mss() as sct: sct.get_pixels(mon) image = np.array(Image.frombuffer('RGB', (sct.width, sct.height), sct.image)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # # TODO: np_img 에다가 prediction 추가할 것. src_image = image.copy() h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] # file_name = image_path.split('/')[-1] # cv2.imwrite('prediction.png', src_image) temp_dict = {} temp_dict["box"] = boxes[indices].tolist() temp_dict["label"] = labels[indices].tolist() temp_dict["label_name"] = [classes[int(i)] for i in labels] # temp_dict["image_name"] = file_name json_dict["boundingbox"].append(copy.deepcopy(temp_dict)) draw_boxes(src_image, boxes, scores, labels, colors, classes) np_img = np.array(src_image) encode_return_code, image_buffer = cv2.imencode('.jpg', np_img) io_buf = io.BytesIO(image_buffer) yield (b'--frame\r\n' b'Content-Type: image/png\r\n\r\n' + io_buf.read() + b'\r\n')
def eveluate_testset(self, ckp_path: str = "checkpoints/"): os.environ["CUDA_VISIBLE_DEVICES"] = "0" phi = 1 weighted_bifpn_flag = True model_path = ckp_path + self.ckp_model_dir + "/" + self.ckp_model_file image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # my dataset classes classes = { value["id"] - 1: value["name"] for value in json.load(open("mine_classes_eveluation_ds.json", "r")).values() } num_classes = 13 score_threshold = 0.50 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet( phi=phi, num_classes=num_classes, weighted_bifpn=weighted_bifpn_flag, score_threshold=score_threshold, ) model.load_weights(model_path, by_name=True) counter = 0 for image_path in glob.glob(self.base_path + "testset_images/*.jpg"): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = ( np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels), ) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) # cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imwrite( "evaluated_images/evaluated_image_" + str(counter) + ".jpg", src_image) counter += 1 # cv2.imshow('image', src_image) # cv2.waitKey(0) if counter == 500: break
def main(): parser = argparse.ArgumentParser() model_path = parser.add_argument('--model_path', required=True) GPU = parser.add_argument('--GPU', default='0') image_path = parser.add_argument('--image_path', required=True) save_path = parser.add_argument('--save_path', required=True) json_path = parser.add_argument('--json_path', default='default_path_json.json') image_size = parser.add_argument('--image_size') file_format = parser.add_argument('--file_format', default='jpeg') score_threshold = parser.add_argument('--score_threshold', default=0.3) phi = parser.add_argument('--phi', default=0, type=int) args = parser.parse_args() json_dict = {} json_dict["boundingbox"] = [] os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU print(args.image_path + '/*.' + args.file_format) file_lists = glob.glob(args.image_path + '/*.' + args.file_format) length = len(file_lists) iteration = 0 print(length) print(args) weighted_bifpn = True model_path = args.model_path image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[args.phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=args.phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) print(model_path) try: os.makedirs(args.save_path) except: print(args.save_path, "is aleardy exist") pass start = time.time() for image_path in tqdm(file_lists): # iteration += 1 # if iteration % 20 == 19: # break image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] file_name = image_path.split('/')[-1] temp_dict = {} temp_dict["box"] = boxes[indices].tolist() temp_dict["label"] = labels[indices].tolist() temp_dict["label_name"] = [classes[int(i)] for i in labels] temp_dict["image_name"] = file_name json_dict["boundingbox"].append(copy.deepcopy(temp_dict)) draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.imwrite(args.save_path + '/' + file_name, src_image) # cv2.waitKey(0) print("time : ", time.time() - start) with open(args.json_path, 'w') as json_data: json.dump(json_dict, json_data, indent=4)
def predict(self, img, classes, threshold, output_path): t1 = time.time() input_size = 608 num_classes = len(classes) original_image = cv2.imread(img) 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]) images_data = [] images_data.append(image_data) images_data_np = np.array(images_data).astype(np.float32) tensor = tf.contrib.util.make_tensor_proto( images_data_np, shape=list(images_data_np.shape)) # 单张图片加载 # image_data = [image_data.tolist()] t2 = time.time() # print('图片预处理时间:{}'.format(t2-t1)) url = self.server options = [('grpc.max_send_message_length', 512 * 1024 * 1024), ('grpc.max_receive_message_length', 512 * 1024 * 1024)] channel = grpc.insecure_channel(url, options=options) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) r = predict_pb2.PredictRequest() r.model_spec.name = self.model r.model_spec.signature_name = 'predict' r.inputs['input'].CopyFrom(tensor) # res = stub.Predict(r, 10.0) result_future = stub.Predict.future(r, 10.0) # 10 secs timeout res = result_future.result() t3 = time.time() # print('GRPC 预测时间:{}'.format(t3-t2)) arr = tf.make_ndarray(res.outputs['output']) # print("arr", arr) # print("output shape", arr.shape) pred_bbox = np.reshape(arr, (-1, 5 + num_classes)) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, threshold) t4 = time.time() # print(']图片 postprocess_boxes 处理时间:{}'.format(t4-t3)) bboxes = utils.nms(bboxes, 0.45, method='nms') t5 = time.time() # print('图片 nms 处理时间:{}'.format( t5-t4)) image = utils.draw_bbox(original_image, bboxes, classes, True) t6 = time.time() # # print("图片draw_bbox处理时间:", t6-t5) # image_np = cv2.cvtColor( image, cv2.COLOR_RGB2BGR ) # 转换一下通道 # cv2.imwrite(filepath, image_np) t7 = time.time() # print('图片写出处理时间:{}'.format( t7-t6)) image = Image.fromarray(image) image.show() image.save(output_path) # 保存图片到本地
image_path = "./576527.jpg" num_classes = 2 input_size = 416 graph = tf.Graph() 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_preprocess(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}) print("pred_bbox: ", pred_sbbox) print("pred_bbox shape: ", np.array(pred_sbbox).shape) 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.1) bboxes = utils.nms(bboxes, 0.45, method='nms') image = utils.draw_bbox(original_image, bboxes) image = Image.fromarray(image) image.show()
def AlgorithmRun(cameraID, timeStamps, img): global Yolov3Inference # Yolov3Inference = Yolov3RemoteInference() # init return values ret = False result = [] boxes = [] save_input = False save_output = False try: original_image_list = [] original_image_size_list = [] original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) original_image_list.append(original_image) original_image_size_list.append(original_image.shape[:2]) # preprocess image_data = utils.preprocess(np.copy(original_image), [Yolov3Inference.h, Yolov3Inference.w]) image_data = image_data.astype(np.float32) # batch images inference avaiable input_batch = [] input_batch.append(image_data) batch_size = len(input_batch) if batch_size != 1: raise ("Do not support batch size {}, expect 1".format(batch_size)) results = Yolov3Inference.run(input_batch, batch_size) for indx in range(batch_size): pred_sbbox = results["pred_sbbox"][indx] # print("pred_sbbox shape:", pred_sbbox.shape) pred_mbbox = results["pred_mbbox"][indx] # print("pred_mbbox shape:", pred_mbbox.shape) pred_lbbox = results["pred_lbbox"][indx] # print("pred_lbbox shape:", pred_lbbox.shape) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + Yolov3Inference.num_classes)), np.reshape(pred_mbbox, (-1, 5 + Yolov3Inference.num_classes)), np.reshape(pred_lbbox, (-1, 5 + Yolov3Inference.num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size_list[indx], 608, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') if len(bboxes) > 0: image, bboxes_json = utils.draw_bbox(original_image_list[indx], bboxes) pic_size = list(image.shape) result = [1] boxes = bboxes_json else: image = img pic_size = list(image.shape) detection_result = { "cameraID": cameraID, "timeStamps": timeStamps, "picSize": pic_size, "boxes": boxes, "result": result, "res1": "", "res2": "" } ret = True except Exception as e: # 捕获错误信息 error_info = traceback.format_exc() logger.error(error_info) image = img pic_size = list(image.shape) # 生成返回的json信息 detection_result = { "cameraID": cameraID, "timeStamps": timeStamps, "picSize": pic_size, "boxes": boxes, "result": result, "res1": "", "res2": "" } detectResult = json.dumps(detection_result) return ret, result, detectResult, save_input, save_output, image
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '-1' phi = 0 weighted_bifpn = True model_path = 'efficientdet-d0.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.5 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] # _, model = efficientdet(phi=phi, # weighted_bifpn=weighted_bifpn, # num_classes=num_classes, # score_threshold=score_threshold) # model.load_weights(model_path, by_name=True) model = load_model('savedmodel/') video_path = 'test/video.mp4' cap = cv2.VideoCapture(video_path) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(cap.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('./out.mp4', codec, fps, (width, height)) times = [] while True: ret, image = cap.read() if not ret: break src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) end = time.time() print(end - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) #calculating fps times.append(end - start) times = times[-20:] ms = sum(times) / len(times) * 1000 fps = 1000 / ms # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) src_image = cv2.putText(src_image, "Time: {:.1f}FPS".format(fps), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) out.write(src_image)
image = image[:, :, ::-1] # if the frame dimensions are empty, grab them if w is None or h is None: (h, w) = image.shape[:2] #h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network #start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze( labels) #print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] ## select only people indices indices1 = np.where(labels[indices] == 0)[0] # select those detections #boxes = boxes[indices1] #labels = labels[indices1] #scores = scores[indices1] # create output file #count = 0 if count > 4500: #2
def main(args=None): if len(sys.argv) is 3: model_path = str(sys.argv[1]) image_data = os.path.join(str(sys.argv[2]), "*.jpg") else: print( "Pass model path and image data path in respectively as command line argument" ) exit() os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 4 weighted_bifpn = False model_path = model_path image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes coco_classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } coco_num_classes = 90 # Dhaka-ai classes dhaka_ai_classes = { 0: 'ambulance', 1: 'auto rickshaw', 2: 'bicycle', 3: 'bus', 4: 'car', 5: 'garbagevan', 6: 'human hauler', 7: 'minibus', 8: 'minivan', 9: 'motorbike', 10: 'pickup', 11: 'army vehicle', 12: 'policecar', 13: 'rickshaw', 14: 'scooter', 15: 'suv', 16: 'taxi', 17: 'three wheelers (CNG)', 18: 'truck', 19: 'van', 20: 'wheelbarrow' } dhaka_ai_num_classes = 21 score_threshold = 0.05 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(dhaka_ai_num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=dhaka_ai_num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) for image_path in glob.glob(image_data): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold # indices = np.where(scores[:] > score_threshold)[0] # select those detections # boxes = boxes[indices] # labels = labels[indices] selected_indices = tf.image.non_max_suppression(boxes, scores, 80, iou_threshold=0.25, score_threshold=0.30) selected_boxes = tf.gather(boxes, selected_indices) selected_labels = tf.gather(labels, selected_indices) selected_boxes = tf.Session().run(selected_boxes) selected_labels = tf.Session().run(selected_labels) # boxes = boxes[selected_indices] # labels = labels[selected_indices] draw_boxes(src_image, selected_boxes, scores, selected_labels, colors, dhaka_ai_classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = False model_path = 'checkpoints/flir_27_0.1779.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 3 score_threshold = 0.8 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] path = r'G:\datasets\iray_infrad_384\JPEGImages' path = r'G:\datasets\iray\200m_404man' imgs = [os.path.join(path, x) for x in os.listdir(path)] for file in imgs: if file.find("Raw") > -1: img = np.fromfile(file, np.uint16).reshape((512, 640)) image = cv2.normalize(img, dst=None, alpha=0, beta=65535, norm_type=cv2.NORM_MINMAX) image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) elif file.split('.')[-1] in ['jpg', 'jpeg', 'bmp', 'png']: image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) else: continue src_image = image.copy() image = image.astype(np.float32) image[..., 0] *= std[0] image[..., 1] *= std[1] image[..., 2] *= std[2] image[..., 0] += mean[0] image[..., 1] += mean[1] image[..., 2] += mean[2] image *= 255. # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)