-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
180 lines (155 loc) · 6.1 KB
/
model.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import torch
import cv2
import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
# Load the YOLOv8 model
day_model = torch.hub.load('yolov5', 'custom', r'weights/day-final.pt', source='local', force_reload=True, trust_repo=True,device="cpu")
thermal_model = torch.hub.load('yolov5',"custom", path=r'weights/night-final-aug.pt', source='local', force_reload=True,device='cpu')
day_class_to_animal = {
0: 'Person',
1: 'Elephant',
2: 'Zebra',
3: 'Giraffe',
4: 'Deer',
5: 'Bison',
6: 'Rhino',
7: 'Boar',
8: 'Leopard',
9: 'Vehicle',
10: 'Fire'
}
thermal_class_to_animal = {
0: "Person",
1: "Elephant",
2: "Deer",
3: "Rhino",
4: "Boar",
5: "Leopard",
6: "Vehicle",
7: "Fire"
}
def plotBbox(results, frame, class_dict):
for box in results.xyxy[0]:
xA, yA, xB, yB, confidence, class_id = box
class_id = int(class_id)
class_name = class_dict.get(int(class_id), 'Unknown')
# Define a unique color for each class
color = get_color(class_id)
xA = int(xA)
xB = int(xB)
yA = int(yA)
yB = int(yB)
# Draw the bounding box with the class-specific color
cv2.rectangle(frame, (xA, yA), (xB, yB), color, 2)
# Add text label with class name and confidence
label = f"{class_name}: {confidence:.2f}"
y = yA - 15 if yA - 15 > 15 else yA + 15
cv2.putText(frame, label, (xA, y), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
return frame
def get_color(class_id):
# Define a list of colors for different classes
colors = [
(255,99,71),
(124,252,0),
(255,215,0),
(255, 255, 0),
(0, 255, 255),
(255, 0, 255),
(255,218,185),
(138,43,226),
(255,20,147),
(176,196,222),
(0,250,154)
]
return colors[class_id]
def predictVideo(temp_video_path, video_class):
cap = cv2.VideoCapture(f'static/{temp_video_path}')
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(fps)
output_path = '/out/output_video.webm'
out = cv2.VideoWriter(f'static/{output_path}', cv2.VideoWriter_fourcc(*"vp80"), fps, (width, height))
result = []
print('prediction start')
if video_class == 'Daylight':
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLOv8 inference on the frame
frame = cv2.resize(frame, (width, height))
results = day_model(frame)
print('Status: OK')
# Check if there are any detections in this frame
# Run YOLOv8 inference on the frame
# Visualize the results on the frame
# print(results.pandas().xyxy[0])
annotated_frame = plotBbox(results=results, frame=frame, class_dict=day_class_to_animal)
# Get bounding box coordinates and labels
out.write(annotated_frame)
if len(results.xyxy[0]) == 0:
continue
classes = results.pandas().xyxy[0]['class']
confidences = results.pandas().xyxy[0]['confidence']
print(classes,confidences)
frame_detections = {"frame_number": len(result) + 1,
"detections":[]}
for class_id, confidence in zip(classes, confidences):
obj = {
"class_id": class_id,
"class_name": day_class_to_animal.get(int(class_id), 'Unknown'),
"confidence": confidence,
}
frame_detections['detections'].append(obj)
result.append(frame_detections)
# Display the annotated frame
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
else:
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLOv8 inference on the frame
frame = cv2.resize(frame, (width, height))
results = thermal_model(frame)
print('OK')
# Check if there are any detections in this frame
# Run YOLOv8 inference on the frame
# Visualize the results on the frame
# print(results.pandas().xyxy[0])
annotated_frame = plotBbox(results=results, frame=frame, class_dict=thermal_class_to_animal)
# Get bounding box coordinates and labels
out.write(annotated_frame)
if len(results.xyxy[0]) == 0:
continue
classes = results.pandas().xyxy[0]['class']
confidences = results.pandas().xyxy[0]['confidence']
print(classes,confidences)
frame_detections = {"frame_number": len(result) + 1,
"detections":[]}
for class_id, confidence in zip(classes, confidences):
obj = {
"class_id": class_id,
"class_name": thermal_class_to_animal.get(int(class_id), 'Unknown'),
"confidence": confidence,
}
frame_detections['detections'].append(obj)
result.append(frame_detections)
# Display the annotated frame
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
cap.release()
out.release()
print(result)
return result, output_path