-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_detection_output.py
More file actions
60 lines (46 loc) · 1.49 KB
/
visualize_detection_output.py
File metadata and controls
60 lines (46 loc) · 1.49 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
59
60
from ultralytics import YOLO
import cv2
import os
# class_names = [
# '대형차_불법차량', '대형차_정상차량',
# '중형차_불법차량', '중형차_정상차량',
# '소형차_불법차량', '소형차_정상차량'
# ]
class_names = ['0', '1',
'2', '3',
'4', '5']
save_dir = 'saved_rois'
os.makedirs(save_dir, exist_ok=True)
model = YOLO("best.pt")
model.to('cpu')
video_path = "MBC_re.mp4"
cap = cv2.VideoCapture(video_path)
frame_count = 0
roi_count = 0 # 저장 ROI 이미지
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
results = model.predict(frame, conf=0.5)[0]
for box in results.boxes:
conf = float(box.conf[0])
cls = int(box.cls[0])
if cls not in [0, 2, 4]: # 불법 차량 클래스만 고려
continue
x1, y1, x2, y2 = map(int, box.xyxy[0])
label = f'{class_names[cls]} {conf:.2f}'
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, label, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# ROI 이미지 자르고 저장
roi = frame[y1:y2, x1:x2]
if roi.size > 0:
filename = f'{save_dir}/roi_{roi_count}.jpg'
cv2.imwrite(filename, roi)
roi_count += 1
cv2.imshow('Detection Output', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
frame_count += 1
cap.release()
cv2.destroyAllWindows()