OSX下的OpenCVVideoWriter不产生输出

2024-03-29 04:37:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从OSX下的OpenCV的python包装器中创建一个视频。我使用的是python 2.7.1、opencv2.3.1a和willowgarage中的python包装器,它们是opencv版本附带的。我有:

import cv,cv2
w = cv2.VideoWriter('foo.avi', cv.FOURCC('M','J','P','G'), 25, (100,100))
for i in range(100):
    w.write(np.ones((100,100,3), np.uint8))

OpenCV说

WARNING: Could not create empty movie file container.
Didn't successfully update movie file.
... [ 100 repetitions]

我不知道下一步该怎么做


Tags: import版本视频foonpmoviecv2附带
3条回答

在尝试了各种选项后,我发现我正在使用的frame.size与VideoWriter中指定的大小不符:因此将其设置为iMac 1280x720的默认值可以工作!

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv2.VideoWriter()
succes = out.open('output.mp4v',fourcc, 15.0, (1280,720),True)
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)
        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

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

我在macOS High Sierra 10.13.4、Python 3.6.5、OpenCV3.4.1上。

下面的代码(源代码:https://www.learnopencv.com/read-write-and-display-a-video-using-opencv-cpp-python/)打开相机,按“q”键成功关闭窗口,并以.avi格式保存视频。

请注意,您需要将其作为.py文件运行。如果在Jupyter Notebook中运行,则窗口在关闭时挂起,需要强制quit Python关闭窗口。

import cv2
import numpy as np

# Create a VideoCapture object
cap = cv2.VideoCapture(0)

# Check if camera opened successfully
if (cap.isOpened() == False): 
  print("Unable to read camera feed")

# Default resolutions of the frame are obtained.The default resolutions are system dependent.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))

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

  if ret == True: 

    # Write the frame into the file 'output.avi'
    out.write(frame)

    # Display the resulting frame    
    cv2.imshow('frame',frame)

    # Press Q on keyboard to stop recording
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

  # Break the loop
  else:
    break 

# When everything done, release the video capture and video write objects
cap.release()
out.release()

# Closes all the frames
cv2.destroyAllWindows() 

关于这个话题有很多过时的和不正确的在线指南——我想我几乎每一个都试过了。在查看了Mac OSX上基于源QTKit的VideoWriter实现之后,我终于能够使用以下代码让VideoWriter输出有效的视频文件:

fps = 15
capSize = (1028,720) # this is the size of my source video
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case
self.vout = cv2.VideoWriter()
success = self.vout.open('output.mov',fourcc,fps,capSize,True) 

要编写图像帧(请注意,imgFrame的大小必须与上面的capSize相同,否则更新将失败):

self.vout.write(imgFrame) 

完成后,请确保:

vout.release() 
self.vout = None

这在macosx10.8.5(山狮)上对我有效:对其他平台没有保证。我希望这个片段能帮别人省下几个小时的实验时间!

相关问题 更多 >