def detect(model_name, weight_path, input_size, image_path): assert model_name in ['yolov3_tiny'] NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES)) STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, tiny=True) 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, ...].astype(np.float32) input_layer = tf.keras.layers.Input([input_size, input_size, 3]) if model_name == 'yolov3_tiny': feature_maps = YOLOv3_tiny(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = ops.decode(fm, NUM_CLASS) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) else: raise ValueError if weight_path: if model_name == 'yolov3_tiny': weight = np.load(weight_path, allow_pickle=True) model.set_weights(weight) else: raise ValueError print('Restoring weights from: %s ' % weight_path) # model.summary() start_time = time.time() pred_bbox = model.predict(image_data) print(time.time() - start_time) pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.5) bboxes = utils.nms(bboxes, 0.3, method='nms') image = visualize.draw_bbox(original_image, bboxes, classes=utils.read_class_names( cfg.YOLO.CLASSES)) image = Image.fromarray(image) image.show()
def detect_tflite(model_name, weight_path, input_size, image_path): assert model_name in ['yolov3_tiny'] STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, True) 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, ...].astype(np.float32) # Load TFLite model and allocate tensors. interpreter = tf.lite.Interpreter(model_path=weight_path) interpreter.allocate_tensors() # Get input and output 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'], image_data) start_time = time.time() interpreter.invoke() pred_bbox = [ interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details)) ] print(time.time() - start_time) pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.5) bboxes = utils.nms(bboxes, 0.3, method='nms') image = visualize.draw_bbox(original_image, bboxes, classes=utils.read_class_names( cfg.YOLO.CLASSES)) image = Image.fromarray(image) image.show()
def detect(image_path, weight_path, input_size): STRIDES = np.array(cfg.YOLO.STRIDES) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS, tiny=False) 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, ...].astype(np.float32) NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES)) input_layer = tf.keras.layers.Input([input_size, input_size, 3]) feature_maps = YOLOv3(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = ops.decode(fm, NUM_CLASS) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) if weight_path: weight = np.load(weight_path, allow_pickle=True) model.set_weights(weight) print('Restoring weights from: %s ' % weight_path) pred_bbox = model.predict(image_data) pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.25) bboxes = utils.nms(bboxes, 0.2, method='nms') image = visualize.draw_bbox(original_image, bboxes, classes=utils.read_class_names( cfg.YOLO.CLASSES)) image = Image.fromarray(image) image.show()
def detect(model_name, weight_path, input_size, image_path, framework): assert model_name in ['yolov3_tiny', 'yolov3', 'yolov4'] if model_name == 'yolov3_tiny': STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, True) elif model_name == 'yolov3': STRIDES = np.array(cfg.YOLO.STRIDES) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_V3, False) elif model_name == 'yolov4': STRIDES = np.array(cfg.YOLO.STRIDES) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS, False) else: raise ValueError NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES)) XYSCALE = cfg.YOLO.XYSCALE 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, ...].astype(np.float32) if framework == 'tf': input_layer = tf.keras.layers.Input([input_size, input_size, 3]) if model_name == 'yolov3_tiny': feature_maps = YOLOv3_tiny(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = ops.decode(fm, NUM_CLASS) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) elif model_name == 'yolov3': feature_maps = YOLOv3(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = ops.decode(fm, NUM_CLASS) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) elif model_name == 'yolov4': feature_maps = YOLOv4(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = ops.decode(fm, NUM_CLASS) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) else: model = None raise ValueError if weight_path.split(".")[-1] == "weights": if model_name == 'yolov3_tiny': utils.load_weights_tiny(model, weight_path) # utils.extract_weights_tiny(model, weight_path) print('load yolo tiny 3') elif model_name == 'yolov3': utils.load_weights_v3(model, weight_path) print('load yolo 3') elif model_name == 'yolov4': utils.load_weights(model, weight_path) print('load yolo 4') else: raise ValueError elif weight_path.split(".")[-1] == "npy": if model_name == 'yolov3_tiny': # utils.load_weights_tiny_npy(model, weight_path) print('load yolo tiny 3 npy') else: model.load_weights(weight_path) print('Restoring weights from: %s ' % weight_path) # weight = np.load('D:\\coursera\\YoLoSerirs\\checkpoint\\yolo3_tiny.npy', allow_pickle=True) # model.set_weights(weight) # model.summary() start_time = time.time() pred_bbox = model.predict(image_data) print(time.time() - start_time) else: # Load TFLite model and allocate tensors. interpreter = tf.lite.Interpreter(model_path=weight_path) interpreter.allocate_tensors() # Get input and output 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'], image_data) start_time = time.time() interpreter.invoke() pred_bbox = [ interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details)) ] print(time.time() - start_time) if model_name == 'yolov4': pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE) else: pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.5) bboxes = utils.nms(bboxes, 0.3, method='nms') image = visualize.draw_bbox(original_image, bboxes) image = Image.fromarray(image) image.show()
import numpy as np from Utils import visualize import matplotlib.pyplot as plt import os data_dir = 'D:\\DataSet\\coco_val2017\\val2017' with open('D:\\coursera\\YoLoSerirs\\data\\val2017.txt', 'r') as f: data = f.readlines() inds = np.random.randint(0, len(data), 20) for ind in inds: img_data = data[ind].split(' ') path = img_data[0] image_path = os.path.join(data_dir, path.split('/')[-1]) if os.path.exists(image_path) == False: raise ValueError bboxes = [] for line in img_data[1:]: x1, y1, x2, y2, label = line.split(',') bboxes.append([int(x1), int(y1), int(x2), int(y2), 1.0, int(label)]) labels = label bboxes = np.array(bboxes) original_image = cv2.imread(image_path) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) img = visualize.draw_bbox(original_image, bboxes) plt.imshow(img) plt.show()
def video_fps(model_name, weight_path, input_size, framework): assert model_name in ['yolov3_tiny'] STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, True) XYSCALE = [1.0, 1.0, 1.0] classes = utils.read_class_names(cfg.YOLO.CLASSES) if framework == 'tf': model = load_model(model_name, weight_path, input_size) else: model, input_details, output_details = load_model_lite(weight_path) video_path = 'D:\\coursera\\YoLoSerirs\\dataset\\test.mp4' vcapture = cv2.VideoCapture(video_path) width = int(vcapture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vcapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = vcapture.get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') start = time.time() count = 0 success = True while success: success, image = vcapture.read() if success: original_image = image 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, ...].astype(np.float32) if framework == 'tf': pred_bbox = model.predict(image_data) else: model.set_tensor(input_details[0]['index'], image_data) model.invoke() pred_bbox = [ model.get_tensor(output_details[i]['index']) for i in range(len(output_details)) ] pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.6) bboxes = utils.nms(bboxes, 0.2, method='nms') image = visualize.draw_bbox(original_image, bboxes, classes=classes) image = image[:, :, [2, 1, 0]] cv2.imshow('cap video', image) # plt.imshow(image) # plt.show() if cv2.waitKey(40) & 0xFF == ord('q'): break count += 1 print("FPS of the video is {:5.2f}".format( (time.time() - start) / count))
def video_fps(model_name, weight_path, input_size, framework): assert model_name in ['yolov3_tiny', 'yolov3', 'yolov4'] if model_name == 'yolov3_tiny': STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, True) elif model_name == 'yolov3': STRIDES = np.array(cfg.YOLO.STRIDES) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_V3, False) elif model_name == 'yolov4': STRIDES = np.array(cfg.YOLO.STRIDES) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS, False) else: raise ValueError if model_name == 'yolov4': XYSCALE = cfg.YOLO.XYSCALE else: XYSCALE = [1.0, 1.0, 1.0] model = load_model(model_name, weight_path, input_size, framework) video_path = 'D:\\coursera\\YoLoSerirs\\dataset\\test.mp4' vcapture = cv2.VideoCapture(video_path) width = int(vcapture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vcapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = vcapture.get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') start = time.time() count = 0 success = True while success: success, image = vcapture.read() if success: original_image = image 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, ...].astype(np.float32) pred_bbox = model.predict(image_data) pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.5) bboxes = utils.nms(bboxes, 0.3, method='nms') image = visualize.draw_bbox(original_image, bboxes) image = image[:, :, [2, 1, 0]] cv2.imshow('cap video', image) # plt.imshow(image) # plt.show() if cv2.waitKey(40) & 0xFF == ord('q'): break count += 1 print("FPS of the video is {:5.2f}".format( (time.time() - start) / count))