OpenCV将框架写入文件python [英] OpenCV write frame to file python
问题描述
嘿,即时通讯开始使用OpenCV,我无法将网络摄像头输出保存到文件中.这就是我所拥有的.这样可以正常运行,启动网络摄像头并创建"output.avi".问题是output.avi很小(414字节),每次我运行程序时都具有相同的确切字节. 我猜问题出在fourcc编码上,但是我还无法找到适合我的情况.我正在Mac OS X上运行.如果您需要更多信息,请告诉我.
Hey so im starting to play around with OpenCV and I cant get my webcam output saved to a file. Here is what I have. This runs fine, launches the webcam and creates "output.avi" The issue is output.avi is tiny(414 bytes) and the same exact bytes each time I run the program. Im guessing the issue is with the fourcc encoding but I havent been able to find what works in my case. I am running on Mac OS X. Let me know if you need anymore information.
import numpy as np
import cv2
path = ('/full/path/Directory/output.avi')
cap = cv2.VideoCapture(0)
cap.set(1, 20.0) #Match fps
cap.set(3,640) #Match width
cap.set(4,480) #Match height
fourcc = cv2.cv.CV_FOURCC(*'XVID')
video_writer = cv2.VideoWriter(path,fourcc, 20.0, (640,480))
while(cap.isOpened()):
#read the frame
ret, frame = cap.read()
if ret==True:
#show the frame
cv2.imshow('frame',frame)
#Write the frame
video_writer.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
video_writer.release()
cv2.destroyAllWindows()
推荐答案
只需更改
fourcc = cv2.cv.CV_FOURCC(*'XVID')
到
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
在这里找到答案: 在OSX下使用opencv VideoWriter不会产生任何输出
这篇关于OpenCV将框架写入文件python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!