I am trying to run OpenCV on RasberriPi but it's not working

  1. [ WARN:0] global …/modules/videoio/src/cap_gstreamer.cpp (1824) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Could not open device ‘/dev/video0’ for reading and writing. [ WARN:0] global …/modules/videoio/src/cap_gstreamer.cpp (914) open OpenCV | GStreamer warning: unable to start pipeline [ WARN:0] global …/modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created [ WARN:0] global …/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can’t open camera by index Traceback (most recent call last): File “/home/koulas/Desktop/16_Client.py”, line 12, in ret,buffer = cv2.imencode(“.jpg”,photo,[int(cv2.IMWRITE_JPEG_QUALITY),30]) cv2.error: OpenCV(4.5.1) …/modules/imgcodecs/src/loadsave.cpp:934: error: (-215:Assertion failed) !image.empty() in function ‘imencode’

  2. [10:18 PM]

The code is working on PC but on Pi no so I think that problem is with opencv

what do the error messages mean?

I have no idea, thats why I am here.

well, the first part says

so why could that be?

either there’s no camera, or there is but something went wrong trying to read from it.

does your pi have a camera attached to it? what camera is it? how is it attached?

The camera is USB, I have tried take a picture with it by commands with terminal -
ffmpeg -f v4l2 -video_size 1280x720 -i /dev/video0 -frames 1 out.jpg
it made a picture.

Good, then this should have worked in principle.

Please present the Python code you ran.

Make sure no other process currently accesses the camera. Then you should try

cap = cv.VideoCapture(0, apiPreference=cv.CAP_V4L2)
assert cap.isOpened()
import cv2,socket,pickle,os
import numpy as np
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_SNDBUF,1000000)
server_ip = "192.168.11.241"
server_port = 6666

cap = cv2.VideoCapture(0)
while True:
    ret,photo = cap.read()
    #cv2.imshow('streaming',photo)
    ret,buffer = cv2.imencode(".jpg",photo,[int(cv2.IMWRITE_JPEG_QUALITY),30])
    x_as_bytes = pickle.dumps(buffer)
    s.sendto((x_as_bytes),(server_ip,server_port))
    if cv2.waitKey(10)==13:
        break
cv2.destroyAllWindows()
cap.release()

I tried to rework it with your lines but it didnt work

This is new code

import cv2,socket,pickle,os
import numpy as np
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_SNDBUF,1000000)
server_ip = "192.168.11.241"
server_port = 6666

cap = cv2.VideoCapture(0, apiPreference=cv2.CAP_V4L2)

while True:
    ret,photo = cap.read()
    #cv2.imshow('streaming',photo)
    ret,buffer = cv2.imencode(".jpg",photo,[int(cv2.IMWRITE_JPEG_QUALITY),30])
    x_as_bytes = pickle.dumps(buffer)
    s.sendto((x_as_bytes),(server_ip,server_port))
    if cv2.waitKey(10)==13:
        break
cv2.destroyAllWindows()
cap.release()
[ WARN:0@0.026] global cap_v4l.cpp:982 open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Traceback (most recent call last):
  File "/home/koulas/Desktop/16_Client.py", line 13, in <module>
    ret,buffer = cv2.imencode(".jpg",photo,[int(cv2.IMWRITE_JPEG_QUALITY),30])
cv2.error: OpenCV(4.7.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:1109: error: (-215:Assertion failed) !image.empty() in function 'imencode'

And this is log :confused:

why did you drop all error checking related to VideoCapture?

That is whole code+ errors. Nothing more showed up.

I am not asking about error messages. I am asking why your code doesn’t contain error checking.

anyway, if the V4L2 backend can’t open the device, but ffmpeg can, I don’t know the reason.

you can try setting CAP_PROP_FRAME_WIDTH to 1280 and HEIGHT to 720. maybe the camera doesn’t support the “default” 640x480 that OpenCV tries.

if that doesn’t help, I’ll stick with “multiple processes”. you probably have multiple instances of your script running, or you keep ffmpeg/VLC/guvcview active on the side to watch the camera, or something like that.

I am not that good.
I don’t know how to add error checking.
I ordered original Pi Cam module 3 so I won’t have problems with USB.
I will try it when I will at home.

to get those pi cams to present as /dev/videoN (V4L/V4L2), the pi has to be configured to use “legacy” video. then you can use them with OpenCV’s VideoCapture.

by default, you need special libraries to access those pi cameras. those libraries are fairly easy to use, especially in python (you get numpy arrays), but then you don’t use OpenCV’s VideoCapture anymore.

i have the same problem -_- i have been in this for 2 weeks so please did u solve it ?