ABAO77 commited on
Commit
2fe16be
·
1 Parent(s): f09998e

update model and have bypass frame file

Browse files
Files changed (2) hide show
  1. app.py +6 -4
  2. app_bypass_frame.py +331 -0
app.py CHANGED
@@ -57,7 +57,8 @@ slider_rect = pygame.Rect(750, 850, 300, 10)
57
  slider_knob_rect = pygame.Rect(750 + int((ROTATION_SPEED / 30) * 300) - 10, 840, 20, 30)
58
  slider_dragging = False
59
 
60
-
 
61
  time.sleep(1)
62
 
63
  time_stop = sys.maxsize
@@ -135,7 +136,7 @@ while running:
135
  hard_time_2 = 1.5
136
  hard_time_3 = 0.5
137
 
138
- if USE_CLASSIFICATION.check() == "LEFT":
139
  serial_port.write(hard_right.encode())
140
  print(hard_right)
141
  time.sleep(0.5)
@@ -156,7 +157,7 @@ while running:
156
 
157
  USE_CLASSIFICATION.change("STRAIGHT")
158
 
159
- if USE_CLASSIFICATION.check() == "LEFT":
160
  serial_port.write(hard_left.encode())
161
  print(hard_left)
162
  time.sleep(0.5)
@@ -190,7 +191,8 @@ while running:
190
 
191
  visualization_img = cv2.resize(
192
  visualization_img,
193
- (visualization_img.shape[1] // 2, visualization_img.shape[0] // 2),
 
194
  )
195
 
196
  pygame_frame = pygame.surfarray.make_surface(
 
57
  slider_knob_rect = pygame.Rect(750 + int((ROTATION_SPEED / 30) * 300) - 10, 840, 20, 30)
58
  slider_dragging = False
59
 
60
+ DISPLAY_WIDTH = 600 # Fixed width for display
61
+ DISPLAY_HEIGHT = 400
62
  time.sleep(1)
63
 
64
  time_stop = sys.maxsize
 
136
  hard_time_2 = 1.5
137
  hard_time_3 = 0.5
138
 
139
+ if USE_CLASSIFICATION.check() == "LEFT" and serial_p:
140
  serial_port.write(hard_right.encode())
141
  print(hard_right)
142
  time.sleep(0.5)
 
157
 
158
  USE_CLASSIFICATION.change("STRAIGHT")
159
 
160
+ if USE_CLASSIFICATION.check() == "LEFT" and serial_p:
161
  serial_port.write(hard_left.encode())
162
  print(hard_left)
163
  time.sleep(0.5)
 
191
 
192
  visualization_img = cv2.resize(
193
  visualization_img,
194
+ # (visualization_img.shape[1] // 2, visualization_img.shape[0] // 2),
195
+ (DISPLAY_WIDTH, DISPLAY_HEIGHT),
196
  )
197
 
198
  pygame_frame = pygame.surfarray.make_surface(
app_bypass_frame.py ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pygame
2
+ import cv2
3
+ import time
4
+ import serial
5
+ import sys
6
+
7
+ from ultrafast.inference_onnx import AI_TRT
8
+ from classification.inference_onnx import inference_classification
9
+ from setting_AI import *
10
+ from a_utils_func_2_model import (
11
+ CLEAN_DATA_CSV_DIRECTION,
12
+ CLEAN_DATA_CSV_DIRECTION_STRAIGHT,
13
+ )
14
+ from a_control_classification import USE_CLASSIFICATION
15
+
16
+ # Added threshold parameters
17
+ INFERENCE_THRESHOLD = 5 # Run inference every X frames
18
+ LIGHT_THRESHOLD = 50 # Light level threshold (0-255)
19
+ MOTION_THRESHOLD = 1000 # Motion detection threshold
20
+
21
+ serial_p = False
22
+ if serial_p:
23
+ serial_port = serial.Serial(
24
+ "COM8", 9600, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE
25
+ )
26
+
27
+ # Initialize camera
28
+ cap = cv2.VideoCapture("./videos/test_video.mp4")
29
+ cap_ = cv2.VideoCapture("./videos/test_video.mp4")
30
+ if serial_p:
31
+ cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
32
+ cap_ = cv2.VideoCapture(1, cv2.CAP_DSHOW)
33
+
34
+ pygame.init()
35
+
36
+ # Screen settings
37
+ screen_width, screen_height = 1600, 900
38
+ screen = pygame.display.set_mode((screen_width, screen_height))
39
+ pygame.display.set_caption("AI Camera Control")
40
+
41
+ # Colors
42
+ WHITE = (240, 240, 240)
43
+ GREEN = (34, 177, 76)
44
+ RED = (200, 50, 50)
45
+ BLACK = (20, 20, 20)
46
+ GRAY = (180, 180, 180)
47
+ DARK_GRAY = (100, 100, 100)
48
+
49
+ # Fonts
50
+ font = pygame.font.Font(None, 50)
51
+ small_font = pygame.font.Font(None, 36)
52
+
53
+ # Buttons
54
+ start_button = pygame.Rect(100, 820, 220, 70)
55
+ end_button = pygame.Rect(400, 820, 220, 70)
56
+
57
+ # Slider settings
58
+ ROTATION_SPEED = 10
59
+ slider_rect = pygame.Rect(750, 850, 300, 10)
60
+ slider_knob_rect = pygame.Rect(750 + int((ROTATION_SPEED / 30) * 300) - 10, 840, 20, 30)
61
+ slider_dragging = False
62
+
63
+ # Threshold controls
64
+ threshold_slider_rect = pygame.Rect(750, 780, 300, 10)
65
+ threshold_knob_rect = pygame.Rect(
66
+ 750 + int((INFERENCE_THRESHOLD / 30) * 300) - 10, 770, 20, 30
67
+ )
68
+ threshold_dragging = False
69
+
70
+ time.sleep(1)
71
+
72
+ time_stop = sys.maxsize
73
+ sleep_time = sys.maxsize
74
+ running = True
75
+ active = False
76
+ clear = True
77
+ push_results = []
78
+
79
+ # Variables for threshold bypass
80
+ frame_count = 0
81
+ prev_frame = None
82
+ last_result = None
83
+ cached_visualization_img = None # Store the last visualization image
84
+ DISPLAY_WIDTH = 600 # Fixed width for display
85
+ DISPLAY_HEIGHT = 400
86
+ while running:
87
+ start_time = time.time()
88
+ screen.fill(WHITE)
89
+ pygame.draw.rect(screen, DARK_GRAY, (0, 800, screen_width, 100))
90
+
91
+ for event in pygame.event.get():
92
+ if event.type == pygame.QUIT:
93
+ running = False
94
+ elif event.type == pygame.MOUSEBUTTONDOWN:
95
+ if start_button.collidepoint(event.pos):
96
+ active = True
97
+ clear = True
98
+ elif end_button.collidepoint(event.pos):
99
+ active = False
100
+ print("Stopped pushing")
101
+ elif slider_knob_rect.collidepoint(event.pos):
102
+ slider_dragging = True
103
+ elif threshold_knob_rect.collidepoint(event.pos):
104
+ threshold_dragging = True
105
+ elif event.type == pygame.MOUSEBUTTONUP:
106
+ slider_dragging = False
107
+ threshold_dragging = False
108
+ elif event.type == pygame.MOUSEMOTION:
109
+ if slider_dragging:
110
+ slider_knob_rect.x = max(
111
+ slider_rect.x, min(event.pos[0] - 10, slider_rect.x + 300 - 20)
112
+ )
113
+ ROTATION_SPEED = int(((slider_knob_rect.x - slider_rect.x) / 300) * 50)
114
+ elif threshold_dragging:
115
+ threshold_knob_rect.x = max(
116
+ threshold_slider_rect.x,
117
+ min(event.pos[0] - 10, threshold_slider_rect.x + 300 - 20),
118
+ )
119
+ INFERENCE_THRESHOLD = max(
120
+ 1,
121
+ int(((threshold_knob_rect.x - threshold_slider_rect.x) / 300) * 30),
122
+ )
123
+
124
+ _, frame = cap.read()
125
+ _, frame_ = cap_.read()
126
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
127
+ visualization_img = frame.copy() # Default to current frame
128
+
129
+ inference_classification(frame_)
130
+
131
+ skip_inference = False
132
+ # Check if we should skip inference based on frame count
133
+ frame_count += 1
134
+
135
+ # Calculate brightness
136
+ brightness = cv2.mean(frame)[0] # Get average brightness
137
+
138
+ # Motion detection (if we have a previous frame)
139
+ motion_level = 0
140
+ if prev_frame is not None:
141
+ frame_diff = cv2.absdiff(
142
+ cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY),
143
+ cv2.cvtColor(prev_frame, cv2.COLOR_RGB2GRAY),
144
+ )
145
+ motion_level = cv2.countNonZero(
146
+ cv2.threshold(frame_diff, 25, 255, cv2.THRESH_BINARY)[1]
147
+ )
148
+
149
+ # Store current frame for next iteration
150
+ prev_frame = frame.copy()
151
+
152
+ # Decide whether to skip inference based on multiple criteria
153
+ if frame_count % INFERENCE_THRESHOLD != 0:
154
+ skip_inference = True
155
+
156
+
157
+ status_text = "INFERENCE" if not skip_inference else "SKIPPED"
158
+
159
+ if active:
160
+ if USE_CLASSIFICATION.check() == "STRAIGHT":
161
+ if clear:
162
+ CLEAN_DATA_CSV_DIRECTION()
163
+ CLEAN_DATA_CSV_DIRECTION_STRAIGHT()
164
+ clear = False
165
+
166
+ if not skip_inference:
167
+ # Run inference
168
+ visualization_img, PUSH_RETURN, Have_lane = AI_TRT(
169
+ frame, paint=True, resize_img=True
170
+ )
171
+ # Cache the visualization image for use when skipping frames
172
+ cached_visualization_img = visualization_img.copy()
173
+ last_result = PUSH_RETURN
174
+
175
+ if PUSH_RETURN:
176
+ if serial_p:
177
+ serial_port.write(PUSH_RETURN.encode())
178
+ push_results.append(PUSH_RETURN)
179
+ if len(push_results) > 5:
180
+ push_results.pop(0)
181
+ angle = min(30, int(PUSH_RETURN.split(":")[1]))
182
+ sleep_time = angle / ROTATION_SPEED
183
+ time_stop = time.time()
184
+ elif cached_visualization_img is not None:
185
+ # Use the cached visualization image when skipping inference
186
+ visualization_img = cached_visualization_img.copy()
187
+
188
+ # Add "Cached" indicator to the visualization
189
+ cv2.putText(
190
+ visualization_img,
191
+ "CACHED VIEW",
192
+ (50, 50),
193
+ cv2.FONT_HERSHEY_SIMPLEX,
194
+ 1,
195
+ (255, 0, 0),
196
+ 2,
197
+ )
198
+
199
+ # Continue using the last command if within sleep time
200
+ if last_result and time.time() - time_stop < sleep_time:
201
+ # Continue using the last command
202
+ pass
203
+ else:
204
+ if serial_p:
205
+ serial_port.write(PUSH_STOP.encode())
206
+ push_results.append(PUSH_STOP)
207
+ if len(push_results) > 5:
208
+ push_results.pop(0)
209
+ time_stop = sys.maxsize
210
+
211
+ if time.time() - time_stop >= sleep_time:
212
+ if serial_p:
213
+ serial_port.write(PUSH_STOP.encode())
214
+ push_results.append(PUSH_STOP)
215
+ if len(push_results) > 5:
216
+ push_results.pop(0)
217
+ time_stop = sys.maxsize
218
+
219
+ else:
220
+ hard_left = "X:000"
221
+ hard_right = "Y:000"
222
+
223
+ hard_time_1 = 0.5
224
+ hard_time_2 = 1.5
225
+ hard_time_3 = 0.5
226
+
227
+ if USE_CLASSIFICATION.check() == "LEFT" and serial_p:
228
+ serial_port.write(hard_right.encode())
229
+ print(hard_right)
230
+ time.sleep(0.5)
231
+ serial_port.write(PUSH_STOP.encode())
232
+ print(PUSH_STOP)
233
+
234
+ serial_port.write(hard_left.encode())
235
+ print(hard_left)
236
+ time.sleep(1.5)
237
+ serial_port.write(PUSH_STOP.encode())
238
+ print(PUSH_STOP)
239
+
240
+ serial_port.write(hard_right.encode())
241
+ print(hard_right)
242
+ time.sleep(0.5)
243
+ serial_port.write(PUSH_STOP.encode())
244
+ print(PUSH_STOP)
245
+
246
+ USE_CLASSIFICATION.change("STRAIGHT")
247
+
248
+ if USE_CLASSIFICATION.check() == "RIGHT" and serial_p:
249
+ serial_port.write(hard_left.encode())
250
+ print(hard_left)
251
+ time.sleep(0.5)
252
+ serial_port.write(PUSH_STOP.encode())
253
+ print(PUSH_STOP)
254
+
255
+ serial_port.write(hard_right.encode())
256
+ print(hard_right)
257
+ time.sleep(1.5)
258
+ serial_port.write(PUSH_STOP.encode())
259
+ print(PUSH_STOP)
260
+
261
+ serial_port.write(hard_left.encode())
262
+ print(hard_left)
263
+ time.sleep(0.5)
264
+ serial_port.write(PUSH_STOP.encode())
265
+ print(PUSH_STOP)
266
+
267
+ USE_CLASSIFICATION.change("STRAIGHT")
268
+
269
+ text_cls = font.render(USE_CLASSIFICATION.check(), True, (0, 0, 255))
270
+ text_rect = text_cls.get_rect(center=(900, 200))
271
+ screen.blit(text_cls, text_rect)
272
+
273
+ # Display inference status
274
+ text_status = font.render(f"Status: {status_text}", True, (0, 0, 255))
275
+ text_status_rect = text_status.get_rect(center=(900, 150))
276
+ screen.blit(text_status, text_status_rect)
277
+
278
+ elapsed_time = time.time() - start_time
279
+ fps = 1 / elapsed_time if elapsed_time > 0 else 0
280
+
281
+ text_fps = font.render(f"FPS: {fps:.2f}", True, (0, 0, 255))
282
+ text_rect = text_fps.get_rect(center=(900, 250))
283
+ screen.blit(text_fps, text_rect)
284
+
285
+ visualization_img = cv2.resize(
286
+ visualization_img,
287
+ # (visualization_img.shape[1] // 2, visualization_img.shape[0] // 2),
288
+ (DISPLAY_WIDTH, DISPLAY_HEIGHT),
289
+ )
290
+
291
+ pygame_frame = pygame.surfarray.make_surface(
292
+ cv2.rotate(cv2.flip(visualization_img, 1), cv2.ROTATE_90_COUNTERCLOCKWISE)
293
+ )
294
+ screen.blit(pygame_frame, (10, 10))
295
+
296
+ # Buttons
297
+ pygame.draw.rect(screen, GREEN if active else GRAY, start_button, border_radius=15)
298
+ pygame.draw.rect(screen, RED, end_button, border_radius=15)
299
+ screen.blit(
300
+ font.render("Start", True, WHITE), (start_button.x + 70, start_button.y + 20)
301
+ )
302
+ screen.blit(font.render("End", True, WHITE), (end_button.x + 80, end_button.y + 20))
303
+
304
+ # Speed Slider
305
+ pygame.draw.rect(screen, GRAY, slider_rect, border_radius=5)
306
+ pygame.draw.ellipse(screen, BLACK, slider_knob_rect)
307
+ screen.blit(font.render(f"Speed: {ROTATION_SPEED}", True, WHITE), (1080, 820))
308
+
309
+ # Threshold Slider
310
+ pygame.draw.rect(screen, GRAY, threshold_slider_rect, border_radius=5)
311
+ pygame.draw.ellipse(screen, BLACK, threshold_knob_rect)
312
+ screen.blit(
313
+ font.render(f"Inference Every: {INFERENCE_THRESHOLD} frames", True, WHITE),
314
+ (1080, 770),
315
+ )
316
+
317
+ # Additional data display
318
+ screen.blit(
319
+ small_font.render(f"Brightness: {brightness:.1f}", True, WHITE), (600, 720)
320
+ )
321
+ screen.blit(small_font.render(f"Motion: {motion_level}", True, WHITE), (600, 760))
322
+
323
+ # Display push results
324
+ for i, result in enumerate(reversed(push_results)):
325
+ screen.blit(small_font.render(result, True, BLACK), (1200, 600 - i * 40))
326
+
327
+ pygame.display.flip()
328
+
329
+ cap.release()
330
+ cv2.destroyAllWindows()
331
+ pygame.quit()