【问题标题】:How to write video then download using cv2 in Google colab?如何在 Google colab 中使用 cv2 编写视频然后下载?
【发布时间】:2022-10-20 07:29:30
【问题描述】:

我正在尝试对视频进行一些图像处理,然后在 colab 上使用 opencv 保存生成的视频。但是,我无法访问我正在写入的生成的视频文件。

import cv2
from google.colab.patches import cv2_imshow
import numpy as np

fourcc = cv2.VideoWriter_fourcc(*'H264')
cap = cv2.VideoCapture(vid_file)
out = cv2.VideoWriter('output.mp4',fourcc,30.0,(1124,1080))
cnt = 0
ret = True
while(ret):

  ret,frame = cap.read()
  print(cnt,end=' ')

  # check if prey was tracked on this frame
  match = np.where(prey_frames==cnt)[0]
  if match:
    prey_frame = match[0]
    # print(prey_frame)
    image = cv2.circle(frame,(int(prey_px[prey_frame].x),95+int(prey_px[prey_frame].y)),
                       radius=5,color=(255,0,255),thickness=2)
  else:
    image = frame

  out.write(image)
  cnt += 1

out.release()
cap.release()
cv2.destroyAllWindows()

据我了解,这应该写入一个名为“output.mp4”的文件。此代码运行没有错误,但当前目录中没有文件,并且没有该名称的文件可供下载(使用 files.download('output.mp4') 返回“找不到文件”错误)。

任何帮助,将不胜感激!

【问题讨论】:

    标签: opencv google-colaboratory


    【解决方案1】:

    我遇到过几次这个问题,我认为这与 Colab 的操作环境仅支持少数视频编码这一事实有关。 我能够让视频作者使用以下内容:

    fourcc = cv2.VideoWriter_fourcc('F','M','P','4')

    【讨论】:

      猜你喜欢
      • 2020-10-18
      • 2020-08-27
      • 1970-01-01
      • 2020-02-15
      • 2019-12-14
      • 2022-07-03
      • 1970-01-01
      • 2020-08-17
      • 2020-07-07
      相关资源
      最近更新 更多