-
-
Notifications
You must be signed in to change notification settings - Fork 17.1k
Description
Hi! Soo what I want to do is that my Yolov5 model saves in a folder the detections it makes when working with a camera in real time, in my Raspberry PI 4B+.
I found this code made by glenn-jocher responding to user sanchaykasturey at the following link:
#11102, and I tried to modify it to my needs, but I realised that although it takes the images and saves them, it doesn't do it because it detects any object or class: It just takes them and doesn't classify any object... I tried changing the model to the 'yolov5s' but then the code doesn't even run.
I'm very confused as I'm new to this and I'm really not sure if this happens because I'm working on a Raspberry or if it's a problem with the code. Could someone help me?
Here is the code I have modified slightly to test...
import torch
from PIL import Image
import cv2
import datetime
CKPT_PATH = '/home/pi/yolov5/yolov5s.pt'
yolov5 = torch.hub.load('/home/pi/yolov5', 'custom', path=CKPT_PATH, source='local', force_reload=True)
vidcap = cv2.VideoCapture(0)
success, image = vidcap.read()
while success:
# Convert image to PIL format
img_pil = Image.fromarray(image)
# Perform YOLOv5 inference
results = yolov5(img_pil)
# Check if any detections are made
if len(results.pred) > 0:
# Save the frame as an image
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
image_name = f"image_{timestamp}.jpg"
cv2.imwrite(image_name, image)
# Read the next frame
success, image = vidcap.read()
# Release the video capture
vidcap.release()
PS: Sorry if I didn't tag it correctly I'm new here and thought this didn't fit any tag.