Turning your webcam into a simple RGB light sensor sampling at the framerate.
Install it in your local package repository with --user
otherwise
become admin and omit it:
setup.py install --user
pip3 install opencv-contrib-python
pip3 install opencv-python
import webcam2rgb
camera = webcam2rgb.Webcam2rgb()
def hasData(retval, data):
where data is an BGR tupel and retval contains any error from openCV
camera.start(callback = hasData)
the callback is then called at the framerate of the camera
This method has the following optional arguments:
cameraNumber=0
starts acquisition from camera numbercameraNumber
width = None
tries to set the width of the camera acquisitionheight = None
tries to set the height of the camera acquisitionfps = None
tries to set the frameratedirectShow = False
switches on Direct Show under Windows
camera.stop()
fs = camera.cameraFs()
Just run demo.py
. It plots the R,G,B channels in three plot windows.
Start your program from the (Anaconda-) console / terminal and never within Spyder. Here is an example for Windows
(base) D:\>
(base) D:\>cd webcam2rgb
(base) D:\webcam2rgb>python demo.py
The problem with Spyder is that it won't let your Python program terminate properly which leaves the video camera in an undefined state. If you then re-run your program it won't be able to talk to your camera. In the worst case you need to reboot your computer. Bottomline: use Spyder for editing, run the program from the console / terminal.
Under Windows, sometimes the camera initilisation does not open the camera correctly - shown by a 0Hz readout from camera.cameraFs()
. If this happens the camera needs to be reopened manually. Try using the directShow = True
parameter to use the Direct Show backend.