diff --git a/blinkDetect.py b/blinkDetect.py index 23b6e53..6fc5d3b 100644 --- a/blinkDetect.py +++ b/blinkDetect.py @@ -21,10 +21,10 @@ thresh = 0.27 -# IMPORTANT: You must download the shape_predictor_68_face_landmarks.dat file from -# https://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 -# and place it in the 'models' folder -modelPath = "models/shape_predictor_68_face_landmarks.dat" +#SINCE MODEL WAS NOT AVAILABLE IN THE REPOSITORY, I DOWNLOADED THE OFFCIAL MODEL FROM THE WEBSITE AND USED THAT +# DOWNLOAD LINK: https://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 +modelPath = r"models\shape_predictor_68_face_landmarks.dat" + sound_path = "alarm.wav" detector = dlib.get_frontal_face_detector() @@ -160,7 +160,7 @@ def getLandmarks(im): validFrames = 0 dummyFrames = 100 -print("Caliberation in Progress!") +print("Calibration in Progress!") while(validFrames < dummyFrames): validFrames += 1 t = time.time() @@ -252,6 +252,8 @@ def getLandmarks(im): cv2.imshow("Blink Detection", frame) + + vid_writer.write(frame) k = cv2.waitKey(1) @@ -264,6 +266,10 @@ def getLandmarks(im): elif k == ord('q'): break + #ADDED FUNCTIONALITY OF TERMINATING ON CLICKING ON 'X' + if cv2.getWindowProperty('Blink Detection', cv2.WND_PROP_VISIBLE) < 1: + break + # print("Time taken", time.time() - t) except Exception as e: diff --git a/face-try.py b/face-try.py index 92d9580..c117ac0 100644 --- a/face-try.py +++ b/face-try.py @@ -7,6 +7,7 @@ import cv2 import sys + # Load Haar cascade cascade_path = '/home/happy/gssoc/driver-drowsiness-detection-system/models/haarcascade_frontalface_default.xml' face_cascade = cv2.CascadeClassifier(cascade_path) @@ -43,15 +44,19 @@ for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3) + cv2.imshow('Face Detection', img) if cv2.waitKey(1) & 0xFF == ord('q'): print("[INFO] Exiting on user request.") break -except KeyboardInterrupt: - print("\n[INFO] Exiting on Ctrl+C") -finally: - cap.release() - cv2.destroyAllWindows() + cv2.imshow('Face Detection', img) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + #ADDED FUNCTIONALITY OF TERMINATING BY CLICKING ON 'X' + if cv2.getWindowProperty('Face Detection', cv2.WND_PROP_VISIBLE) < 1: + break +cap.release() +cv2.destroyAllWindows() diff --git a/main.py b/main.py index f7d3d16..9aa0cf7 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ + import tkinter as tk from tkinter import ttk, messagebox import subprocess @@ -18,12 +19,31 @@ def run_blink_detection(): except Exception as e: messagebox.showerror("Error", f"Failed to run blink detection:\n{e}") + def on_quit(root): if face_proc and face_proc.poll() is None: face_proc.terminate() root.destroy() + +def face(): + subprocess.Popen(["python", "face-try.py"]) + +def blink(): + subprocess.Popen(["python", "blinkDetect.py"]) + +def on_quit(): + global current_process + # If a process is running, terminate it! + if current_process and current_process.poll() is None: + current_process.terminate() # Send SIGTERM or equivalent + try: + current_process.wait(timeout=3) + except subprocess.TimeoutExpired: + current_process.kill() # Force kill if still running + root.destroy() # Close the GUI + def main(): root = tk.Tk() root.title("Driver Drowsiness Detection System") @@ -34,17 +54,26 @@ def main(): frame = ttk.Frame(root, padding=20) frame.pack(expand=True) + + + frame = Frame(root) + frame.pack(side=TOP, pady=40) - btn_face = ttk.Button(frame, text="Face Detection", command=run_face_detection) - btn_face.pack(side=tk.LEFT, padx=10, pady=10) + button1 = Button(frame, text="Face Detection", command=face) + button1.pack(side=LEFT, padx=10, pady=10) - btn_blink = ttk.Button(frame, text="Blink Detection", command=run_blink_detection) - btn_blink.pack(side=tk.RIGHT, padx=10, pady=10) + button2 = Button(frame, text="Blink Detection", command=blink) + button2.pack(side=LEFT, padx=10, pady=10) - btn_quit = ttk.Button(root, text="Quit", command=lambda: on_quit(root)) - btn_quit.pack(side=tk.BOTTOM, pady=20) + button3 = Button(root, text="Quit", command=root.destroy) + button3.pack(side=BOTTOM, pady=30) root.mainloop() + + + + + if __name__ == "__main__": main() \ No newline at end of file