OpenCV 4.1.0 not changing brightness
I made a program that can change the brightness/contrast of the camera by pressing the arrow keys, while showing a live feed of what the camera sees. Since I only used one camera, I initiated it using cv2.VideoCapture(-1), and it worked fine.
However, when I tried to add a second camera to the program, I ran into an issue. The two cameras can be found at /dev/video0 and /dev/video1. When I changed the first camera to use cv2.VideoCapture(0), the program stopped working. After further testing, whenever I try to change any setting on a camera initialized with a value other than -1, the property is set to zero. If I adjust a property twice (typically brightness or contrast) before taking a picture, I get an error (libv4l2: error setting pixformat: Device or resource busy). If I try to do anything with the camera after that, it fails entirely. Using cam.set() returns false no matter what property/value I provide.
These issues only show up using cv2.VideoCapture(0) and cv2.VideoCapture(1), but cv2.VideoCapture(-1) always works. I've tried keeping only one camera plugged in and using cv2.VideoCapture(0) and it still fails, and if I don't try to set any properties I can successfully take an image from both cameras, so I don't believe bandwidth is an issue.
Edit: I simplified the code to figure out what was causing the issue. This is what I am currently using:
import cv2
cam = cv2.VideoCapture(0)
r1 = cam.set(10,55)
ret0,img0 = cam.read()
r2 = cam.set(11,60)
ret1,img1 = cam.read()
r3 = cam.set(10,70)
print(r1,r2,r3)
print(ret0,ret1)
The output was as follows:
libv4l2: error setting pixformat: Device or resource busy
(True, True, False)
(True, False)
If I only took one picture, or only changed one setting, there was not an error.
Please add your code, and tell us something on how both camera's are connected to the pc. If they are both on the same physical USB bus, that can render issues.
At first, the USBs were in the same bus. Since I wasn't sure if that was causing the issue, I actually unplugged one. Even with just one camera plugged in, the program still failed to work unless I used -1 as the index. If I wanted to constantly take pictures and never change a setting, it worked fine though.
Can you please use the correct flags instead of integer values, as described here?
Do you mean using cv2.CAP_PROP_BRIGHTNESS instead of 10, and the same for contrast? It didn't make a difference
But it tells us what you are trying to change. You cannot expect us to dig into the code to figure it out. Are you sure your camera's allow setting these values through software?
Yes. Sorry, that was not my intention, and I thought I specified what parameters I was changing. If I used cv2.VideoCapture(-1), every setting was able to be adjusted correctly. When I changed to using cv2.VideoCapture(0), the program stopped working. At that time, only one camera was plugged in and could be found at /dev/video0.
can you add between each
read()
operation a wait? I know that when grabbing frames in a loop, in C++ I always add awaitKey(1)
or awaitKey(10)
, which is in milliseconds. It can be that the second grab is happening at the same time the first is finishing and thus the driver isn't released yet. In that case, the program can crash. Grabbing frames in two seperate threads can also solve this issue I experienced, but thats a more heavy workaround.I figured out how to get it to work (answer below). For what it's worth, though, adding a wait did not work. I tried adding
waitKey(500)
between each line of code with no effect in the output. An index of -1 still worked, and an index of 0 still failed. Thank you for your time!