Question: Hi ! When I run this code, it just shows the gui and a frozen display where it should've continuously show the display from the

Hi!
When I run this code, it just shows the gui and a frozen display where it should've continuously show the display from the rasapberry pi camera lens. Currently learning Yolov5 object detection in real-time (use of picamera2 module is a must in the latest python update in raspberry pi). Please show me how I can continuously run the camera of the raspberry pi so that it can detect object in real-time. This code is running in raspberry pi 64-bit Bullseye system and I am using raspberry pi camera v2. Thank you so much to the person that will help me solve this camera problem
The code (run in Thonny):
import tkinter as tk
from tkinter import Label, Frame
import torch
from picamera2 import Picamera2
import numpy as np
from PIL import Image, ImageTk
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/best.pt',force_reload=True)
model.conf =0.2
app = tk.Tk()
app.geometry("700x600")
app.title("GUI")
label1= Label(app, text='detectionTest', font=("Arial",24))
label1.pack(side='top', pady=20)
vid_frame = Frame(height=600, width=600)
vid_frame.pack()
vid = Label(vid_frame)
vid.pack()
camera = Picamera2()
camera_config = camera.create_preview_configuration(main={"size":(640,480)})
camera.configure(camera_config)
camera.start()
def detect():
while True:
request = camera.capture_request()
frame = request.make_array('main')
results = model(frame)
output_image = np.squeeze(results.render())
display_image = Image.fromarray(output_image)
imgtk = ImageTk.PhotoImage(image=display_image)
vid.imgtk = imgtk
vid.configure(image=imgtk)
vid.after(10, detect)
break
detect()
app.mainloop()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!