import cv2
import mediapipe as mp
import time
import threading
import [Link] as GPIO
# GPIO setup
[Link]([Link])
BRAKE_GPIO = 5
BUZZER_GPIO = 17
BYPASS_GPIO = 6 # Button for bypass mode
[Link](BRAKE_GPIO, [Link])
[Link](BUZZER_GPIO, [Link])
[Link](BYPASS_GPIO, [Link], pull_up_down=GPIO.PUD_UP) # Pull-up resistor
[Link](BRAKE_GPIO, [Link])
mp_pose = [Link]
cap = [Link]('/dev/video0')
[Link](3, 640) # Lower resolution for faster processing
[Link](4, 480)
[Link](cv2.CAP_PROP_FPS, 30)
frame = None
frame_lock = [Link]()
stop_threads = False
# Capture frames from the camera
def capture_frames():
global frame
while not stop_threads:
ret, f = [Link]()
if ret:
with frame_lock:
frame = [Link]()
[Link](0.005) # Reduce delay for real-time capture
capture_thread = [Link](target=capture_frames, daemon=True)
capture_thread.start()
last_human_detected_time = 0
brakes_on = False
with mp_pose.Pose(min_detection_confidence=0.7, min_tracking_confidence=0.6) as
pose:
prev_frame_time = [Link]()
frame_skip = 1 # Skip every second frame
frame_count = 0
while True:
frame_count += 1
if frame_count % frame_skip != 0:
continue # Skip frame processing
with frame_lock:
if frame is None:
continue
frame_copy = [Link]()
# Check if bypass button is pressed
bypass_mode = [Link](BYPASS_GPIO) == [Link] # Button pressed = LOW
if bypass_mode:
brakes_on = False # Force brakes OFF in bypass mode
else:
frame_rgb = [Link](frame_copy, cv2.COLOR_BGR2RGB)
results = [Link](frame_rgb)
if results.pose_landmarks:
visible_landmarks = [lm for lm in results.pose_landmarks.landmark
if [Link] > 0.75]
if len(visible_landmarks) > 12:
padding = 30 # Expand detection area
x_coords = [lm.x for lm in visible_landmarks]
y_coords = [lm.y for lm in visible_landmarks]
min_x = max(0, int(min(x_coords) * frame_copy.shape[1]) -
padding)
max_x = min(frame_copy.shape[1], int(max(x_coords) *
frame_copy.shape[1]) + padding)
min_y = max(0, int(min(y_coords) * frame_copy.shape[0]) -
padding)
max_y = min(frame_copy.shape[0], int(max(y_coords) *
frame_copy.shape[0]) + padding)
[Link](frame_copy, (min_x, min_y), (max_x, max_y), (0,
255, 0), 3)
brakes_on = True # Apply brakes
last_human_detected_time = [Link]()
elif [Link]() - last_human_detected_time >= 1:
brakes_on = False # Delay brake release
# Update brake output
[Link](BRAKE_GPIO, [Link] if brakes_on else [Link])
[Link](BUZZER_GPIO, [Link] if brakes_on else [Link])
# Display brake status
brake_status = "Brakes ON" if brakes_on else "Brakes OFF"
bypass_text = "BYPASS ACTIVE" if bypass_mode else ""
color = (0, 0, 255) if brakes_on else (0, 255, 0)
[Link](frame_copy, brake_status, (20, 40), cv2.FONT_HERSHEY_SIMPLEX,
1, color, 2)
[Link](frame_copy, bypass_text, (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 0), 2)
# Calculate and display FPS
new_frame_time = [Link]()
fps = 1 / (new_frame_time - prev_frame_time) if new_frame_time -
prev_frame_time > 0 else 0
prev_frame_time = new_frame_time
fps_text = f"FPS: {int(fps)}"
[Link](frame_copy, fps_text, (20, 120), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 255, 255), 2)
[Link]('Webcam Pose Detection', frame_copy)
if [Link](1) & 0xFF == ord('q'):
break
stop_threads = True
capture_thread.join()
[Link]()
[Link]()
[Link]()