-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_model.py
More file actions
58 lines (37 loc) · 1.42 KB
/
test_model.py
File metadata and controls
58 lines (37 loc) · 1.42 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
58
from ultralytics import YOLO
import os
from PIL import Image, ImageDraw, ImageFont
model_path = "model/best.pt"
def test_yolo_model():
images_dir = "images"
model = YOLO(model_path)
image_files = [os.path.join(images_dir, f"example_{i}.jpg") for i in range(1, 4)]
for image_file in image_files:
print(f"Testowanie obrazu: {image_file}")
results = model(image_file)
for result in results:
print(result.boxes.xyxy)
def draw_results_on_image(image_path):
model_path = "model/best.pt"
model = YOLO(model_path)
results = model(image_path)
image = Image.open(image_path).convert("RGB")
draw = ImageDraw.Draw(image)
for box in results[0].boxes:
x_min, y_min, x_max, y_max = map(int, box.xyxy[0].tolist())
confidence = box.conf.item()
draw.rectangle([x_min, y_min, x_max, y_max], outline="green", width=2)
text = f"Conf: {confidence:.2f}"
text_bbox = draw.textbbox((x_min, y_min), text)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
text_position = (x_min, y_min - text_height)
draw.rectangle(
[text_position, (x_min + text_width, y_min)],
fill="green",
)
draw.text(text_position, text, fill="white")
image.show()
if __name__ == "__main__":
test_yolo_model()
draw_results_on_image("images/example_2.jpg")