Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to set webcam codec in OpenCV 3.1.0 Python?

I have a problem with my webcams using the uncompressed YUYV codec in OpenCV Python which makes it so that I have horrible frame rates. I would like to set the codec to MJPG, but I can't seem to figure out how. Here is what I have tried(along with variations of it)

import cv2 #Opencv 3.1.0
import matplotlib.pyplot as plt
import numpy as np
vid = cv2.VideoCapture(0)
vid.set(6, cv2.CV_FOURCC('M','J', 'P', 'G'))#Should set the codec, but it doesn't

while True:
    _, img = vid.read()

    cv2.imshow('image', img)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

vid.release()
cv2.destroyAllWindows()

When I run it, I get cv2 has no attribute to CV_FOURCC. I have been using this as a reference, but it doesn't really explain what I am supposed to pass in after I input the 6 in the vid.set(). I have the cv2.CV_FOURCC, but I just guessed that is what it wanted. Any help would be greatly appreciated!