Question: I am currently running Raspberry pi 6 4 - bit OS ( bookworm ) . I am currently doing an object detection project using YOLOv

I am currently running Raspberry pi 64-bit OS (bookworm). I am currently doing an object detection project using YOLOv5 in Pycharms using python. This code works well when I connect webcam to raspberry pi but it seems it does not work with raspberry pi camera. Please make this code below that can connect to my raspberry pi camera for object detection. Please take note that the old picamera module does not work anymore with the latest OS of raspberry pi. As far as I am aware, it seems picamera2 or libcamera works fine.
The code so far:
import tkinter as tk
import torch
import picamera2
import cv2
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("ObjectTest")
label1= tk.Label(app, text='detectionTest', font=("Arial",24))
label1.pack(side='top', pady=20)
vidFrame = tk.Frame(height=600, width=600)
vidFrame.pack()
vid = tk.Label(vidFrame, text="")
vid.pack()
camera = picamera2.PiCamera()
def detect():
frame = np.empty((240,320,3), dtype=np.uint8)
camera.capture(frame,'bgr', use_video_port=True)
results = model(frame)
output_image = 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)
detect()
app.mainloop()
I will appreciate explanations for the code that you will add and please also let me know if you installed any other packages. Thank you so much

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 Accounting Questions!