Downloads ยท 30 days
41
5% of all-time downloads
shayan5422/Eye-Movement-Recognition
Eye-Movement-Recognition is a object detection model from shayan5422. Use it when you need objects located in an image. It is set up for keras. The card lists the license as mit.
Downloads ยท 30 days
41
5% of all-time downloads
All-time downloads
771
Public
Repo size
1.3 GB
Likes
0
Public
Click a slice to open those files.
.keras1 GB ยท 78%
From the Hugging Face model README
The Eye and Eyebrow Movement Recognition model is an advanced real-time system designed to accurately detect and classify subtle facial movements, specifically focusing on the eyes and eyebrows. Currently, the model is trained to recognize three distinct movements:
Leveraging a CNN-LSTM (Convolutional Neural Network - Long Short-Term Memory) architecture, the model effectively captures both spatial features from individual frames and temporal dynamics across sequences of frames. This ensures robust and reliable performance in real-world scenarios.
This model is ideal for a variety of applications, including but not limited to:
Note: The model is intended for research and educational purposes. Ensure compliance with privacy and ethical guidelines when deploying in real-world applications.
The model employs a CNN-LSTM architecture to capture both spatial and temporal features:
TimeDistributed CNN Layers:
Flatten Layer:
LSTM Layer:
Dense Layers:
Output Layer:
The model was trained on a curated dataset consisting of short video clips (1-2 seconds) capturing the three target movements:
Each video was recorded using a standard webcam under varied lighting conditions and backgrounds to ensure robustness. The videos were manually labeled and organized into respective directories for preprocessing.
The model was evaluated on a separate test set comprising 60 samples for each class. The evaluation metrics are as follows:
Clone the Repository
git clone https://huggingface.co/shayan5422/eye-eyebrow-movement-recognition
cd eye-eyebrow-movement-recognition
Install Homebrew (if not already installed)
Homebrew is a package manager for macOS that simplifies the installation of software.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Micromamba
Micromamba is a lightweight package manager compatible with Conda environments.
brew install micromamba
Create and Activate a Virtual Environment
We'll use Micromamba to create an isolated environment for our project.
# Create a new environment named 'eye_movement' with Python 3.9
micromamba create -n eye_movement python=3.9
# Activate the environment
micromamba activate eye_movement
Install Required Libraries
We'll install TensorFlow with Metal support (tensorflow-macos and tensorflow-metal) along with other necessary libraries.
# Install TensorFlow for macOS
pip install tensorflow-macos
# Install TensorFlow Metal plugin for GPU acceleration
pip install tensorflow-metal
# Install other dependencies
pip install opencv-python dlib imutils tqdm scikit-learn matplotlib seaborn h5py
Note: Installing
dlibcan sometimes be challenging on macOS. If you encounter issues, consider installing it via Conda or refer to dlib's official installation instructions.
Download Dlib's Pre-trained Shape Predictor
This model is essential for facial landmark detection.
# Navigate to your project directory
cd /path/to/your/project/eye-eyebrow-movement-recognition/
# Download the shape predictor
curl -LO http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
# Decompress the file
bunzip2 shape_predictor_68_face_landmarks.dat.bz2
Ensure that the shape_predictor_68_face_landmarks.dat file is in the same directory as your scripts.
import tensorflow as tf
# Load the trained model
model = tf.keras.models.load_model('final_model_sequences.keras')
import cv2
import numpy as np
import dlib
from imutils import face_utils
from collections import deque
import queue
import threading
# Initialize dlib's face detector and landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
# Initialize queues for threading
input_queue = queue.Queue()
output_queue = queue.Queue()
# Define sequence length
max_seq_length = 30
def prediction_worker(model, input_q, output_q):
while True:
sequence = input_q.get()
if sequence is None:
break
# Preprocess and predict
# [Add your prediction logic here]
# Example:
prediction = model.predict(sequence)
class_idx = np.argmax(prediction)
confidence = np.max(prediction)
output_q.put((class_idx, confidence))
# Start prediction thread
thread = threading.Thread(target=prediction_worker, args=(model, input_queue, output_queue))
thread.start()
# Start video capture
cap = cv2.VideoCapture(0)
frame_buffer = deque(maxlen=max_seq_length)
while True:
ret, frame = cap.read()
if not ret:
break
# Preprocess frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
if len(rects) > 0:
rect = rects[0]
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# Extract ROIs and preprocess
# [Add your ROI extraction and preprocessing here]
# Example:
preprocessed_frame = preprocess_frame(frame, detector, predictor)
frame_buffer.append(preprocessed_frame)
else:
frame_buffer.append(np.zeros((64, 256, 1), dtype='float32'))
# If buffer is full, send to prediction
if len(frame_buffer) == max_seq_length:
sequence = np.array(frame_buffer)
input_queue.put(np.expand_dims(sequence, axis=0))
frame_buffer.clear()
# Check for prediction results
try:
while True:
class_idx, confidence = output_queue.get_nowait()
movement = index_to_text.get(class_idx, "Unknown")
text = f"{movement} ({confidence*100:.2f}%)"
cv2.putText(frame, text, (30, 30), cv2.FONT_HERSHEY_SIMPLEX,
0.8, (0, 255, 0), 2, cv2.LINE_AA)
except queue.Empty:
pass
# Display the frame
cv2.imshow('Real-time Movement Prediction', frame)
# Exit on 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Cleanup
cap.release()
cv2.destroyAllWindows()
input_queue.put(None)
thread.join()
Note: Replace the placeholder comments with your actual preprocessing and prediction logic as implemented in your scripts.
This project is licensed under the MIT License.
Feel free to reach out or contribute to enhance the capabilities of this model!