I could not stream camera on raspberry pi 4

53 Views Asked by At

I'm trying to use my picamera module v3 on raspi 4 and meet this error:

[ WARN:[email protected]] global cap_v4l.cpp:1134 tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.

This is the code:

import cv2 
from ultralytics import YOLO
import numpy as np
import imutils
model = YOLO('best (5) (1).pt')
cap = cv2.VideoCapture(0)
isClosed = True
color = (255, 255, 255)
thickness = 3

while cap.isOpened():
    success, frame = cap.read()
    if success:
        results = model(frame)
        annotated_frame = results[0].plot()
        res = results[0]
        masks = res.masks
        mask1 = masks[0]
        mask = mask1.data[0].numpy()
        polygon = mask1.xy[0]
        polygon = polygon.reshape((-1, 1, 2))
        #print(point)
        #image = cv2.polylines(frame, np.int32([polygon]), isClosed, color, thi>
        image = np.zeros(frame.shape,np.uint8)
        cv2.fillPoly(image,np.int32([polygon]), color)
        cv2.imshow("YOLOv8 Inference", frame)
        cv2.imshow("YOLOv8 Inference1", image)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()

I tried to change cap = cv2.VideoCapture(0) to cap = cv2.VideoCapture(1) or (-1) but there's no result. How can I fix it?

1

There are 1 best solutions below

0
Mohammad Hashemi On

Open your terminal and write

vcgencmd get_camera

The result shows you if the camera is detected. If your camera was not detected please at first check ribbon cable to connect to specific slot correctly. After test these ways if you have problems please share to solve problem together.