【问题标题】:numpy.ndarray has no attribute read (when trying to pass a video)numpy.ndarray 没有读取属性(尝试传递视频时)
【发布时间】:2021-07-03 20:01:17
【问题描述】:

我正在尝试为文件编写带有注释的视频(或者至少在使用 google colab 时将其打印到我们的屏幕上)。我试过使用 cv_imshow 但这一次打印一帧视频,这不是我想要的。我已将脚本修改为使用 VideoWriter,但在使用 cap.read() 时仍然卡住,因为我收到一条错误消息,指出 numpy.ndarray has no attribute read。

我理解为什么会发生此错误,因为我相信 .read() 函数正在等待视频,而我正在尝试传递一个 numpy 数组。但是,我似乎无法找到另一种解决方法。任何帮助将不胜感激。

这是我正在使用的完整代码:

import cv2
import tensorflow as tf
from google.colab.patches import cv2_imshow

cap = cv2.VideoCapture(r'/content/drive/MyDrive/vid1.mp4')
from google.colab.patches import cv2_imshow
import numpy as np


while True:
    ret, image_np = cap.read()

    image_np_expanded = np.expand_dims(image_np, axis=0)

    input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
    detections, predictions_dict, shapes = detect_fn(input_tensor)

    label_id_offset = 1
    image_np_with_detections = image_np.copy()

    viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'][0].numpy(),
          (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
          detections['detection_scores'][0
                                         ].numpy(),
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=200,

          min_score_thresh=.30,
          agnostic_mode=False)

    cap=image_np_with_detections

    
  
    res=(800,600)              # this format fail to play in Chrome/Win10/Colab
              # fourcc = cv2.VideoWriter_fourcc(*'MP4V') #codec
    fourcc = cv2.VideoWriter_fourcc(*'H264') #codec
    out = cv2.VideoWriter('output.mp4', fourcc, 20.0, res)

    while(True):
    # Capture frame-by-frame
      ret, frame = cap.read()

      print("Frame number: " + str(counter))
      counter = counter+1
      if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    out.write(frame)

   

 out.release() 

 

cap.release()
cv2.destroyAllWindows()

提前致谢!

【问题讨论】:

  • 仔细追踪cap的值。是的 read 仅适用于打开的文件。很明显,如果cap 已经是一个数组,则不需要“读取”它。我看到cap 设置在循环之前,但也在循环内部。不要尝试“随机”修复;确保您了解错误以及导致问题的变量。只有这样你才能做出正确的修复。
  • 你好 hpaulj。谢谢您的意见。我理解您的评论并同意 cap 不应包含在循环中,因为它已经指的是从循环之前提供的路径中读取的文件。我在循环中包含 cap.read() 的(不正确的)尝试是读取不同的帧,以便 out.write(frame) 可以使用它们。请问您是否知道如何为此目的调整代码?
  • read 文档谈论抓取下一帧。那么为什么要更改cap 呢? cap=image_np_with_detections 有什么意义?

标签: python numpy object-detection video-processing object-detection-api


【解决方案1】:

我设法调整代码以使 VideoWriter 正常工作。正如 hpaulj 指出的那样,我分配了两次变量 cap。

正确的代码如下:

cap = cv2.VideoCapture(r'/content/drive/MyDrive/Workspace/Images/Test/vid3.mp4')
res=(800,600)            
fourcc = cv2.VideoWriter_fourcc(*'H264') #codec
out = cv2.VideoWriter('/content/drive/MyDrive/Workspace/Images/Test/vid3output.mp4', fourcc, 20.0, res)

  while True:
    ret, image_np = cap.read()
    ##expand dimensions as the model expects images to have the shape :: [1,None, None,3]
    image_np_expanded = np.expand_dims(image_np, axis=0)

    input_tensor = tf.convert_to_tensor(image_np_expanded, dtype=tf.float32)
    detections, predictions_dict, shapes = detect_fn(input_tensor)

    label_id_offset = 1
    image_np_with_detections = image_np.copy()

    viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'][0].numpy(),
          (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
          detections['detection_scores'][0].numpy(),
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=200,
          min_score_thresh=.5,
          agnostic_mode=False)

    out.write(cv2.resize(image_np_with_detections,(800,600)))

 
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2020-02-21
    • 2020-12-08
    • 2019-12-10
    • 2017-07-10
    • 2020-05-10
    • 2019-11-04
    • 2017-05-24
    相关资源
    最近更新 更多