python调用摄像头录制视频_如何在Python中使用OpenCV存储网络摄像头视频

1586010002-jmsa.png

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"

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.

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.

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.

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

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!

解决方案

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")

On the other note, the 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

means that you messed up the dimension with

frame = imutils.resize(frame, width=640, height=480)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用 OpenCV 库在 Python 调用摄像头并保存视频。下面是一个简单的代码示例: ``` import cv2 # 打开摄像头 cap = cv2.VideoCapture(0) # 设置编码器和保存视频的文件名 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) while True: # 读取帧 ret, frame = cap.read() if not ret: break # 将帧写入视频文件 out.write(frame) # 显示帧 cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() out.release() cv2.destroyAllWindows() ``` 这段代码会打开第一个可用的摄像头,并以每秒 20 帧的速率保存为 `output.avi` 文件。你可以通过修改代码的参数来调整帧率、分辨率等。 ### 回答2: 使用Python调用摄像头保存视频可以通过使用OpenCV库来实现。OpenCV是一个开源计算机视觉库,它提供了许多函数和工具来处理图像和视频。 首先,我们需要安装OpenCV库。可以通过使用pip命令来安装OpenCV,如下所示: ``` pip install opencv-python ``` 接下来,我们可以编写一个Python脚本来调用摄像头并保存视频。下面是一个示例代码: ```python import cv2 # 获取相机输入 cap = cv2.VideoCapture(0) # 判断摄像头是否成功打开 if not cap.isOpened(): print("无法打开摄像头") exit() # 设置保存视频的编码格式和帧率 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) while True: # 读取视频帧 ret, frame = cap.read() if ret: # 显示当前帧 cv2.imshow('frame', frame) # 将帧写入输出视频文件 out.write(frame) # 按下'q'键停止录制 if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # 释放资源 cap.release() out.release() cv2.destroyAllWindows() ``` 以上代码,我们首先创建了一个VideoCapture对象cap来读取摄像头输入。然后我们定义了一个VideoWriter对象out来保存视频帧。在while循环,我们读取摄像头帧并显示它们,然后写入输出视频文件。按下键盘上的'q'键后,我们退出循环并释放资源。 运行该脚本后,将会保存名为output.avi的视频文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值