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

**URL:** <https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799>\
**Category:** Python\
**Tags:** raspberrypi\
**Created:** [January 29, 2023, 9:11am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799 "2023-01-29T09:11:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Koulas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/koulas/32/7066_2.png) [@Koulas](https://forum.opencv.org/u/Koulas)\
**Post date:** [January 29, 2023, 9:11am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/1 "2023-01-29T09:11:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 29, 2023, 10:35am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/2 "2023-01-29T10:35:13Z")

</div>

what do the error messages mean?

---

<div class="post-metadata">

**Author:** ![Koulas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/koulas/32/7066_2.png) [@Koulas](https://forum.opencv.org/u/Koulas)\
**Post date:** [January 29, 2023, 11:27am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/3 "2023-01-29T11:27:14Z")

</div>

I have no idea, thats why I am here.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 29, 2023, 12:43pm UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/4 "2023-01-29T12:43:37Z")

</div>

well, the first part says

> [@Koulas](#):
>
> Could not open device ‘/dev/video0’ for reading

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?

---

<div class="post-metadata">

**Author:** ![Koulas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/koulas/32/7066_2.png) [@Koulas](https://forum.opencv.org/u/Koulas)\
**Post date:** [January 29, 2023, 1:03pm UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/5 "2023-01-29T13:03:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 29, 2023, 11:05pm UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/6 "2023-01-29T23:05:41Z")

</div>

> [@Koulas](#):
>
> ffmpeg -f v4l2  
> 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

```auto
cap = cv.VideoCapture(0, apiPreference=cv.CAP_V4L2)
assert cap.isOpened()

```

---

<div class="post-metadata">

**Author:** ![Koulas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/koulas/32/7066_2.png) [@Koulas](https://forum.opencv.org/u/Koulas)\
**Post date:** [January 30, 2023, 5:27am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/7 "2023-01-30T05:27:30Z")

</div>

```auto
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

---

<div class="post-metadata">

**Author:** ![Koulas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/koulas/32/7066_2.png) [@Koulas](https://forum.opencv.org/u/Koulas)\
**Post date:** [January 30, 2023, 3:25pm UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/8 "2023-01-30T15:25:06Z")

</div>

This is new code

```auto
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()

```

```auto
[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 😕

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 31, 2023, 12:02am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/9 "2023-01-31T00:02:39Z")

</div>

why did you drop all error checking related to VideoCapture?

---

<div class="post-metadata">

**Author:** ![Koulas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/koulas/32/7066_2.png) [@Koulas](https://forum.opencv.org/u/Koulas)\
**Post date:** [January 31, 2023, 5:32am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/10 "2023-01-31T05:32:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 31, 2023, 11:54am UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/11 "2023-01-31T11:54:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Koulas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/koulas/32/7066_2.png) [@Koulas](https://forum.opencv.org/u/Koulas)\
**Post date:** [January 31, 2023, 12:11pm UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/12 "2023-01-31T12:11:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 31, 2023, 9:49pm UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/13 "2023-01-31T21:49:30Z")

</div>

> [@Koulas](#):
>
> Pi Cam module 3

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.

---

<div class="post-metadata">

**Author:** ![Youssef\_Hossam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/youssef_hossam/32/9927_2.png) [@Youssef\_Hossam](https://forum.opencv.org/u/Youssef_Hossam)\
**Post date:** [March 13, 2024, 2:05pm UTC](https://forum.opencv.org/t/i-am-trying-to-run-opencv-on-rasberripi-but-its-not-working/11799/14 "2024-03-13T14:05:44Z")

</div>

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