This is a Flask web application that uses OpenCV and a trained TensorFlow model to detect whether a user's eyes are open or closed. If the user's eyes are closed for more than a certain proportion of time, the system will alert the user to focus.
- Flask
- OpenCV
- TensorFlow
- time
- collections
- numpy
- os
- The application uses a webcam to capture video frames.
- Each frame is processed to detect faces and eyes using OpenCV's Haar cascades.
- If eyes are detected, the region of the image containing the eyes is passed to a pre-trained TensorFlow model, which classifies the eyes as either open or closed.
- The application keeps track of the eye state predictions over the last N seconds. If the proportion of 'closed' predictions exceeds a certain threshold, the system alerts the user to focus.
eye_classifer(img): This function takes an image as input, detects faces and eyes in the image using Haar cascades, and returns the region of the image containing the first detected eye.is_focused(): This function calculates the proportion of 'closed eyes' predictions in the last N seconds and returns whether this proportion is below a certain threshold.classify_frame(frame, model): This function takes a frame and a model as input, preprocesses the frame, uses the model to classify the frame as 'open eyes' or 'closed eyes', and returns the prediction.gen(): This is a generator function that reads frames from the webcam, processes each frame, makes predictions, checks if the user is focused, and yields the processed frames.video_feed(): This is a route that returns the response from thegen()function.index(): This is the home page route that renders theindex.htmltemplate.
To run the app, execute the script. The app will start on 0.0.0.0 port 8080.
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)