在视频上运行TensorFlow对象检测API

时间:2018-06-25 07:20:45

标签: python object-detection

我正在尝试运行TensorFlow对象检测API。当我在网络摄像头上使用该行时可以使用它:

cap = cv2.VideoCapture(0)

但是当我尝试在带有该行的视频中进行操作时:

cap = cv2.VideoCapture('kev.mp4')

我最终遇到了这个错误:

Traceback (most recent call last):
  File "C:\Users\leahj\Documents\models-master\research\object_detection\object_detection_tutorial_cam.py", line 147, in <module>
    feed_dict={image_tensor: image_np_expanded})
  File "C:\Users\leahj\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "C:\Users\leahj\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1104, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "C:\Users\leahj\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

视频和代码都在此文件夹中:

  

C:\ Users \ leahj \ Documents \ models-master \ research \ object_detection

有什么想法吗?

5 个答案:

答案 0 :(得分:0)

ComplexObject

opencv无法读取帧。因此,变量“ cap”为“无”。您可以尝试检查是否可以打印任何框架。我认为是关于cv2.VideoCapture函数的。

答案 1 :(得分:0)

您是否检查了流是否正确打开。您可以使用下面的行来尝试。

cap.isOpened()

如果上述方法返回true,则表示读取文件没有问题。然后,您需要在下面的行中设置要读取的帧。

cap.set(cv2.CAP_PROP_POS_MSEC,frameNumber)

然后,您可以按如下方式读取该特定框架:

成功,图片= cap.read()

希望有帮助!

答案 2 :(得分:0)

当尝试运行“使用网络摄像头检测对象”教程时,我遇到了相同的问题。但我猜想是由视频的最后一帧+1引起的。 (类似于视频的最后一帧,然后在接下来获取cv2时,它将失去视频,例如空帧。) 我尝试过,这是真的。

# cut out the last frame to avoid error type.
if not ret:
   break

如何知道视频何时完成here

将其放在哪里?像这样的东西:

        # running
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        # cut out the last frame to avoid error type.
        if not ret:
            break
        # Actual detection.
        (boxes, scores, classes, num_detections) = sess.run(
            [boxes, scores, classes, num_detections],
            feed_dict={image_tensor: image_np_expanded})
        # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8)
        # write to output
        out.write(image_np)
        # Display output
        cv2.imshow('object detection', image_np)

答案 3 :(得分:0)

我以这种方式解决了这个问题,希望对任何人都可以帮助

cap = cv2.VideoCapture(video_path_test)
    with od_graph.as_default():
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        with tf.Session(config=config, graph=od_graph) as sess:
            read_success = True
            while read_success:
                read_success, image_np = cap.read()
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # More code here... #
                if not read_success:
                   break
                (boxes, scores, classes, num_detections) = sess.run(
                        [boxes, scores, classes, num_detections],
                        feed_dict={image_tensor: image_np_expanded})

答案 4 :(得分:0)

我遇到了同样的问题,我通过输入绝对位置解决了

从此;

cap = cv2.VideoCapture('kev.mp4')

到这里;

cap = cv2.VideoCapture('/media/*username*/*some directories*/kev.mp4')

这对我有用。