如何使用 OpenCV 在 Python 中存储网络摄像头视频
How to store webcam video with OpenCV in Python
我在 Python 中有一个脚本可以读取我的网络摄像头并将其显示在 window 中。我现在想存储结果,所以在 this tutorial 之后我写了下面的代码:
import cv2
import imutils
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
try:
(grabbed, frame) = camera.read() # grab the current frame
frame = imutils.resize(frame, width=640, height=480)
cv2.imshow("Frame", frame) # show the frame to our screen
key = cv2.waitKey(1) & 0xFF # I don't really have an idea what this does, but it works..
video_writer.write(frame) # Write the video to the file system
except KeyboardInterrupt:
break
# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print "\n\nBye bye\n"
这在新的 window 中完美地显示了来自我的网络摄像头的实时视频片段。但是写入视频文件似乎失败了。它确实创建了一个名为 output.avi
的文件,但该文件为空(零字节)并且在命令行上我看到以下错误:
OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
etc.
我清楚地将帧的大小调整为我想要保存视频的大小 (640x480),所以我不确定为什么它不匹配。
当我再次 运行 脚本时(所以在这种情况下空 output.avi
已经存在),它显示这些错误:
2017-04-17 10:57:14.147 Python[86358:5848730] AVF: AVAssetWriter status: Cannot Save
2017-04-17 10:57:14.332 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.366 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.394 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
etc.
教程中说四位 FourCC 代码用于指定依赖于平台的视频编解码器,可用代码列表可以在 fourcc.org 中找到。我在 OSX 所以我尝试了一堆不同的编解码器代码:DIVX、XVID、MJPG、X264、WMV1、WMV2。但不幸的是 none 他们为我工作。他们都给出相同的错误,除了 MJPG
,这给了我以下错误:
OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
Traceback (most recent call last):
File "store_video.py", line 15, in <module>
video_writer.write(frame) # Write the video to the file system
cv2.error: /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write
有人知道这里出了什么问题吗?欢迎所有提示!
可能是因为您使用 AVFoundation 构建了 OpenCV,它不支持 XVID 或其他编解码器。您可以尝试 mp4v
和 m4v
扩展名。
import cv2
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
video_writer = cv2.VideoWriter('output.m4v', fourcc, 30.0, (640, 480))
while True:
(grabbed, frame) = camera.read() # grab the current frame
frame = cv2.resize(frame, (640,480))
cv2.imshow("Frame", frame) # show the frame to our screen
key = cv2.waitKey(33) & 0xFF # I don't really have an idea what this does, but it works..
video_writer.write(frame) # Write the video to the file system
if key==27:
break;
# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print("\n\nBye bye\n")
另一方面,错误
OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
意味着你弄乱了维度
frame = imutils.resize(frame, width=640, height=480)
您可以像我在代码中使用的那样尝试 cv2.resize
。当 cv2
已经可以做到这一点时,就不需要使用其他库了。
我在 Python 中有一个脚本可以读取我的网络摄像头并将其显示在 window 中。我现在想存储结果,所以在 this tutorial 之后我写了下面的代码:
import cv2
import imutils
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
try:
(grabbed, frame) = camera.read() # grab the current frame
frame = imutils.resize(frame, width=640, height=480)
cv2.imshow("Frame", frame) # show the frame to our screen
key = cv2.waitKey(1) & 0xFF # I don't really have an idea what this does, but it works..
video_writer.write(frame) # Write the video to the file system
except KeyboardInterrupt:
break
# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print "\n\nBye bye\n"
这在新的 window 中完美地显示了来自我的网络摄像头的实时视频片段。但是写入视频文件似乎失败了。它确实创建了一个名为 output.avi
的文件,但该文件为空(零字节)并且在命令行上我看到以下错误:
OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
etc.
我清楚地将帧的大小调整为我想要保存视频的大小 (640x480),所以我不确定为什么它不匹配。
当我再次 运行 脚本时(所以在这种情况下空 output.avi
已经存在),它显示这些错误:
2017-04-17 10:57:14.147 Python[86358:5848730] AVF: AVAssetWriter status: Cannot Save
2017-04-17 10:57:14.332 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.366 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.394 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
etc.
教程中说四位 FourCC 代码用于指定依赖于平台的视频编解码器,可用代码列表可以在 fourcc.org 中找到。我在 OSX 所以我尝试了一堆不同的编解码器代码:DIVX、XVID、MJPG、X264、WMV1、WMV2。但不幸的是 none 他们为我工作。他们都给出相同的错误,除了 MJPG
,这给了我以下错误:
OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
Traceback (most recent call last):
File "store_video.py", line 15, in <module>
video_writer.write(frame) # Write the video to the file system
cv2.error: /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write
有人知道这里出了什么问题吗?欢迎所有提示!
可能是因为您使用 AVFoundation 构建了 OpenCV,它不支持 XVID 或其他编解码器。您可以尝试 mp4v
和 m4v
扩展名。
import cv2
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
video_writer = cv2.VideoWriter('output.m4v', fourcc, 30.0, (640, 480))
while True:
(grabbed, frame) = camera.read() # grab the current frame
frame = cv2.resize(frame, (640,480))
cv2.imshow("Frame", frame) # show the frame to our screen
key = cv2.waitKey(33) & 0xFF # I don't really have an idea what this does, but it works..
video_writer.write(frame) # Write the video to the file system
if key==27:
break;
# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print("\n\nBye bye\n")
另一方面,错误
OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
意味着你弄乱了维度
frame = imutils.resize(frame, width=640, height=480)
您可以像我在代码中使用的那样尝试 cv2.resize
。当 cv2
已经可以做到这一点时,就不需要使用其他库了。