Repository navigation
Expand file tree
/
Copy pathimproved1.py
More file actions
257 lines (191 loc) · 6.46 KB
/
Copy pathimproved1.py
File metadata and controls
257 lines (191 loc) · 6.46 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import cv2
import numpy as np
import time
import platform
from ultralytics import YOLO
import serial
ser = serial.Serial('/dev/ttyAMA0', 115200)
def send(cmd):
msg = f"{cmd[0]}\n"
if ser:
try:
ser.write(msg.encode())
except:
pass
print("CMD:", msg.strip())
# ---------------- CONFIG ----------------
WIDTH = 640
HEIGHT = 480
ROI_Y_START = int(HEIGHT * 0.3)
DIST_CONST = 550
DIST_THRESHOLD = 180 # earlier detection
EMERGENCY_DIST = 90 # immediate reverse
MIN_AREA = 900 # more sensitive motion
ALPHA = 0.9
steer_smoothed = 0
last_action = "FORWARD"
# ---------------- DISTANCE ----------------
def estimate_distance(h):
if h <= 0:
return 999
return DIST_CONST / h
# ---------------- INIT ----------------
print("Loading YOLO...")
model = YOLO("yolov8n.pt")
print("Model loaded")
# ---------------- CAMERA ----------------
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("ERROR: Camera not opening")
exit()
cap.set(3, WIDTH)
cap.set(4, HEIGHT)
time.sleep(2)
# ---------------- BACKGROUND ----------------
ret, prev_frame = cap.read()
if not ret:
print("Cannot read camera")
exit()
prev_frame = prev_frame[ROI_Y_START:HEIGHT]
prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
prev_gray = cv2.GaussianBlur(prev_gray, (21, 21), 0)
print("ADAS Running (FAST MODE)")
# ---------------- LOOP ----------------
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
continue
frame = cv2.resize(frame, (WIDTH, HEIGHT))
roi = frame[ROI_Y_START:HEIGHT]
# ================= MOTION DETECTION =================
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
diff = cv2.absdiff(prev_gray, gray)
thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
motion_boxes = []
for cnt in contours:
if cv2.contourArea(cnt) < MIN_AREA:
continue
x, y, w, h = cv2.boundingRect(cnt)
motion_boxes.append((x, y + ROI_Y_START, w, h))
prev_gray = gray
# ================= YOLO (SKIPPED FRAMES FOR SPEED) =================
frame_count += 1
if frame_count % 2 == 0:
results = model(frame, conf=0.45, imgsz=256, verbose=False)
else:
results = []
human_boxes = []
obstacle_boxes = []
for r in results:
for box in r.boxes:
cls = int(box.cls[0])
conf = float(box.conf[0])
if conf < 0.45:
continue
x1, y1, x2, y2 = map(int, box.xyxy[0])
w = x2 - x1
h = y2 - y1
if h < 60:
continue
if cls == 0:
human_boxes.append((x1, y1, w, h))
elif cls in [1, 2, 3, 5, 7]:
obstacle_boxes.append((x1, y1, w, h))
# ================= MERGE =================
all_boxes = []
for (hx, hy, hw, hh) in human_boxes:
for (mx, my, mw, mh) in motion_boxes:
if (hx < mx + mw and hx + hw > mx and hy < my + mh and hy + hh > my):
all_boxes.append(("HUMAN", (hx, hy, hw, hh)))
break
for (ox, oy, ow, oh) in obstacle_boxes:
all_boxes.append(("OBSTACLE", (ox, oy, ow, oh)))
for b in motion_boxes:
all_boxes.append(("MOTION", b))
# ================= STEERING =================
steer = 0
min_dist = 999
left_blocked = False
center_blocked = False
right_blocked = False
for label, (x, y, w, h) in all_boxes:
dist = estimate_distance(h)
if label == "HUMAN":
dist *= 0.85
cx = x + w // 2
# Region classification
if cx < WIDTH // 3:
region = "LEFT"
elif cx > 2 * WIDTH // 3:
region = "RIGHT"
else:
region = "CENTER"
if dist < DIST_THRESHOLD:
if region == "LEFT":
left_blocked = True
elif region == "RIGHT":
right_blocked = True
else:
center_blocked = True
# Steering influence
offset = (cx - WIDTH // 2) / (WIDTH // 2)
weight = 2.5 / (dist + 1e-5)
if label == "HUMAN":
weight *= 1.5
elif label == "MOTION":
weight *= 2.0 # fast reaction
steer += offset * weight
min_dist = min(min_dist, dist)
# Draw
color = (0, 255, 0) if label == "HUMAN" else (255, 0, 0) if label == "OBSTACLE" else (0, 0, 255)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
# ================= SMOOTH =================
steer_smoothed = ALPHA * steer_smoothed + (1 - ALPHA) * steer
steer_final = steer_smoothed * 0.6
# ================= DECISION =================
if min_dist < EMERGENCY_DIST:
action = "BACK"
elif min_dist < DIST_THRESHOLD:
if center_blocked:
if not left_blocked:
action = "LEFT"
elif not right_blocked:
action = "RIGHT"
else:
action = "BACK"
elif left_blocked:
action = "RIGHT"
elif right_blocked:
action = "LEFT"
else:
action = "FORWARD"
else:
if steer_final > 0.2:
action = "RIGHT"
elif steer_final < -0.2:
action = "LEFT"
else:
action = "FORWARD"
# ================= SEND =================
if action != last_action:
send(action)
last_action = action
print("lasttt",action)
# ================= DISPLAY =================
cv2.putText(frame, f"STEER: {steer_final:.3f}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
cv2.putText(frame, f"ACTION: {action}", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
cv2.line(frame, (0, ROI_Y_START), (WIDTH, ROI_Y_START), (255, 255, 0), 2)
cv2.imshow("ADAS System (FAST)", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# ---------------- CLEANUP ----------------
cap.release()
cv2.destroyAllWindows()
if ser:
ser.close()