From 191f5db3be0a1d4e9d2e10156a983081ffc7e2f2 Mon Sep 17 00:00:00 2001 From: Palak <138296311+palak-38@users.noreply.github.com> Date: Sun, 27 Jul 2025 15:35:05 +0530 Subject: [PATCH 1/4] Update main.py (launch file) 1. Fixed the window becoming unresponsive after calling a subprocess 2. Enhanced the functionality by enabling the subprocesses to be terminated by pressing the (X) icon. 3. Fixed inconsistencies between function names and called subprocesses. 4. Improved the overall GUI. --- main.py | 58 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/main.py b/main.py index 0986e00..01f8f47 100644 --- a/main.py +++ b/main.py @@ -1,39 +1,41 @@ -# -*- coding: utf-8 -*- -""" -Created on Fri Nov 22 11:04:42 2019 +from tkinter import * +from tkinter.ttk import * +import subprocess -@author: Lenovo -""" -from tkinter import * -from tkinter.ttk import * -import subprocess - -def blink(): - subprocess.call(["python", "face-try.py"]) - -def lane(): - subprocess.call(["python", "blinkDetect.py"]) root = Tk() root.geometry('500x500') style = Style() -style.configure('TButton', font =('calibri', 20, 'bold'), borderwidth = '2') -#root.title('The game') -root.geometry("500x500") -#tk.resizable(0, 0) -frame = tk.Frame(root) -frame.pack() - +style.configure('TButton', font=('calibri', 20, 'bold'), borderwidth='2') -button1 = Button(frame, text="Face Detection", fg="red", command=blink,height=25, width=10) -button1.pack(side=root.LEFT) -button2 = Button(frame, text="Blink Detection", fg="red",command=lane) -button2.pack(side=root.RIGHT) - -button3 = Button(frame, text="Quit", fg="red",command=root.destroy) -button3.pack(side=root.BOTTOM) +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 +frame = Frame(root) +frame.pack(side=TOP, pady=40) + +button1 = Button(frame, text="Face Detection", command=face) +button1.pack(side=LEFT, padx=10, pady=10) + +button2 = Button(frame, text="Blink Detection", command=blink) +button2.pack(side=LEFT, padx=10, pady=10) + +button3 = Button(root, text="Quit", command=root.destroy) +button3.pack(side=BOTTOM, pady=30) root.mainloop() From 34b80ae47a74b7f8c44c2ac6b477a68346390f01 Mon Sep 17 00:00:00 2001 From: Palak <138296311+palak-38@users.noreply.github.com> Date: Sun, 27 Jul 2025 15:40:15 +0530 Subject: [PATCH 2/4] Update blinkDetect.py ADDED FUNCTIONALITY OF BEING STOPPED BY CLICKING ON 'X' --- blinkDetect.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/blinkDetect.py b/blinkDetect.py index 8887b98..0cd428e 100644 --- a/blinkDetect.py +++ b/blinkDetect.py @@ -20,7 +20,7 @@ RESIZE_HEIGHT = 460 thresh = 0.27 -modelPath = "models/shape_predictor_70_face_landmarks.dat" +modelPath = r"models\shape_predictor_68_face_landmarks.dat" sound_path = "alarm.wav" detector = dlib.get_frontal_face_detector() @@ -147,7 +147,7 @@ def getLandmarks(im): validFrames = 0 dummyFrames = 100 -print("Caliberation in Progress!") +print("Calibration in Progress!") while(validFrames < dummyFrames): validFrames += 1 t = time.time() @@ -235,6 +235,8 @@ def getLandmarks(im): cv2.imshow("Blink Detection", frame) + + vid_writer.write(frame) k = cv2.waitKey(1) @@ -246,6 +248,9 @@ def getLandmarks(im): elif k == ord('q'): break + #--ADDED FUNCTIONALITY OF BEING STOPPED BY CLICKING ON X + if cv2.getWindowProperty('Blink Detection', cv2.WND_PROP_VISIBLE) < 1: + break # print("Time taken", time.time() - t) @@ -254,4 +259,4 @@ def getLandmarks(im): capture.release() vid_writer.release() - cv2.destroyAllWindows() \ No newline at end of file + cv2.destroyAllWindows() From b327d8aad681ea54ed96738b53e85ba7eba066f3 Mon Sep 17 00:00:00 2001 From: Palak <138296311+palak-38@users.noreply.github.com> Date: Sun, 27 Jul 2025 15:41:25 +0530 Subject: [PATCH 3/4] Update face-try.py --- face-try.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/face-try.py b/face-try.py index 43d3f2c..966af6e 100644 --- a/face-try.py +++ b/face-try.py @@ -6,12 +6,14 @@ """ import cv2 -face_cascade = cv2.CascadeClassifier('E:\\project\\main\\models\\haarcascade_frontalface_default.xml') - +face_cascade = cv2.CascadeClassifier(r'models\haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) while cap.isOpened(): _, img = cap.read() + if img is None: + print("Can't acess camera! Ending process..") + break gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 4) @@ -22,6 +24,8 @@ 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() \ No newline at end of file +cv2.destroyAllWindows() From 28a22be4871aeca1d7b58e543cdd562225343177 Mon Sep 17 00:00:00 2001 From: Palak <138296311+palak-38@users.noreply.github.com> Date: Sun, 27 Jul 2025 15:46:13 +0530 Subject: [PATCH 4/4] Update blinkDetect.py 1. CHANGED MODEL TO AVAILABLE MODEL N THE WEB, SINCE IT WAS MISSING FROM THE MODELS FOLDER 2. ADDED FUNCTIONALITY OF EXITING THE SUBPROCESS BY CLICKING 'X' --- blinkDetect.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/blinkDetect.py b/blinkDetect.py index 0cd428e..086af8d 100644 --- a/blinkDetect.py +++ b/blinkDetect.py @@ -20,6 +20,8 @@ RESIZE_HEIGHT = 460 thresh = 0.27 +#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" @@ -248,7 +250,8 @@ def getLandmarks(im): elif k == ord('q'): break - #--ADDED FUNCTIONALITY OF BEING STOPPED BY CLICKING ON X + + #ADDED FUNCTIONALITY OF TERMINATING ON CLICKING ON 'X' if cv2.getWindowProperty('Blink Detection', cv2.WND_PROP_VISIBLE) < 1: break