Legacy camera compatability with tkinter and opencv?

cannot seem to get my camera GUI to work, i run the code it just does nothing. No error no gui.
i tried changing the cv2.VideoCature() to -1 and i just get an error.
is this a legacy camera compatability issue?

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


cap = cv2.VideoCapture(0)

#width, height =  (220,220)# width of camera, height of camera
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 200)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 200)

app = Tk()
app.bind('<Escape>', lambda e: app.quit())

label_widget = Label(app)
label_widget.pack()


def open_camera():
    # capture the video frmae by frame
    _, frame = cap.read()
    
    # convert image from one color spacee to the other
    opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    
    #capture the latest frame and transform to image
    captured_image = Image.fromarray(opencv_image)
    
    #convert captured image to photoimage
    photo_image = ImageTk.PhotoImage(image=captured_image)
    
    # displaying photoimage in the label
    label_widget.photo_image = photo_image

    # configuree image in the label
    label_widget.configure(image=photo_image)

    #repeat the same process after every 10 seconds
    label_widget.after(10, open_camera)
    
    
button1 = Button(app, text="Open Camera", command=open_camera)
button1.pack()

app.mainloop()

    

Hi @wes261965

I once followed this thread below to expose the available cameras on my device.
This is a good way to check if your cameras are compatible.
if you see the camera you are looking for, you can specify it’s port index to opencv and target it. :slight_smile:

Good luck.
Pix :heavy_heart_exclamation:

hi @wes261965 - what other troubleshooting steps have you tried?

It might be a good start to simplify the project down the the essentials - can you get an image to capture without using tkinter code? or are there any helpful error messages in that case?

1 Like