简体   繁体   English

使用 OpenCV/Python 无法更改网络摄像头的编解码器

[英]Changing codec of webcam is not possible using OpenCV/Python

I am using windows 10 and python 3.7 / OpenCV4 and a Logitech C922 webcam.我正在使用 windows 10 和 python 3.7 / OpenCV4 和罗技 C922 网络摄像头。 While the cam seems to provide 30 fps using the windows camera app, i can not get more than 5-6 fps using OpenCV.虽然使用 windows 相机应用程序似乎可以提供 30 fps,但使用 OpenCV 时我无法获得超过 5-6 fps。 Resolution is set to FHD.分辨率设置为全高清。

cam = cv2.VideoCapture(cv2.CAP_DSHOW+0)
while(1):
    ret,frame = cam.read()
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

In another post I found the solution to change the codec to MJPG.在另一篇文章中,我找到了将编解码器更改为 MJPG 的解决方案。 However, the camera does not accept changing the codec.但是,相机不接受更改编解码器。 I tried:我试过了:

cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('m','j','p','g'))
cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
cam.set(cv2.CAP_PROP_FOURCC, float(cv2.VideoWriter_fourcc('m','j','p','g'))
cam.set(cv2.CAP_PROP_FOURCC, float(cv2.VideoWriter_fourcc('M','J','P','G'))
cam.set(cv2.CAP_PROP_FOURCC, 1196444237.0)

The camera always returns "844715353.0"相机总是返回“844715353.0”

How can I achieve higher fps?我怎样才能获得更高的fps?

i work with Logitech c920 webcam and i test WebcamVideoStream class in imutils module and this module use threading and the queue data structure for improve the FPS when processing video streams with OpenCV is to move the I/O (ie, the reading of frames from the camera sensor) to a separate thread, and you can set your custom resolution on this file webcamvideostream.py .我使用 Logitech c920 网络摄像头并在imutils模块中测试WebcamVideoStream class 并且该模块使用线程和队列数据结构来提高 FPS 使用 OpenCV 处理视频流时移动 I/O(即从相机传感器)到一个单独的线程,你可以在这个文件webcamvideostream.py上设置你的自定义分辨率。

you can use threading to obtain higher FPS.您可以使用线程来获得更高的 FPS。 maybe this solution help you.也许这个解决方案可以帮助你。

plz see these links:请看这些链接:

https://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/ https://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/

https://www.pyimagesearch.com/2017/02/06/faster-video-file-fps-with-cv2-videocapture-and-opencv/ https://www.pyimagesearch.com/2017/02/06/faster-video-file-fps-with-cv2-videocapture-and-opencv/

https://github.com/jrosebr1/imutils/blob/master/imutils/video/webcamvideostream.py https://github.com/jrosebr1/imutils/blob/master/imutils/video/webcamvideostream.py

It seems that order matters.似乎顺序很重要。 As I understand, OpenCv uses ffmpeg in the background.据我了解,OpenCv 在后台使用 ffmpeg。 In ffmpeg the command should be something like this:在 ffmpeg 中,命令应该是这样的:

ffmpeg -f dshow -framerate 60 -video_size 1280x720 -input_format mjpeg -i video="my webcam" out.mkv

So that your OpenCV code should be something like this所以你的 OpenCV 代码应该是这样的

my_cam_index = 0 
cap = cv2.VideoCapture(my_cam_index, cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FPS, 60.0) 
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,720) 
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M','J','P','G'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM