-
Notifications
You must be signed in to change notification settings - Fork 3
/
detecting-images.py
87 lines (69 loc) · 3.27 KB
/
detecting-images.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import cv2
from ultralytics import YOLO
def detect_objects_in_photo(image_path):
image_orig = cv2.imread(image_path)
yolo_model = YOLO('./runs/detect/Normal_Compressed/weights/best.pt')
results = yolo_model(image_orig)
for result in results:
classes = result.names
cls = result.boxes.cls
conf = result.boxes.conf
detections = result.boxes.xyxy
for pos, detection in enumerate(detections):
if conf[pos] >= 0.5:
xmin, ymin, xmax, ymax = detection
label = f"{classes[int(cls[pos])]} {conf[pos]:.2f}"
color = (0, int(cls[pos]), 255)
cv2.rectangle(image_orig, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
cv2.putText(image_orig, label, (int(xmin), int(ymin) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1, cv2.LINE_AA)
result_path = "./imgs/Test/teste.jpg"
cv2.imwrite(result_path, image_orig)
return result_path
def detect_objects_in_video(video_path):
yolo_model = YOLO('./runs/detect/Normal_Compressed/weights/best.pt')
video_capture = cv2.VideoCapture(video_path)
width = int(video_capture.get(3))
height = int(video_capture.get(4))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
result_video_path = "detected_objects_video2.avi"
out = cv2.VideoWriter(result_video_path, fourcc, 20.0, (width, height))
while True:
ret, frame = video_capture.read()
if not ret:
break
results = yolo_model(frame)
for result in results:
classes = result.names
cls = result.boxes.cls
conf = result.boxes.conf
detections = result.boxes.xyxy
for pos, detection in enumerate(detections):
if conf[pos] >= 0.5:
xmin, ymin, xmax, ymax = detection
label = f"{classes[int(cls[pos])]} {conf[pos]:.2f}"
color = (0, int(cls[pos]), 255)
cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
cv2.putText(frame, label, (int(xmin), int(ymin) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1, cv2.LINE_AA)
out.write(frame)
video_capture.release()
out.release()
return result_video_path
def detect_objects_and_plot(path_orig):
image_orig = cv2.imread(path_orig)
yolo_model = YOLO('./runs/detect/Normal_Compressed/weights/best.pt')
results = yolo_model(image_orig)
for result in results:
classes = result.names
cls = result.boxes.cls
conf = result.boxes.conf
detections = result.boxes.xyxy
for pos, detection in enumerate(detections):
if conf[pos] >= 0.5:
xmin, ymin, xmax, ymax = detection
label = f"{classes[int(cls[pos])]} {conf[pos]:.2f}"
color = (0, int(cls[pos]), 255)
cv2.rectangle(image_orig, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
cv2.putText(image_orig, label, (int(xmin), int(ymin) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1, cv2.LINE_AA)
cv2.imshow("Teste", image_orig)
cv2.waitKey(0)
cv2.destroyAllWindows()