def representative_data_gen(input_size):
    fimage = open('D:\\coursera\\YoLoSerirs\\data\\val2017.txt').read().split()
    for input_value in range(100):
        if os.path.exists(fimage[input_value]):
            original_image = cv2.imread(fimage[input_value])
            original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
            image_data = utils.image_preprocess(np.copy(original_image),
                                                [input_size, input_size])
            img_in = image_data[np.newaxis, ...].astype(np.float32)
            yield [img_in]
        else:
            continue
Пример #2
0
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()
Пример #3
0
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()
Пример #4
0
    def parse_annotation(self, annotation):

        line = annotation.split()
        image_path = line[0]
        if not os.path.exists(image_path):
            raise KeyError("%s does not exist ... " %image_path)
        image = cv2.imread(image_path)
        bboxes = np.array([list(map(int, box.split(','))) for box in line[1:]])

        if self.data_aug:
            image, bboxes = self.random_horizontal_flip(np.copy(image), np.copy(bboxes))
            image, bboxes = self.random_crop(np.copy(image), np.copy(bboxes))
            image, bboxes = self.random_translate(np.copy(image), np.copy(bboxes))

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image, bboxes = utils.image_preprocess(np.copy(image),
                                               [self.train_input_size, self.train_input_size],
                                               np.copy(bboxes))
        return image, bboxes
Пример #5
0
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()
Пример #6
0
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()
Пример #7
0
    # next(dataset)

    import matplotlib.pyplot as plt
    from Config.config import cfg

    txt = 'D:/DataSet/rebuild/2002/08/24/big/img_490.jpg 110,24,180,132,0'
    cells = txt.split(' ')
    img_path = cells[0]
    boxes = []
    for cell in cells[1:]:
        xmin, ymin, xmax, ymax, label = cell.split(',')
        xmin, ymin, xmax, ymax, label = int(xmin), int(ymin), int(xmax), int(
            ymax), int(label)
        boxes.append([xmin, ymin, xmax, ymax])
    boxes = np.array(boxes)

    # img = cv2.imread('D:\\coursera\\YoLoSerirs_Project\\dataset\\val\\3.jpg')
    img = cv2.imread(img_path)

    dataset = General_Dataset('train', cfg=cfg)

    img, boxes = dataset.random_crop(img, boxes)
    # img, boxes = dataset.random_horizontal_flip(img, boxes)
    # img, boxes = dataset.random_translate(img, boxes)

    img_pad, bboxes = utils.image_preprocess(img,
                                             target_size=[416, 416],
                                             gt_boxes=boxes)

    plt.imshow(img_pad)
    plt.show()
Пример #8
0
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))