Question: Important instruction: do not use the old picamera module for python and do not answer this using picamera module because it is incorrect. This code

Important instruction: do not use the old picamera module for python and do not answer this using picamera module because it is incorrect. This code is based and must be based on the Picamera2 library pdf file documentation.
I specifically need to run this with picamera2 module. Using the picamera module is not compatible to my system. So far, the GUI shows up with a black or frozen display that is coming from the raspberry pi camera. Please make this code so that is able to detect object in real-time using the raspberry pi camera. I need the raspberry pi camera to continuously run so it can detect object in real-time.
The code (run in Thonny):
import tkinter as tk
from tkinter import Label, Frame
import torch
from picamera2 import Picamera2 #based on the Picamera2 library pdf file
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()
#based on the Picamera2 library pdf file
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() #based on the Picamera2 library pdf file
frame = request.make_array('main') #based on the Picamera2 library pdf file
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!