-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
57 lines (41 loc) · 1.6 KB
/
Copy pathpredict.py
File metadata and controls
57 lines (41 loc) · 1.6 KB
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
import os
import cv2
from matplotlib import pyplot as plt
from ultralytics import YOLO
def read_classes(classes_list):
return {i: name for i, name in enumerate(classes_list)}
def predict(model, image_path, output_path, save=False):
fig, axes = plt.subplots(1, figsize=(15, 5))
axes = [axes]
image = cv2.imread(image_path)
results = model.predict(image_path, conf=0.55)
for result in results:
boxes = result.boxes.xyxy
confs = result.boxes.conf
classes = result.boxes.cls
# Draw bounding boxes and labels on the frame
for box, conf, cls in zip(boxes, confs, classes):
x1, y1, x2, y2 = map(int, box)
label = f"{model.names[int(cls)]} {conf:.2f}"
color = (0, 255, 0)
cv2.rectangle(image, (x1, y1), (x2, y2), color, 3)
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 2, color, 2)
axes[0].set_title(os.path.basename(image_path))
plt.tight_layout()
plt.show()
if save:
if not os.path.exists(output_path):
os.makedirs(output_path)
raise NotImplementedError
def inference_on_video():
model_path = './best_model.pt'
# Local paths
# video_path = './data/id_video_data/test2.mp4'
# output_path = './data/id_video_data'
# Server paths
image_path = '/datashare/HW1/labeled_image_data/images/val/ff8c22da-output_0182.png'
output_path = './'
model = YOLO(model_path)
predict(model, image_path, output_path)
if __name__ == '__main__':
inference_on_video()