How to read video from camera using opencv python

asked 2016-11-21 03:48:41 -0500

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

berak gravatar image

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


import numpy as np
import cv2

cap = cv2.VideoCapture(0)

   # Capture frame-by-frame
   ret, frame =

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

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

edit retag flag offensive close merge delete


it might not be that straightforward.

if you can't get vlc, mplayer or other software to use your camera, then it probably means, that you have to use some special sdk from them (meaning, that you'll have to switch to c++ or such)

btw, which os ?

berak gravatar imageberak ( 2016-11-21 04:04:01 -0500 )edit

Thanks for the reply Berak. The os is ms windows 10 pro. I would prefer to stick to opencv in python.

Ankur Chaurasia gravatar imageAnkur Chaurasia ( 2016-11-21 06:25:20 -0500 )edit

does it work with the builtin (win10) camera app ?

berak gravatar imageberak ( 2016-11-21 06:30:38 -0500 )edit

unfortunately I do not have a builtin camera; this computer is a desktop in my office.

Ankur Chaurasia gravatar imageAnkur Chaurasia ( 2016-11-21 06:33:12 -0500 )edit

Did you solve it? I have the exact same problem.

tang232 gravatar imagetang232 ( 2019-02-15 14:06:00 -0500 )edit

I also have the same problem, any update?

Katerina gravatar imageKaterina ( 2019-03-12 08:32:11 -0500 )edit

Did you solve it. I have the similar problem

Uday gravatar imageUday ( 2019-07-26 07:54:44 -0500 )edit