-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiModel.py
65 lines (41 loc) · 1.84 KB
/
aiModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import cv2
from ultralytics import YOLO
import numpy as np
# Load the video capture
cap = cv2.VideoCapture("orange.mp4")
# Initialize the YOLO model
model = YOLO("yolov8m.pt")
def trackFrame(frame):
# Run YOLOv8 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True ,classes = [49], device = "mps",iou_threshold=0.95)
result = results[0]
classes = result.boxes.cls.cpu().numpy()
print("checking if classes really exist",result,classes)
# Visualize the results on the frame
annotated_frame = result.plot()
# Return the annotated frame
return annotated_frame
def processFrame(frame):
# Perform inference using the YOLO model on the frame
results = model(frame, device="mps")
# Counter to determine number of oranges in a single frame
count = 0
# Access the first result (YOLO model may return multiple results for different scales)
result = results[0]
# Extract bounding boxes, classes, and confidence scores
bounding_boxes = result.boxes.xyxy.cpu().numpy()
classes = result.boxes.cls.cpu().numpy()
confidences = result.boxes.conf.cpu().numpy()
# Iterate over detected objects
for box, class_type, confidence in zip(bounding_boxes, classes, confidences):
(x1, y1, x2, y2) = box.astype(int)
if confidence > 0.6 :
# Draw bounding box and label on the frame
label = model.names[int(class_type)]
label_text = f"{label}: {confidence:.2f}"
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, label_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Increment the count of recognised oranges in the frame
count +=1
# Display the annotated frame
return frame