Skip to content

Commit c51d379

Browse files
committed
Added necessary files
1 parent a7939e7 commit c51d379

8 files changed

+270
-0
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Ambulance Detection Using openCV
2+
Enables green light and navigation to nearest hospital
3+
4+
## Introduction
5+
Pre Trained the model with 4000+ ambulance images with YoloV5 for ambulance detection. For this setup we used the IP camera of mobile using droid cam app. This python script will detect the ambulance with confident>0.80(probability) and send the ssh command to raspberry pi to turn ON the green light.
6+
7+
## Screenshots of ambulance detection
8+
9+
![Screenshot from 2024-10-17 02-02-49](https://github.com/user-attachments/assets/15438382-25f8-4090-9ef6-c4500bb20e6f)
10+
11+
![Screenshot from 2024-10-20 18-45-09](https://github.com/user-attachments/assets/c48c77c5-ee1f-4b53-99cc-2616bee8a3ec)
12+
13+
14+
15+
## Screenshots of Navigation to nearest hospital
16+
17+
![navigation UI](https://github.com/user-attachments/assets/a4c1775b-1a1f-4e62-a4e5-c75327249775)
18+
19+
20+
![navigation1](https://github.com/user-attachments/assets/d402f026-8c52-4360-abb2-1e112bffb36e)
21+
22+
23+
![navigation2](https://github.com/user-attachments/assets/752c67ee-a3f5-4cc4-b8a0-11290da6ef50)
24+
25+
# Video of navigation
26+
27+
28+
29+
https://github.com/user-attachments/assets/b3c14834-3937-4829-8a81-ef60e2bb1dfd
30+

detection.py

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import cv2
2+
import torch
3+
import numpy as np
4+
import time
5+
import queue
6+
import threading
7+
import paramiko
8+
9+
device = torch.device('cpu')
10+
11+
12+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='/home/lotus/Downloads/Ambulance-detection-and-navigation/model/best.pt', device=device)
13+
model.conf = 0.75 #confidential probability
14+
model.iou = 0.45
15+
16+
ip_cam_url = "http://192.168.188.253:4747/video" #Use Droid cam
17+
18+
19+
frame_queue = queue.Queue(maxsize=2)
20+
display_frame_queue = queue.Queue(maxsize=2)
21+
22+
23+
stop_event = threading.Event()
24+
FRAME_SKIP = 2
25+
TARGET_WIDTH = 640
26+
TARGET_HEIGHT = 480
27+
# Raspberry Pi SSH connection info
28+
RASPBERRY_PI_IP = "192.168.188.22" # Replace with your Raspberry Pi's IP address
29+
USERNAME = "rasukutty" # Replace with your Raspberry Pi's username
30+
PASSWORD = "mjt" # Replace with your Raspberry Pi's password
31+
REMOTE_SCRIPT_PATH = "/home/rasukutty/run_script.py" # Replace with your script path on the Raspberry Pi
32+
SCRIPT_PATH = "/home/rasukutty/run_script.py"
33+
RUNNING_SCRIPT = "run.py"
34+
BUZZER_SCRIPT = "/home/rasukutty/buzzer.py"
35+
36+
ssh = paramiko.SSHClient()
37+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
38+
ssh.connect(RASPBERRY_PI_IP, username=USERNAME, password=PASSWORD)
39+
40+
def send_command_to_raspberry_pi():
41+
"""Send a command to the Raspberry Pi to run a Python script."""
42+
try:
43+
# Command to run the script on Raspberry Pi
44+
command = f"python3 {REMOTE_SCRIPT_PATH}"
45+
46+
# Execute the command
47+
stdin, stdout, stderr = ssh.exec_command(command)
48+
print("Script executed on Raspberry Pi.")
49+
50+
# Optionally print the output
51+
output = stdout.read().decode()
52+
error = stderr.read().decode()
53+
print(f"Output: {output}")
54+
print(f"Error: {error}")
55+
56+
except Exception as e:
57+
print(f"Failed to send command to Raspberry Pi: {e}")
58+
59+
# Timer for detection
60+
last_detected_time = None
61+
detection_duration_threshold = 1 # Time in seconds
62+
63+
def capture_frames(cap):
64+
frame_count = 0
65+
while not stop_event.is_set():
66+
ret, frame = cap.read()
67+
if ret:
68+
frame_count += 1
69+
if frame_count % FRAME_SKIP == 0:
70+
frame = cv2.resize(frame, (TARGET_WIDTH, TARGET_HEIGHT))
71+
if frame_queue.full():
72+
frame_queue.get()
73+
frame_queue.put(frame)
74+
else:
75+
print("Failed to get frame from camera. Retrying...")
76+
time.sleep(0.1)
77+
78+
def detect_ambulance(frame):
79+
global last_detected_time
80+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
81+
82+
# Perform detection
83+
results = model(rgb_frame, size=TARGET_WIDTH)
84+
85+
# Process results
86+
detected_now = False
87+
for det in results.xyxy[0]: # det is (x1, y1, x2, y2, conf, cls)
88+
class_id = int(det[5])
89+
confidence = float(det[4])
90+
91+
if model.names[class_id] in ['ambulance', 'emergency-vehicle'] and confidence >= 0.80:
92+
label = f'{model.names[class_id]} {confidence:.2f}'
93+
color = (0, 255, 0) # Green for bounding box
94+
c1, c2 = (int(det[0]), int(det[1])), (int(det[2]), int(det[3]))
95+
cv2.rectangle(frame, c1, c2, color, 2)
96+
cv2.putText(frame, label, (c1[0], c1[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
97+
98+
# If detected, record the time
99+
detected_now = True
100+
101+
# Check detection status
102+
if detected_now:
103+
if last_detected_time is None:
104+
last_detected_time = time.time() # Start the timer
105+
else:
106+
# Check if ambulance is continuously detected for more than the threshold
107+
elapsed_time = time.time() - last_detected_time
108+
if elapsed_time >= detection_duration_threshold:
109+
ambulance_detected = True
110+
last_detected_time = None # Reset the timer after detection
111+
# Create flag file to signal Raspberry Pi
112+
stdin, stdout, stderr = ssh.exec_command(f"pgrep -f {RUNNING_SCRIPT}")
113+
pid = stdout.read().decode().strip()
114+
115+
if pid:
116+
# Send a SIGTERM signal to the process to stop it
117+
ssh.exec_command(f"kill -SIGTERM {pid}")
118+
command2 = f"python3 {BUZZER_SCRIPT}"
119+
stdin, stdout, stderr = ssh.exec_command(command2)
120+
time.sleep(2)
121+
command1 = f"python3 {SCRIPT_PATH}"
122+
stdin, stdout, stderr = ssh.exec_command(command1)
123+
124+
else:
125+
# Reset last_detected_time if not detected
126+
last_detected_time = None
127+
128+
129+
return frame
130+
131+
def process_frames():
132+
while not stop_event.is_set():
133+
if not frame_queue.empty():
134+
frame = frame_queue.get()
135+
processed_frame = detect_ambulance(frame)
136+
if display_frame_queue.full():
137+
display_frame_queue.get()
138+
display_frame_queue.put(processed_frame)
139+
140+
def display_frames():
141+
window_name = 'Ambulance and Emergency Vehicle Detection'
142+
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
143+
cv2.resizeWindow(window_name, TARGET_WIDTH, TARGET_HEIGHT)
144+
145+
while not stop_event.is_set():
146+
if not display_frame_queue.empty():
147+
frame = display_frame_queue.get()
148+
cv2.imshow(window_name, frame)
149+
if cv2.waitKey(1) & 0xFF == ord('q'):
150+
stop_event.set()
151+
else:
152+
time.sleep(0.001) # Short sleep to prevent busy-waiting
153+
154+
def main():
155+
print("Model class names:", model.names)
156+
print("Starting ambulance and emergency vehicle detection. Press 'q' to quit.")
157+
158+
cap = cv2.VideoCapture(ip_cam_url)
159+
160+
if not cap.isOpened():
161+
print("Error: Could not open video stream. Please check your camera URL.")
162+
return
163+
164+
# Set camera resolution
165+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, TARGET_WIDTH)
166+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, TARGET_HEIGHT)
167+
168+
# Start threads
169+
threads = [
170+
threading.Thread(target=capture_frames, args=(cap,)),
171+
threading.Thread(target=process_frames),
172+
threading.Thread(target=display_frames)
173+
]
174+
175+
for t in threads:
176+
t.start()
177+
178+
try:
179+
while not stop_event.is_set():
180+
time.sleep(0.1)
181+
except KeyboardInterrupt:
182+
print("Stopping...")
183+
finally:
184+
stop_event.set()
185+
for t in threads:
186+
t.join()
187+
188+
cap.release()
189+
cv2.destroyAllWindows()
190+
ssh.close() # Close SSH connection at the end of the program
191+
192+
if __name__ == "__main__":
193+
main()

model/best.pt

13.8 MB
Binary file not shown.

raspberrypi/buzzer.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from gpiozero import LED
2+
import time
3+
4+
buz = LED(22)
5+
6+
while True
7+
buz.on()
8+
time.sleep(2)
9+

raspberrypi/run.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
from gpiozero import LED
3+
from time import sleep
4+
import signal
5+
import sys
6+
7+
led_red = LED(17)
8+
led_green = LED(23)
9+
10+
def cleanup(signal_received, frame):
11+
led_red.off()
12+
led_green.off()
13+
sys.exit(0)
14+
15+
signal.signal(signal.SIGINT, cleanup)
16+
signal.signal(signal.SIGTERM, cleanup)
17+
18+
try:
19+
while True:
20+
led_red.on()
21+
sleep(20)
22+
led_red.off()
23+
led_green.on()
24+
sleep(10)
25+
led_green.off()
26+
except Exception as e:
27+
cleanup(None, None)

raspberrypi/run_script.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from gpiozero import LED
2+
from time import sleep
3+
import os
4+
5+
led_red = LED(17)
6+
led_green = LED(23)
7+
8+
while True :
9+
led_red.off()
10+
led_green.on()
11+
sleep(14)

video/lv_0_20241114215415.mp4

15.4 MB
Binary file not shown.

video/lv_0_20241114220258.mp4

11 MB
Binary file not shown.

0 commit comments

Comments
 (0)