Ask Your Question

Revision history [back]

How to read video from camera using opencv python

Hi guys,

I want to perform some simple video processing on camera feed using opencv(3.1) python(3.5) programming. The camera I am using is Thorlabs DCC1545M (monochrome). The camera is able to show the live feed through its software uc480 viewer. However, when I try to read the camera through python, using the following standard code (given below), it fails, as the 'ret' remains 'FALSE'. I also tried changing the argument in the "cap = cv2.VideoCapture(0)" from '0' to '1' and other numbers, but that did not help either.

I guess it should be straightforward, but I am doing something obviously wrong. Any help would be much appreciated!

Thanks, Ankur

Code:

import numpy as np import cv2

cap = cv2.VideoCapture(0)

while(True): # Capture frame-by-frame ret, frame = cap.read()

# Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break

cap.release() cv2.destroyAllWindows()

click to hide/show revision 2
No.2 Revision

updated 2016-11-21 03:59:47 -0600

berak gravatar image

How to read video from camera using opencv python

Hi guys,

I want to perform some simple video processing on camera feed using opencv(3.1) python(3.5) programming. The camera I am using is Thorlabs DCC1545M (monochrome). The camera is able to show the live feed through its software uc480 viewer. However, when I try to read the camera through python, using the following standard code (given below), it fails, as the 'ret' remains 'FALSE'. I also tried changing the argument in the "cap = cv2.VideoCapture(0)" from '0' to '1' and other numbers, but that did not help either.

I guess it should be straightforward, but I am doing something obviously wrong. Any help would be much appreciated!

Thanks, Ankur

Code:

import numpy as np
import cv2

cv2 cap = cv2.VideoCapture(0)

cv2.VideoCapture(0) while(True): # Capture frame-by-frame ret, frame = cap.read()

cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break

break cap.release() cv2.destroyAllWindows()

cv2.destroyAllWindows()