Exemplo n.º 1
0
def demo(model, config, anchors):
    if torch.cuda.is_available():
        model.cuda()
    class_names = load_class_names(config.namesfile)
    fh = open(config.val_label)
    print("load val_label: {}".format(config.val_label))
    i = 0
    image_paths = get_first_10_imgs(config)
    print("Got images: ")
    print(image_paths)
    for i, image_path in enumerate(image_paths):
        img = cv2.imread(image_path)
        sized = cv2.resize(img, (config.width, config.height))
        sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)
        start = time.time()
        boxes = do_detect(model, sized, 0.4, 0.6, torch.cuda.is_available())
        finish = time.time()
        print('{}: Predicted in {:.04f} seconds.'.format(
            image_path, (finish - start)))

        plot_boxes_cv2(img,
                       boxes[0],
                       savename=os.path.join("demo_img",
                                             "predictions_{}.jpg".format(i)),
                       class_names=class_names)
Exemplo n.º 2
0
def detect(path, log, display):
    iou = 0.5
    confidence = 0.5
    class_names, n_classes = load_class_names()
    model = YOLOv3(n_classes=n_classes,
                   iou_threshold=iou,
                   confidence_threshold=confidence)
    inputs = tf.placeholder(tf.float32, [1, *model.input_size, 3])
    detections = model(inputs)
    saver = tf.train.Saver(tf.global_variables(scope=model.scope))

    with tf.Session() as sess:
        saver.restore(sess, './weights/model.ckpt')
        if display:
            cv2.namedWindow('Detection')
        video = cv2.VideoCapture(path)
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        fps = video.get(cv2.CAP_PROP_FPS)
        frame_size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)),
                      int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        basename = os.path.basename(path)
        outfile_name = f'./results/detection_for_{basename}.mp4'
        out = cv2.VideoWriter(outfile_name, fourcc, fps, frame_size)
        print(f'Video will be saved at {outfile_name}')
        print('Press q to quit')
        while True:
            tm = datetime.now()
            retval, frame = video.read()
            if not retval:
                break
            resized_frame = cv2.resize(frame,
                                       dsize=tuple(
                                           (x)
                                           for x in model.input_size[::-1]),
                                       interpolation=cv2.INTER_NEAREST)
            result = sess.run(detections, feed_dict={inputs: [resized_frame]})
            draw_boxes_frame(frame, frame_size, result, class_names,
                             model.input_size)

            if display:
                cv2.imshow('Detections', frame)

            key = cv2.waitKey(1) & 0xFF
            if key == ord('q'):
                break
            out.write(frame)
            if log:
                print(f'Frame processed in {datetime.now() - tm} sec')
        cv2.destroyAllWindows()
        video.release()

    print('Done')
def get_class_name(cat):
    class_names = load_class_names("./data/coco.names")
    if cat >= 1 and cat <= 11:
        cat = cat - 1
    elif cat >= 13 and cat <= 25:
        cat = cat - 2
    elif cat >= 27 and cat <= 28:
        cat = cat - 3
    elif cat >= 31 and cat <= 44:
        cat = cat - 5
    elif cat >= 46 and cat <= 65:
        cat = cat - 6
    elif cat == 67:
        cat = cat - 7
    elif cat == 70:
        cat = cat - 9
    elif cat >= 72 and cat <= 82:
        cat = cat - 10
    elif cat >= 84 and cat <= 90:
        cat = cat - 11
    return class_names[cat]
Exemplo n.º 4
0
path_config = cfg.SERVICE.DETECT_CONFIG
confidences_threshold = cfg.SERVICE.THRESHOLD
num_of_class = cfg.SERVICE.NUMBER_CLASS

detectron = config_detectron()
detectron.MODEL.DEVICE = cfg.SERVICE.DEVICE
detectron.merge_from_file(path_config)
detectron.MODEL.WEIGHTS = path_weigth

detectron.MODEL.ROI_HEADS.SCORE_THRESH_TEST = confidences_threshold
detectron.MODEL.ROI_HEADS.NUM_CLASSES = num_of_class

PREDICTOR = DefaultPredictor(detectron)

# create labels
CLASSES = load_class_names(cfg.SERVICE.CLASSES)

# init app
app = Flask(__name__)


@app.route('/predict', methods=['POST'])
def predict_image():
    if request.method == 'POST':
        file = request.files['file']
        image_file = file.read()
        image = cv2.imdecode(np.frombuffer(image_file, dtype=np.uint8), -1)

        height, width, channels = image.shape
        center_image = (width//2, height//2)
        print("shape image: ", (width, height))
def demo_tensorflow(tfpb_file="./weight/yolov4.pb",
                    image_path=None,
                    print_sensor_name=False):
    graph_name = 'yolov4'
    tf.compat.v1.disable_eager_execution()
    with tf.compat.v1.Session() as persisted_sess:
        print("loading graph...")
        with gfile.FastGFile(tfpb_file, 'rb') as f:
            graph_def = tf.compat.v1.GraphDef()
            graph_def.ParseFromString(f.read())

        persisted_sess.graph.as_default()
        tf.import_graph_def(graph_def, name=graph_name)

        # print all sensor_name
        if print_sensor_name:
            tensor_name_list = [
                tensor.name for tensor in
                tf.compat.v1.get_default_graph().as_graph_def().node
            ]
            for tensor_name in tensor_name_list:
                print(tensor_name)

        inp = persisted_sess.graph.get_tensor_by_name(graph_name + '/' +
                                                      'input:0')
        print(inp.shape)
        out1 = persisted_sess.graph.get_tensor_by_name(graph_name + '/' +
                                                       'output_1:0')
        out2 = persisted_sess.graph.get_tensor_by_name(graph_name + '/' +
                                                       'output_2:0')
        out3 = persisted_sess.graph.get_tensor_by_name(graph_name + '/' +
                                                       'output_3:0')
        print(out1.shape, out2.shape, out3.shape)

        # image_src = np.random.rand(1, 3, 608, 608).astype(np.float32)  # input image
        # Input
        image_src = cv2.imread(image_path)
        resized = cv2.resize(image_src, (inp.shape[2], inp.shape[3]),
                             interpolation=cv2.INTER_LINEAR)
        img_in = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
        img_in = np.transpose(img_in, (2, 0, 1)).astype(np.float32)
        img_in = np.expand_dims(img_in, axis=0)
        img_in /= 255.0
        print("Shape of the network input: ", img_in.shape)

        feed_dict = {inp: img_in}

        outputs = persisted_sess.run([out1, out2, out3], feed_dict)
        print(outputs[0].shape)
        print(outputs[1].shape)
        print(outputs[2].shape)

        boxes = post_processing(img_in, 0.4, outputs)

        num_classes = 80
        if num_classes == 20:
            namesfile = 'data/voc.names'
        elif num_classes == 80:
            namesfile = 'data/coco.names'
        else:
            namesfile = 'data/names'

        class_names = load_class_names(namesfile)
        result = plot_boxes_cv2(image_src,
                                boxes,
                                savename=None,
                                class_names=class_names)
        cv2.imshow("tensorflow predicted", result)
        cv2.waitKey()
Exemplo n.º 6
0
    if use_cuda:
        model.cuda()

    img = cv2.imread(imgfile)

    # Inference input size is 416*416 does not mean training size is the same
    # Training size could be 608*608 or even other sizes
    # Optional inference sizes:
    #   Hight in {320, 416, 512, 608, ... 320 + 96 * n}
    #   Width in {320, 416, 512, 608, ... 320 + 96 * m}
    sized = cv2.resize(img, (width, height))
    sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)

    from utils.utils import load_class_names, plot_boxes_cv2
    from tool.torch_utils import do_detect

    for i in range(2):  # This 'for' loop is for speed check
        # Because the first iteration is usually longer
        boxes = do_detect(model, sized, 0.4, 0.6, use_cuda)

    if namesfile == None:
        if n_classes == 20:
            namesfile = 'data/voc.names'
        elif n_classes == 80:
            namesfile = 'data/coco.names'
        else:
            print("please give namefile")

    class_names = load_class_names(namesfile)
    plot_boxes_cv2(img, boxes[0], 'predictions.jpg', class_names)
Exemplo n.º 7
0
    use_json = cfg.CONFIG.USE_JSON 
    train_json_path = cfg.CONFIG.TRAIN_JSON_PATH 
    val_json_path = cfg.CONFIG.VAL_JSON_PATH 

    use_xml = cfg.CONFIG.USE_XML 
    root_dir = cfg.CONFIG.ROOT_DIR 
    train_dir = cfg.CONFIG.TRAIN_DIR 
    label_train_dir = cfg.CONFIG.LABEL_TRAIN_DIR 
    image_train_dir = cfg.CONFIG.IMAGE_TRAIN_DIR
    val_dir = cfg.CONFIG.VAL_DIR 
    label_val_dir = cfg.CONFIG.LABEL_VAL_DIR 
    image_val_dir = cfg.CONFIG.IMAGE_VAL_DIR 
    json_val_name = cfg.CONFIG.JSON_VAL_NAME 
    json_train_name = cfg.CONFIG.JSON_TRAIN_NAME 

    list_classes = load_class_names(cfg.CONFIG.LABELS_PATH)

    use_augment = cfg.CONFIG.USE_AUGMENT 
    size = cfg.CONFIG.SIZE 
    flip_prob = cfg.CONFIG.FLIP_PROB 
    min_brightness = cfg.CONFIG.MIN_BRIGHTNESS 
    max_brightness = cfg.CONFIG.MAX_BRIGHTNESS 
    min_contrast = cfg.CONFIG.MIN_CONTRAST 
    max_contrast = cfg.CONFIG.MAX_CONTRAST 
    min_saturation = cfg.CONFIG.MIN_SATURATION 
    max_saturation = cfg.CONFIG.MAX_SATURATION 

    config_model_path = cfg.CONFIG.CONFIG_MODEL_PATH 
    num_workers = cfg.CONFIG.NUM_WORKERS
    weights_path = cfg.CONFIG.WEIGHTS_PATH 
    image_per_batch = cfg.CONFIG.IMAGE_PER_BATCH 
Exemplo n.º 8
0
            list_boxes.append([x, y, w, h])
            list_scores.append(score)
            list_classes.append(class_id)

    return list_boxes, list_scores, list_classes


if __name__ == '__main__':
    cfg = get_config()
    cfg.merge_from_file('configs/detect.yaml')

    path_weigth = cfg.DETECTOR.DETECT_WEIGHT
    path_config = cfg.DETECTOR.DETECT_CONFIG
    confidences_threshold = cfg.DETECTOR.THRESHOLD
    num_of_class = cfg.DETECTOR.NUMBER_CLASS

    detectron = config_detectron()
    detectron.MODEL.DEVICE = cfg.DETECTOR.DEVICE
    detectron.merge_from_file(path_config)
    detectron.MODEL.WEIGHTS = path_weigth

    detectron.MODEL.ROI_HEADS.SCORE_THRESH_TEST = confidences_threshold
    detectron.MODEL.ROI_HEADS.NUM_CLASSES = num_of_class

    PREDICTOR = DefaultPredictor(detectron)
    CLASSES = load_class_names(cfg.DETECTOR.VEHICLE_CLASS)

    image = cv2.imread("demo.jpg")
    list_boxes, list_scores, list_classes = predict(image, PREDICTOR, CLASSES)
    print("list_classes: ", list_classes)