numpy.ndarray 没有读取属性(尝试传递视频时)

numpy.ndarray has no attribute read (when trying to pass a video)

我正在尝试将带有注释的视频写入文件(或者至少在使用 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()

提前致谢!

为了让 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()