I seem to constantly be getting a Camera Index out of Range issue, and I was wondering if VideoCapture is not set to work with Qubes?
Hey, welcome to the Qubes OS forum!
I just tried on a disposable VM running Fedora-42, and I was able to use cv2 without any trouble.
However, I think that I was able to reproduce your issue. Did you attach your camera to the AppVM prior running the code?
In case you are not aware, for security reasons, Qubes OS does not let your AppVMs see/access your devices automatically. Thus, you must first “attach” the devices to the desired AppVM.
The GUI way
To do so, click on the “Qubes Devices” icon on the right of your taskbar – it looks like a usb symbol. You should see your camera. Click on it, and then select your AppVM under the label “Attach to qube”. It should work!
When you are done, you can detach your camera the same way.
The Command-Line Way
You could achieve the same thing using the command line!
- In
dom0, list all your usb devices, using
For me, my camera is linked toqvm-device usb listsys-usb:4-7. - Attach the camera to your AppVM:
(I would have replacedqvm-device usb attach <your-AppVM-name> sys-usb:<DEVID><DEVID>by4-7).
It’s done!
To detach it after:
qvm-device detach <your-AppVM-name> sys-usb:<DEVID>
Please note that attachments like that are not persistent throughout reboots. If you need such a thing, you should see the section “USB Devices” of the “Qubes OS Global Config”.
Please let me know if this doesn’t work out for you. ; )
Thanks for the insight.
I did have the camera attached to the AppVM I am using under Qubes devices.
I am also using the template workstation dvm, so I am not sure if that makes any difference?
I am using 4.3 so I can no longer attach the device to something called sys-USB or similar. I no longer see the option at least.
But even attaching the device directly to the AppVM I am using did not work.
Sorry if I misled you into understanding that
Anyway, you did the right thing: attaching the camera to the AppVM.
Then, could you share a few more details with us? Which template you are using, etc. Maybe you could try to open your camera with a regular application, like cheese? Does it work?
I just installed python3-opencv on a whonix-workstation-17-dvm, and it worked seamlessly. Maybe it’s an issue with your camera/drivers.
4.3 has whonix-workstation-18, and that newer version has some extra hardening applied, which might cause issues with camera access. Can you try on plain Debian or Fedora app qube?
I am quite new to qubes, so I am not sure if debian-13/fedora-42 xfce is what you are referring to?
That qube does not have access to the internet so it might not be something I could try on.
Do you know what an AppVM/app qube is?
An important security feature of Qubes OS relies on separating AppVMs and TemplateVMs:
- A TemplateVM is an offline qube, that is meant not to run any application, but whose purpose is to only install software. For instance, I could have a browsing template, with Firefox installed, for instance. Now,
fedora-42-xfceanddebian-13-xfceare templates, oftenly installed during the Qubes OS installation. See How to install software — Qubes OS Documentation. - An AppVM is a qube which, on startup, will inherit the root folder of the template it is based on, and sort of “merge” it with its (persistent) home folder. This is a very good thing, because it means that, every time you spin up an app qube, it will be in a rather “clean” state, i. e. any changes made to its root folder will not persist.
Thus, Marmarek was asking you to test your program on an AppVM based on either debian-13-xfce or fedora-42-xfce, in order to see if the issue you encountered is specific to Whonix dvm, or if it is due to your hardware/smth else.
If you do not know how to create an AppVM based on a template, see Getting started — Qubes OS Documentation.
The package python3-opencv should not require internet to work, so you should not encounter any issue with that.
I had just tried testing with guvcview and it seems to work. It is just CV2 that is having an issue. And Debian works too.
I’m a bit confused sorry, should we understand that you managed to get cv2 working on Debian? Or the other way? ![]()
It is working on an AppVM based on debian and the camera works on guvcview but not CV2 on the whonix based VM.
Again, I got it working on whonix-workstation-18-dvm.
I launched a root shell:
qvm-run -u root dispXXXX qterminal
In qterminal:
apt install python3-opencv
Then I created a file test.py containing a code found on OpenCV in Python: A Full Tutorial for Beginners (With Examples) - painlessprogramming.com.
cap = cv2.VideoCapture(0) # 0 for webcam
while True:
ret, frame = cap.read()
cv2.imshow('Live Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I attached my camera to dispXXXX, then run
python3 test.py
And I saw my – I have to say, splendid – face ![]()
Could you try to reproduce? Or tell me what you did differently.