如何在Python中使用OpenCV存储网络摄像头视频 [英] How to store webcam video with OpenCV in Python

查看:195
本文介绍了如何在Python中使用OpenCV存储网络摄像头视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,可以读取网络摄像头并将其显示在窗口中.我现在要存储结果,因此请遵循本教程我编写了以下代码:

I've got a script in Python which reads out my webcam and shows it in a window. I now want to store the results, so following this tutorial I wrote the following code:

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"

这可以在新窗口中完美显示我的摄像头的实时视频素材.但是写入视频文件似乎失败.它确实创建了一个名为output.avi的文件,但是该文件为空(零字节),并且在命令行上我看到以下错误:

This perfectly shows the real time video footage from my webcam in a new window. But writing the video file seems to fail. It does create a file called output.avi, but the file is empty (zero bytes) and on the command line I see the following errors:

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),所以我不确定为什么它不匹配.

I clearly resize the frame to the size in which I want to save the video (640x480) so I'm not sure why it wouldn't match.

当我再次运行脚本(因此在这种情况下,空的output.avi已经存在)时,它显示以下错误:

When I run the script again (so in this case the empty output.avi already exists), it shows these errors:

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.但不幸的是,他们都没有为我工作.除了MJPG以外,它们都给出相同的错误,这给了我以下错误:

In the tutorial it says that the Four digit FourCC code is used to specify the video codec which is platform dependent and that the list of available codes can be found in fourcc.org. I'm on OSX so I tried a bunch of different codec-codes: DIVX, XVID, MJPG, X264, WMV1, WMV2. But unfortunately none of them work for me. They all give the same errors, except for MJPG, which gives me the following error:

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

有人知道这里可能出什么问题吗?欢迎所有提示!

Does anybody know what could be wrong here? All tips are welcome!

推荐答案

可能是因为您使用AVFoundation构建了OpenCV,但它不支持XVID或其他编解码器.您可以尝试mp4vm4v扩展名.

It's probably because you built OpenCV with AVFoundation and it doesn't support XVID or other codec. You can try mp4v and m4v extension.

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已经可以使用另一个库了.

You can try cv2.resize as I used in my code. There's no need to use another library when cv2 can do that already.

这篇关于如何在Python中使用OpenCV存储网络摄像头视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆