-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_inference_yolov8.py
More file actions
43 lines (30 loc) · 1.22 KB
/
image_inference_yolov8.py
File metadata and controls
43 lines (30 loc) · 1.22 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
import cv2
import supervision as sv
from supervision import Color
from ultralytics import YOLO
YOLO_MODEL = YOLO("orang_outan_detection/train7/weights/best.pt")
BOX_ANNOTATOR = sv.BoxAnnotator(color=Color.from_hex("#FF00E4"))
# path_image = "petit-ouran-outang-mort-pairi-daiza-belgique-960x640.jpg"
path_image = "weekend-chimpanze.jpg"
confidence = 0.6
output = YOLO_MODEL(path_image, verbose=False)[0]
detections = sv.Detections.from_ultralytics(output)
detections = detections[detections.confidence >= confidence]
labels = [
f"{output.names[class_id]} {confidence:0.2f}"
for _, _, confidence, class_id, _ in detections
]
thickness = 2
text_thickness = 1
text_scale = 1.0
height, width, _ = output.orig_img.shape
thickness_ratio = ((width + height) / 2) / 400
text_scale_ratio = ((width + height) / 2) / 600
text_thickness_ratio = ((width + height) / 2) / 400
BOX_ANNOTATOR.thickness = int(thickness * thickness_ratio)
BOX_ANNOTATOR.text_scale = float(text_scale * text_scale_ratio)
BOX_ANNOTATOR.text_thickness = int(text_thickness * text_thickness_ratio)
annotated_frame = BOX_ANNOTATOR.annotate(
scene=output.orig_img.copy(), detections=detections, labels=labels
)
cv2.imwrite("annotate_image.jpg", annotated_frame)