Spaces:
No application file
No application file
File size: 7,158 Bytes
f113e60 2fe16be f113e60 2fe16be f113e60 2fe16be f113e60 2fe16be f113e60 |
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 |
import pygame
import cv2
import time
import serial
import sys
from ultrafast.inference_onnx import AI_TRT
from classification.inference_onnx import inference_classification
from setting_AI import *
from a_utils_func_2_model import (
CLEAN_DATA_CSV_DIRECTION,
CLEAN_DATA_CSV_DIRECTION_STRAIGHT,
)
from a_control_classification import USE_CLASSIFICATION
serial_p = False
if serial_p:
serial_port = serial.Serial(
"COM8", 9600, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE
)
# Initialize camera
cap = cv2.VideoCapture("./videos/test_video.mp4")
cap_ = cv2.VideoCapture("./videos/test_video.mp4")
if serial_p:
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap_ = cv2.VideoCapture(1, cv2.CAP_DSHOW)
pygame.init()
# Screen settings
screen_width, screen_height = 1600, 900
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("AI Camera Control")
# Colors
WHITE = (240, 240, 240)
GREEN = (34, 177, 76)
RED = (200, 50, 50)
BLACK = (20, 20, 20)
GRAY = (180, 180, 180)
DARK_GRAY = (100, 100, 100)
# Fonts
font = pygame.font.Font(None, 50)
small_font = pygame.font.Font(None, 36)
# Buttons
start_button = pygame.Rect(100, 820, 220, 70)
end_button = pygame.Rect(400, 820, 220, 70)
# Slider settings
ROTATION_SPEED = 10
slider_rect = pygame.Rect(750, 850, 300, 10)
slider_knob_rect = pygame.Rect(750 + int((ROTATION_SPEED / 30) * 300) - 10, 840, 20, 30)
slider_dragging = False
DISPLAY_WIDTH = 600 # Fixed width for display
DISPLAY_HEIGHT = 400
time.sleep(1)
time_stop = sys.maxsize
sleep_time = sys.maxsize
running = True
active = False
clear = True
push_results = []
while running:
start_time = time.time()
screen.fill(WHITE)
pygame.draw.rect(screen, DARK_GRAY, (0, 800, screen_width, 100))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if start_button.collidepoint(event.pos):
active = True
clear = True
elif end_button.collidepoint(event.pos):
active = False
print("Stopped pushing")
elif slider_knob_rect.collidepoint(event.pos):
slider_dragging = True
elif event.type == pygame.MOUSEBUTTONUP:
slider_dragging = False
elif event.type == pygame.MOUSEMOTION and slider_dragging:
slider_knob_rect.x = max(
slider_rect.x, min(event.pos[0] - 10, slider_rect.x + 300 - 20)
)
ROTATION_SPEED = int(((slider_knob_rect.x - slider_rect.x) / 300) * 50)
_, frame = cap.read()
_, frame_ = cap_.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
visualization_img = frame
inference_classification(frame_)
if active:
if USE_CLASSIFICATION.check() == "STRAIGHT":
if clear:
CLEAN_DATA_CSV_DIRECTION()
CLEAN_DATA_CSV_DIRECTION_STRAIGHT()
clear = False
visualization_img, PUSH_RETURN, Have_lane = AI_TRT(
frame, paint=True, resize_img=True
)
if PUSH_RETURN:
if serial_p:
serial_port.write(PUSH_RETURN.encode())
push_results.append(PUSH_RETURN)
if len(push_results) > 5:
push_results.pop(0)
angle = min(30, int(PUSH_RETURN.split(":")[1]))
sleep_time = angle / ROTATION_SPEED
time_stop = time.time()
if time.time() - time_stop >= sleep_time:
if serial_p:
serial_port.write(PUSH_STOP.encode())
push_results.append(PUSH_STOP)
if len(push_results) > 5:
push_results.pop(0)
time_stop = sys.maxsize
else:
hard_left = "X:000"
hard_right = "Y:000"
hard_time_1 = 0.5
hard_time_2 = 1.5
hard_time_3 = 0.5
if USE_CLASSIFICATION.check() == "LEFT" and serial_p:
serial_port.write(hard_right.encode())
print(hard_right)
time.sleep(0.5)
serial_port.write(PUSH_STOP.encode())
print(PUSH_STOP)
serial_port.write(hard_left.encode())
print(hard_left)
time.sleep(1.5)
serial_port.write(PUSH_STOP.encode())
print(PUSH_STOP)
serial_port.write(hard_right.encode())
print(hard_right)
time.sleep(0.5)
serial_port.write(PUSH_STOP.encode())
print(PUSH_STOP)
USE_CLASSIFICATION.change("STRAIGHT")
if USE_CLASSIFICATION.check() == "LEFT" and serial_p:
serial_port.write(hard_left.encode())
print(hard_left)
time.sleep(0.5)
serial_port.write(PUSH_STOP.encode())
print(PUSH_STOP)
serial_port.write(hard_right.encode())
print(hard_right)
time.sleep(1.5)
serial_port.write(PUSH_STOP.encode())
print(PUSH_STOP)
serial_port.write(hard_left.encode())
print(hard_left)
time.sleep(0.5)
serial_port.write(PUSH_STOP.encode())
print(PUSH_STOP)
USE_CLASSIFICATION.change("STRAIGHT")
text_cls = font.render(USE_CLASSIFICATION.check(), True, (0, 0, 255))
text_rect = text_cls.get_rect(center=(900, 200))
screen.blit(text_cls, text_rect)
elapsed_time = time.time() - start_time
fps = 1 / elapsed_time if elapsed_time > 0 else 0
text_fps = font.render(f"FPS: {fps:.2f}", True, (0, 0, 255))
text_rect = text_fps.get_rect(center=(900, 250))
screen.blit(text_fps, text_rect)
visualization_img = cv2.resize(
visualization_img,
# (visualization_img.shape[1] // 2, visualization_img.shape[0] // 2),
(DISPLAY_WIDTH, DISPLAY_HEIGHT),
)
pygame_frame = pygame.surfarray.make_surface(
cv2.rotate(cv2.flip(visualization_img, 1), cv2.ROTATE_90_COUNTERCLOCKWISE)
)
screen.blit(pygame_frame, (10, 10))
# Buttons
pygame.draw.rect(screen, GREEN if active else GRAY, start_button, border_radius=15)
pygame.draw.rect(screen, RED, end_button, border_radius=15)
screen.blit(
font.render("Start", True, WHITE), (start_button.x + 70, start_button.y + 20)
)
screen.blit(font.render("End", True, WHITE), (end_button.x + 80, end_button.y + 20))
# Slider
pygame.draw.rect(screen, GRAY, slider_rect, border_radius=5)
pygame.draw.ellipse(screen, BLACK, slider_knob_rect)
screen.blit(font.render(f"Speed: {ROTATION_SPEED}", True, WHITE), (1080, 820))
# Display push results
for i, result in enumerate(reversed(push_results)):
screen.blit(small_font.render(result, True, BLACK), (1200, 600 - i * 40))
pygame.display.flip()
cap.release()
cv2.destroyAllWindows()
pygame.quit()
|