博客
关于我
Python cv2 摄像头获取与视频保存
阅读量:216 次
发布时间:2019-02-28

本文共 958 字,大约阅读时间需要 3 分钟。

import cv2cap = cv2.VideoCapture(0)fourcc = cv2.VideoWriter_fourcc(*'XVID')# 获取视频帧率fps = cap.get(cv2.CAP_PROP_FPS)# 获取视频分辨率size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))# 创建视频输出文件out = cv2.VideoWriter('camera_test.avi', fourcc, 10.0, size)while True:    ret, frame = cap.read()    # 水平翻转图像    frame = cv2.flip(frame, 1)    out.write(frame)    # 在图像上显示提示信息    cv2.putText(frame, 'Press Q to save and quit', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)    cv2.imshow('frame', frame)    if cv2.waitKey(1) & 0xFF == ord('q'):        breakcap.release()out.release()cv2.destroyAllWindows()

注意:如果生成的视频无法正常打开,建议使用自动获取视频分辨率的方式,即`size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))`,而不是手动设置视频尺寸。同时,视频编码格式`fourcc`可以通过以下方式进行测试和优化:```pythonvideo_FourCC = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')

请确保所有必要的权限和设置已经正确配置,以避免视频生成和播放过程中出现问题。如果有任何错误或异常,建议及时检查视频编码格式、分辨率设置以及存储权限等。

```

转载地址:http://wrpi.baihongyu.com/

你可能感兴趣的文章
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>