0

I am curently trying to run the code below, which feeds a live stream to a tkinter gui:

from tkinter import *
from PIL import ImageTk, Image
import cv2


root = Tk()
# Create a frame
app = Frame(root, bg="white")
app.grid()
# Create a label in the frame
lmain = Label(app)
lmain.grid()

# Capture from camera
cap = cv2.VideoCapture(0)

# function for video streaming
def video_stream():
    _, frame = cap.read()
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(1, video_stream) 

video_stream()
root.mainloop()

I have installed all of the necessary libraries however, when I try to run the script, I receive the following error:

Traceback (most recent call last):
  File "/home/raspberry/project/myenv/tk_gui.py", line 27, in <module>
    video_stream()
  File "/home/raspberry/project/myenv/tk_gui.py", line 20, in video_stream
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
cv2.error: OpenCV(4.7.0) /tmp/pip-install-savyw78j/opencv-python_b8f9ba2be8f24d66872bc39347c45cd7/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

I have had a look around but I can't seem to find any issue relating to this. Am I perhaps calling something wrong or haven't properly installed the libraries?

OpenCV version: 4.7.0 Camera: Raspberry Pi Camera Module 3 (IMX708)

Any help would be appreciated,

Thanks in advance!

1 Answer 1

0

There is no problem with your code, works fine on my pc, I just reproduced your error by disconnecting camera. Please make sure you connected your camera correctly to the raspberry pi. Try reconnecting the camera. !_src.empty() means you are passing the argument "frame" which is empty, i.e. it does not have anything for cv2 to convert the colour (cvtColor). Thus your code cannot fetch the frame from your camera. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.