-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (86 loc) · 2.96 KB
/
main.py
File metadata and controls
111 lines (86 loc) · 2.96 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
105
106
107
108
109
110
111
import cv2
import numpy as np
from motion import MotionDetector
from flow import OpticalFlowEstimator
from features import FeatureExtractor
from decision import IntentDecision
from visualize import draw_intent
MIN_CONTOUR_AREA = 800
COLLISION_AREA_THRESHOLD = 9000 # heuristic
def summarize_scene(intents):
"""
Priority-based scene-level decision.
Higher risk always overrides lower risk.
"""
if "COLLISION_RISK" in intents:
return " COLLISION RISK", (0, 0, 255)
elif "APPROACHING" in intents:
return "APPROACHING OBJECT", (0, 0, 255)
elif "CROSSING" in intents:
return "OBJECT CROSSING", (0, 255, 255)
else:
return "MOVING AWAY / SAFE", (0, 255, 0)
def main():
cap = cv2.VideoCapture(0)
motion_detector = MotionDetector()
flow_estimator = OpticalFlowEstimator()
feature_extractor = FeatureExtractor()
intent_decider = IntentDecision()
while True:
ret, frame = cap.read()
if not ret:
break
motion_mask = motion_detector.detect(frame)
mag, ang = flow_estimator.compute(frame)
# Collect intents for scene-level summary
scene_intents = []
if motion_mask is not None and mag is not None:
contours, _ = cv2.findContours(
motion_mask,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE
)
for cnt in contours:
area = cv2.contourArea(cnt)
if area < MIN_CONTOUR_AREA:
continue
x, y, w, h = cv2.boundingRect(cnt)
# Feature extraction
features = feature_extractor.extract(
(x, y, w, h),
mag,
ang
)
# ML intent prediction
intent, confidence = intent_decider.predict(features)
# Collision risk logic
if intent == "APPROACHING" and (w * h) > COLLISION_AREA_THRESHOLD:
intent = "COLLISION_RISK"
# Store intent for scene-level decision
scene_intents.append(intent)
# Object-level visualization (boxes remain unchanged)
draw_intent(
frame,
(x, y, w, h),
intent,
confidence
)
# Scene-level conclusion (ONE clear message)
if scene_intents:
summary_text, summary_color = summarize_scene(scene_intents)
cv2.putText(
frame,
summary_text,
(30, 45),
cv2.FONT_HERSHEY_SIMPLEX,
1.1,
summary_color,
3
)
cv2.imshow("Predictive Motion Intent System", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()