usb webcam can not open with this simple code

import cv2

# Open the webcam
cap = cv2.VideoCapture(0)

# Check if the webcam is successfully opened
if not cap.isOpened():
    print("Failed to open webcam.")
    exit(1)

# Capture a frame from the webcam
ret, frame = cap.read()

# Check if the frame is captured successfully
if not ret:
    print("Failed to capture frame.")
    exit(1)

# Display the captured frame
cv2.imshow("Webcam", frame)
cv2.waitKey(0)

# Release the webcam
cap.release()
cv2.destroyAllWindows()

Does the video device exist ?
If so, what is the user/group of the device.
If you are not running as root are you a member of the above group

sudo su
as root

and
I test this usermod as debian

ls /dev/video0 exist
and I try chmod +x file.py
and i also try the xrendr
export DISPLAY=:0.0
xrandr --output HDMI1

sudo apt-get install python3-opencv libopencv-videoio-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

I also test this code
import cv2

Open the video capture device using V4L2

video_capture = cv2.VideoCapture(‘/dev/video0’, cv2.CAP_V4L2)

Check if the video capture device was successfully opened

if not video_capture.isOpened():
print(“Unable to open video capture device”)
exit()

while True:
# Read a frame from the video capture
ret, frame = video_capture.read()

# Check if the frame was successfully read
if not ret:
    print("Error reading frame from video capture")
    break

# Display the frame on the screen
cv2.imshow('Webcam', frame)

# Exit the loop if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

Release the video capture and close the window

video_capture.release()
cv2.destroyAllWindows()

[ WARN:0] global …/modules/videoio/src/cap_v4l.cpp (1004) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
Error reading frame from video capture

and also
import cv2

GStreamer pipeline

pipeline = “v4l2src device=/dev/video0 ! videoconvert ! video/x-raw,format=BGR ! appsink”

Create a VideoCapture object using the GStreamer pipeline

cap = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)

Check if the VideoCapture object was successfully initialized

if not cap.isOpened():
print(“Failed to open camera.”)
exit(1)

Read and display frames from the camera

while True:
ret, frame = cap.read()

if not ret:
    print("Failed to capture frame.")
    break

# Display the frame using imshow
cv2.imshow("Webcam", frame)

# Check for the 'q' key to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

Release the VideoCapture object and close any open windows

cap.release()
cv2.destroyAllWindows()

all of that work on ubuntu but not work on debian 9.9 beagle bone black

Ok just had a quick play.

This is using a newer OS version than you, clean install, just downloaded.

BeagleBoard.org Debian Bullseye Xfce Image 2023-05-18

I installed python3-opencv which pulled in the dependencies.

This is what I am running (see below). I have no display so can’t be sure what it is capturing, but I get no errors.

I had to add the cap.set() calls though to make it work. Just cv2.VideoCapture() on its own does not work.

#!/usr/bin/python3

import cv2

cap = cv2.VideoCapture("/dev/video0", cv2.CAP_V4L2)

cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
width = 320
height = 240
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)


if not cap.isOpened():
        print('Unable to open video capture device')
        exit()


while(True):
        ret,frame = cap.read()
        if not ret:
                print('Error reading frame from video capture')
                break
        else:
                print('Got frame')

        if cv2.waitKey(1)&0xff == ord('q'):
                break

cap.release()

I suggest firstly you try setting the mode, and resolution as I have done. It that still fails I would suggest updating your OS.

Bullseye XFCE (with graphical desktop) for BeagleBone AI-64 via microSD card

it doesn’t work for beagle bone black

That’s totally expected, the BeagleBone Black and BeagleBone AI-64 use a different processor…

Grab an image from here, for your BeagleBone Black (am335x): Debian 11.x (Bullseye) - Monthly Snapshot - 2023-10-07

Regards,

after that install we have to build opencv with cmake through cross compile