MultiNet icon indicating copy to clipboard operation
MultiNet copied to clipboard

How to run the code on a video file

Open anjanakumar opened this issue 7 years ago • 2 comments

Hi, Is it possible to use a video file as input for demo.py??

anjanakumar avatar Jul 10 '17 10:07 anjanakumar

@anjanakumar Yes, you can see the following example method I provided. The key point is applying opencv VideoCapture to read a video and VideoWriter to save your output result. Modified the code in demo.py. One thing to mention is FLAGS.data_file is the video file path and FLAGS.save_video is a bool value to determine whether save the output video.

def run_my_eval(load_out, output_folder, data_file):
    meta_hypes, subhypes, submodules, decoded_logits, sess, image_pl = load_out
    seg_softmax = decoded_logits['segmentation']['softmax']
    pred_boxes_new = decoded_logits['detection']['pred_boxes_new']
    pred_confidences = decoded_logits['detection']['pred_confidences']
    eval_list = [seg_softmax, pred_boxes_new, pred_confidences]

    def my_preprocess(image):
        # define your own method
        pass

    def my_postprocess(shape, image):
        # define your own method
        pass

    assert os.path.isfile(data_file), \
    'file {} does not exist'.format(data_file)

    camera = cv2.VideoCapture(data_file)

    assert camera.isOpened(), \
    'Cannot capture source'

    _, frame = camera.read()
    height, width, _ = frame.shape

    if FLAGS.save_video:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        fps = round(camera.get(cv2.CAP_PROP_FPS))
        video_writer = cv2.VideoWriter('video.avi', fourcc, fps, (width, height))

    test_constant_input(subhypes)
    test_segmentation_input(subhypes)

    import utils.train_utils as dec_utils

    # buffers for demo in batch
    buffer_pre = list()

    elapsed = int()
    start = timer()    

    logging.info('Start video prediction.')
    while camera.isOpened():
        elapsed += 1
        _, frame = camera.read()
        if frame is None:
            logging.info('End of Video')
            break

        # resize input frame
        preprocessed = my_preprocess(frame)

        # run one frame predict
        feed_dict = {image_pl: preprocessed}
        output = sess.run(eval_list, feed_dict=feed_dict)

        seg_softmax, pred_boxes_new, pred_confidences = output

        # Create Segmentation Overlay
        shape = preprocessed.shape
        seg_softmax = seg_softmax[:, 1].reshape(shape[0], shape[1])
        hard = seg_softmax > 0.5
        overlay_image = utils.fast_overlay(preprocessed, hard)

        # Draw Detection Boxes
        new_img, rects = dec_utils.add_rectangles(
            subhypes['detection'], [overlay_image], pred_confidences,
            pred_boxes_new, show_removed=False,
            use_stitching=True, rnn_len=subhypes['detection']['rnn_len'],
            min_conf=0.50, tau=subhypes['detection']['tau'])

        postprocessed = my_postprocess(new_img)

        # save one frame in ouput video
        video_writer.write(postprocessed)

        if elapsed % 50 == 0:
            logging.info('Speed (fps): {0:3.3f} FPS'.format(
                elapsed / (timer() - start)))

    logging.info('Video prediction complete. Cost {0:3.3f} sec.'.format(
        timer() - start))
    logging.info('Save output video file to video.avi')
    if FLAGS.save_video:
        video_writer.release()
    camera.release()

lujian9328 avatar Aug 15 '17 06:08 lujian9328

@lujian9328 你好,请问你视频检测成功了吗,这两个函数具体写什么呢【my_preprocess(image),my_postprocess(shape, image)】,看到请回复,谢谢

screamdw avatar Apr 10 '20 14:04 screamdw