WSL - webcam USB : Can not open camera by index - python

I have followed this tutorial: https://github.com/dorssel/usbipd-win/wiki/WSL-support#usbip-client-tools
I successfully connect my USB camera with my WSL2, but can not open the camera.
$lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 046d:08cb Logitech, Inc. Mic (Notebooks Pro)
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
My camera USB is Logitech, Inc. Mic (Notebooks Pro) and my program:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
it shows
[ WARN:0#0.009] global /io/opencv/modules/videoio/src/cap_v4l.cpp (889) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
I’ve used v4l-utils to query my container’s video devices
v4l2-ctl --list-devices
Failed to open /dev/video0: No such file or directory
I have added some config in Device Driver like #NotTheDr01ds said and now my camera show up in WSL2 with command
v4l2-ctl --list-devices
UVC Camera (046d:08c9) (usb-vhci_hcd.0-1):
/dev/video0
/dev/video1
But when I tried to run my program, its showed
[ WARN:0#10.310] global /io/opencv/modules/videoio/src/cap_v4l.cpp (1000) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
The light of camera is on but can not capture anything. I have tried these command below:
lsmod
rmmod uvcvideo
modprobe uvcvideo nodrop=1 timeout=5000
but its showed error
rmmod uvcvideo
rmmod: ERROR: Module uvcvideo is builtin.
I have not find any solutions yet. Any suggestion??

I can't provide a complete answer at this point, but here's what I know so far about camera support under WSL2:
You have the first step correct by sharing the camera via USB/IP, but that's unfortunately only part of the process.
WSL2's kernel does not include any media drivers by default either, so you'll definitely need to add those to your kernel:
sudo apt install build-essential flex bison dwarves libssl-dev libelf-dev
libncurses-dev
mkdir ~/src
cd ~/src
git clone --depth=1 https://github.com/microsoft/WSL2-Linu
x-Kernel.git
make KCONFIG_CONFIG=Microsoft/config-wsl menuconfig
Enable the following options. All should be enabled as built-in, not modules. The option should have an "*" next to it rather than an "M":
Select Device Drivers -> Enable Multimedia support
Select Multimedia support -> Media Drivers: Enable Media USB Adapters
Select Media USB Adapters: Enable USB Video Class (UVC)
Save the config to "Microsoft/config-wsl.uvc" (or whatever you want to call it)
Exit menuconfig
make KCONFIG_CONFIG=Microsoft/config-wsl.uvc
(A bit fuzzy on the details here since it's been a few weeks since I did it) Copy the platform bzImage to a location on your Windows drive.
Update the .wslconfig file in your Windows user profile directory to point to the new kernel (see here for starters).
wsl --shutdown and restart to load the new kernel
Confirm it is in play with uname -a
Still working on this part, but it seems to help if you disable the device driver for the camera in Windows. Otherwise Linux and Windows will be fighting for control.
Even with all that in place, I have not been able to capture video yet. I can at least get the camera to show up in WSL2 after that as evidenced with v4l2-ctl --list-devices.
Hope that helps you at least make some progress. I'd be interested in knowing if you solve it completely.

Related

libusb can't find a specific device connected to computer

I'm trying to run a python code to control AVR ATmega32u4 and then, turn on and turn off the LEDs connected to it. For that, I got LUFA firmware, compiled the .c files and burnt it to m32u4. The VID is 03EB and PID is 204F, but I can't find it with libusb (is not listed), and so I can't run the python code because it doesn't find the usb device. How can I solve this issue?
OS: Windows 10

Why do I got error while video capturing with opencv

The following code has now started to return an error to me:
cv2 import
capture = cv2.VideoCapture(1)
capture.release()
return
[ WARN:0] global /tmp/pip-req-build-qacpj5ci/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
I have a video grabber connected and I had no problems with it earlier.
I don't see '/dev/video1' on my computer, but I see only '/dev/video0'.
From the information I found on the Internet, it seems to me that the most probable reason is that I did not close the capturing device.
I use ubuntu 18.04, python 3.6.9 and opencv-python===4.4.0.46.
I was running the code with jenkins as sudo (and sometimes the job was stopped manualy).
I tried to restart the computer and some other things I found on the Internet, but unfortunately nothing helped.
I'll add that at first I also couldn't run the webcam script in the computer (i.e. with parameter 0), but after some time I succeeded (in the meantime I was whisking some suspicious processes).

Why do I have to reset my USB cameras after capturing images with GStreamer (4 cameras multithreaded)

I have a python script that identifies four 13MP USB 3.0 cameras attached to my Ubuntu 18.04 machine, and launches the following GStreamer command for each in an individual thread (shown for example here is video0):
gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=1 ! image/jpeg ! filesink location=/root/test0.jpg
Each camera's parameters is set via v4l2-ctl thusly:
v4l2-ctl -d /dev/video0 --set-fmt-video=width=4208,height=3120,pixelformat=MJPG
The first time this launches it works great. I get four nice 13MP images and no output errors from the CLI. However, if I run this again, my images start to look scrambled. Here's an example:
I can solve this problem by resetting my USB devices. (I do it via fnctl in python, but it's functionally the same as re-plugging the devices.)
Can anyone explain how I could modify my GStreamer command to avoid this behavior?
try adding -ev to your gst-launch command

OpenCV Python Scripts Mac "aborts"

So I'm just trying to run the basic OpenCV program
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
But for some reason when I try to run it (with python 2 or 3) I get this weird abort statement
[1] 74309 abort python3 index.py
(I'm guessing the 5 digit number is the PID) However, it does work if I pass in a path to an already existing video in the VideoCapture function. I am a beginner in all of this so I'm not really sure what the problem is
Thanks :)
I found the solution!
I tried running the script using the default terminal that comes with mac and it worked :) So it seems there was some weird issue with the third party terminal (iTerm) I was using
You have to authorize iTerm to access the camera (in System Preferences). By default, neither Terminal nor iTerm has this permission. macOS Mojave just asks to allow iTerm and your Python program works fine!
It's not a problem with iTerm, or maybe was a problem.
I had the same problem running OpenCV from VSCode. It turned out to be indeed permission error for accessing the camera on the new macOS. I couldn't get VSCode to ask for the permission (or add it manually through system preferences).
My solution was to run VSCode as root using
sudo /Applications/Visual Studio Code.app/Contents/MacOS/Electron
It's not the cleanest solution, but it worked for me.
In VSCode ,
1.type cmd+shift+p
2.type "shell command: Install code in PATH"
3. Close vscode
4. Use "sudo code" to open vscode
5. It will give warning not to run as a root user
6. Ignore the warning and run the file , you will not get the "Abort trap: 6" error anymore.

Raspberry pi:enable camera module

I have attached an usb camera with raspberry pi and also enable it using sudo raspi-config command . I rebooted the raspberry pi after enabling it, but it doesn't enable - so getting error (error is..mmal: Camera is not detected. Please check carefully the camera module is installed correctly)
When run command (raspistill -o image.jpg) to capture an image, I have also run update and upgrade command to update raspberry pi but doesn't solve problem.
And main thing when I run ffnpng -i /dev/video video.avi, it records video so camera working so why this error coming? And what is the solution for that.
Is there any other way or command to cature image? plz help me. Thank u in advance.
Have you installed the fswebcam module? You can get it using:
sudo apt-get install fswebcam
Images are taken using the command fswebcam <image_name>.jpg
A full tutorial can be found here from raspberrypi.org.
It looks like the problem is the connection between camera module and Raspberry PI CSI interface. Check if the CSI cable is correctly connected to CSI interface on the Raspberry PI board or try to unplug camera cable and plug it again.
raspistillyuv and raspistill are official image capturing programs for the camera module: link
USB cameras are not mounted under mmal (Only CSI). Try running v4l2-ctl --list-devices to see if your camera is mounted correctly. You will see there is a /dev/videoX like string under your camera if it's mounted correctly. Use that X value to access the camera.
As an example, in OpenCV(Python) you can capture an image as bellow,
import cv2
cap = cv2.VideoCapture(X)
ret, frame = cap.read()
cv2.imwrite("image_name.jpg",frame)
cap.release()

Categories

Resources