-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (78 loc) · 2.18 KB
/
Copy pathmain.py
File metadata and controls
104 lines (78 loc) · 2.18 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
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
"""
main.py
"""
import signal
import time
import cv2
import utils
import camera
import detection
import storage
import stream
from config.settings import INFER_EVERY, FRAME_DELAY
#startup
utils.validate()
utils.report()
session = storage.Session()
writer = storage.DetectionWriter(session)
model = detection.load_model()
cam = camera.open_camera()
flask_active = stream.start()
show_gui = utils.show_preview()
if show_gui:
cv2.namedWindow("YOLO Live", cv2.WINDOW_NORMAL)
print("Detection running - press Ctrl+C to stop\n")
_running = True
def _shutdown(sig=None, frame=None):
global _running
_running = False
signal.signal(signal.SIGTERM, _shutdown)
signal.signal(signal.SIGINT, _shutdown)
# main loop
frame_id = 0
infer_tick = 0
try:
while _running:
frame_id += 1
infer_tick += 1
# 1. Capture
raw_frame = cam.read()
if raw_frame is None:
if FRAME_DELAY > 0:
time.sleep(FRAME_DELAY)
continue
# 2. Skip inference on intermediate frames
if infer_tick < INFER_EVERY:
if flask_active:
stream.update_frame(raw_frame)
if FRAME_DELAY > 0:
time.sleep(FRAME_DELAY)
continue
infer_tick = 0
# 3. Pre-process
rgb_frame = detection.preprocess(raw_frame)
# 4. Inference
detections, annotated = detection.run_inference(model, rgb_frame)
# 5. Update stream
if flask_active:
stream.update_frame(annotated)
# 6. GUI preview
if show_gui:
cv2.imshow("YOLO Live", annotated)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# 7. Debug print
if not detections.empty:
print(detections[["name", "confidence"]].to_string(index=False))
else:
print(f"[frame {frame_id}] no detections")
# 8. Save
writer.save_detection(frame_id, raw_frame, annotated, detections)
if FRAME_DELAY > 0:
time.sleep(FRAME_DELAY)
#clean shutdown
finally:
cam.release()
writer.flush()
cv2.destroyAllWindows()
print("Clean exit")