|
| 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() |
0 commit comments