Changing USB Camera settings on Raspberry Pi

We’re doing vision processing using a Raspberry Pi and opencv, but instead of using the Pi camera module, we’re using an off the shelf USB camera. (Specifically, a Play-Station Eye) We didn’t have to install anything special for the camera. Just plug and play.

Everything’s working fine…except.

We’re finding that we would like to change the settings of the camera. Specifically, we want to turn off auto-exposure, but we could change other settings as well, and we want to do it in Python. For the Pi camera module, we know how to do this using the picamera library, but has anyone done the same thing with a USB camera?

(Our immediate problem is that the autoexposure messes with the results of our hsv filtering. We need a consistent exposure setting.)

Use robotpy-cscore. The cscore library has extensive support for setting USB camera properties.

ok, so tell me if i have this wrong, i would do something along the lines of:
import cv2
import cscore

cscCam = cscore.UsbCamera(name=‘cam0’, dev=0)
VideoCamera.setExposureManual(cscCam, .1)

while True:
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow(‘frame’, frame)
cv2.waitKey(1)

(CD wont let me space the code)

and that should set the exposure to .1 for cam0? or what else would i have to do?

PS: To whom it may concern, i was experimenting with buttons, didnt mean to report my own post:yikes:

We have had luck using V4L2-ctl to set parameters at boot in cronjobs

Nope.

Instead of VideoCamera.setExposureManual, UsbCamera is a subclass of VideoCamera, so you would just do csccam.setExposureManual. I suppose it’s not very obvious from the documentation, I’ll see if I can address that.

Instead of using cv2.VideoCapture, you can use the cscore cvSource to retrieve images from a UsbCamera instance.

There are several examples that show various things you can do with cscore: https://github.com/robotpy/robotpy-cscore/tree/master/examples

In case anyone is wanting to use v4l2ctrl to set the camera parameters, here is an example. We used v4l2ucp’s GUI to disable auto exposure and set a manual value, then we used v4l2ctrl -d /dev/video0 -s settings.conf. The vision program then ran v4l2ctrl -d /dev/video0 -l settings.conf every time it loaded.

Excellent. Thanks a lot. That should be able to get us moving.

can i get some help installing cscore on windows, i keep getting stuck on:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Python35-32\Include -IC:\Users\mikep\AppData\Roaming\Python\Python35\Include -Icscore_src/include -Intcore_src/wpiutil/include -IC:\Python35-32\lib\site-packages

umpy\core\include -IC:\Python35-32\include -IC:\Python35-32\include “-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE” “-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE” “-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt” “-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um” “-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\shared” “-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\um” “-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\winrt” /EHsc /Tpsrc/_cscore.cpp /Fobuild emp.win32-3.5\Release\src/_cscore.obj /EHsc /DVERSION_INFO=“2017.0.1”
_cscore.cpp
ntcore_src/wpiutil/include\llvm/Hashing.h(570): warning C4244: ‘argument’: conversion from ‘uint64_t’ to ‘std::size_t’, possible loss of data
ntcore_src/wpiutil/include\llvm/Hashing.h(582): warning C4244: ‘argument’: conversion from ‘uint64_t’ to ‘std::size_t’, possible loss of data
ntcore_src/wpiutil/include\llvm/Hashing.h(621): warning C4244: ‘argument’: conversion from ‘uint64_t’ to ‘std::size_t’, possible loss of data
src/_cscore.cpp(12): fatal error C1083: Cannot open include file: ‘opencv2/opencv.hpp’: No such file or directory
error: command ‘C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe’ failed with exit status 2

----------------------------------------

Command “C:\Python35-32\python.exe -u -c “import setuptools, tokenize;file=‘C:\Users\mikep\AppData\Local\Temp\pip-build-okmfjzkx\robotpy-cscore\setup.py’;f=getattr(tokenize, ‘open’, open)(file);code=f.read().replace(’
', ’
');f.close();exec(compile(code, file, ‘exec’))” install --record C:\Users\mikep\AppData\Local\Temp\pip-qismrs91-record\install-record.txt --single-version-externally-managed --compile” failed with error code 1 in C:\Users\mikep\AppData\Local\Temp\pip-build-okmfjzkx\robotpy-cscore\

where and what for do i need to install opencv(im guessing in visualstudio, but that has lead me to a rabbit hole)

I’ll be honest, I haven’t tried robotpy-cscore on Windows… and I don’t think there’s a ‘global’ place to install built packages like OpenCV. I’m sure there’s a way to do it, and if you figure it out let me know and I’ll stick it in the documentation.

We went down this rabbit-hole last year. This year we are simply using shades ::safety:: . Seriously, they work. Don’t even bother with your exposure settings!

No way, ill have to give it a shot! (Ive been thinking about trying to make an aperture or something, but now that you say it, that just makes sense)