Cannot access webcam with opencv2 in python

I am trying to use opencv2 to run yolov8 on my webcam input. Unfortunately I cannot access my webcam with opencv. I am using a CSI-Cam and “nvgstcapture-1.0” does work.

I get the following error:
Traceback (most recent call last):
File “”, line 59, in
cv2.imshow(‘Webcam’, img)
cv2.error: OpenCV(4.8.1) /io/opencv/modules/highgui/src/window.cpp:971: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

and my relevant code segments look like this (my guess is that there is either a problem with the imshow() or cv2.VideoCapture(0):

from ultralytics import YOLO
import cv2
import math

start webcam

cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)


model = YOLO(“yolo-Weights/”)

object classes

classNames = [“person”, “bicycle”, “car”, “motorbike”, “aeroplane”, “bus”, “train”, “truck”, “boat”,
“traffic light”, “fire hydrant”, “stop sign”, “parking meter”, “bench”, “bird”, “cat”,
“dog”, “horse”, “sheep”, “cow”, “elephant”, “bear”, “zebra”, “giraffe”, “backpack”, “umbrella”,
“handbag”, “tie”, “suitcase”, “frisbee”, “skis”, “snowboard”, “sports ball”, “kite”, “baseball bat”,
“baseball glove”, “skateboard”, “surfboard”, “tennis racket”, “bottle”, “wine glass”, “cup”,
“fork”, “knife”, “spoon”, “bowl”, “banana”, “apple”, “sandwich”, “orange”, “broccoli”,
“carrot”, “hot dog”, “pizza”, “donut”, “cake”, “chair”, “sofa”, “pottedplant”, “bed”,
“diningtable”, “toilet”, “tvmonitor”, “laptop”, “mouse”, “remote”, “keyboard”, “cell phone”,
“microwave”, “oven”, “toaster”, “sink”, “refrigerator”, “book”, “clock”, “vase”, “scissors”,
“teddy bear”, “hair drier”, “toothbrush”

while True:
success, img =
results = model(img, stream=True)

# coordinates
for r in results:
    boxes = r.boxes

    for box in boxes:
        # bounding box
        x1, y1, x2, y2 = box.xyxy[0]
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values

        # put box in cam
        cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)

        # confidence
        confidence = math.ceil((box.conf[0]*100))/100
        print("Confidence --->",confidence)

        # class name
        cls = int(box.cls[0])
        print("Class name -->", classNames[cls])

        # object details
        org = [x1, y1]
        font = cv2.FONT_HERSHEY_SIMPLEX
        fontScale = 1
        color = (255, 0, 0)
        thickness = 2

        cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)

cv2.imshow('Webcam', img)
if cv2.waitKey(1) == ord('q'):



cap = cv2.VideoCapture(0)

Please input the corresponding pipeline instead of 0.
You can find some examples below:


Thanks for your answer.
I tried
cap = cam= cv2.VideoCapture(nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM),width=1920, height=1080,framerate=21/1, format=NV12 ! nvvidconv flip-method=2 ! video/x-raw,format=BGRx, width=816, height=616, pixel-aspect-ratio=1/1 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1).

Now I get an OpenCV-Error:
OpenCV(4.8.1) /io/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! appsink in function ‘icvExtractPattern’

and still the same error:
cv2.error: OpenCV(4.8.1) /io/opencv/modules/highgui/src/window.cpp:971: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

Also Yolo tells me, that the source is missing and defaults to two local images.


Could you try to run the gst-launch command to see if the pipeline works?

For example:

gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! 'video/x-raw(memory:NVMM),width=3280, height=2464,framerate=21/1, format=NV12' ! nvvidconv flip-method=2 ! 'video/x-raw, width=816, height=616' ! nvvidconv ! nvegltransform ! nveglglessink -e



Sorry for the missing.

The web camera needs to use V4L2 components.
You can find some examples in the below topic:


This command works perfectly.

None of the pipelines from the the other post seemed to work and still resulted in
“error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file)”.


“error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file)”.

Are you calling the VideoWriter int he same app?
If yes, could you turn it off and try it again?



I “solved” the problem by buying a USB-Webcam that instantly works. It seems to me that those are better than CSI-Cameras - at least in my simple case. Thanks for trying to help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.