TKinter-Code (main.py):
Code: Select all
import cv2
import tkinter as tk
from PIL import Image, ImageTk
# Initialize main window
root = tk.Tk()
root.title("Camera Stream")
# Create a label in the window to display frames
label = tk.Label(root)
label.pack()
# Open the default camera (usually 0)
cap = cv2.VideoCapture(0)
def update_frame():
ret, frame = cap.read()
if ret:
# Convert the frame from BGR to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Convert to PIL Image
img = Image.fromarray(frame)
# Convert to ImageTk
imgtk = ImageTk.PhotoImage(image=img)
# Update the label
label.imgtk = imgtk
label.configure(image=imgtk)
# Repeat every 10ms
label.after(10, update_frame)
# Start updating frames
update_frame()
# Run the GUI loop
root.mainloop()
# Release camera when closing
cap.release()
Mobile version