Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeshkatakam committed May 28, 2024
1 parent 3954f0c commit 2b2e85a
Show file tree
Hide file tree
Showing 16 changed files with 1,958 additions and 2 deletions.
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Ignore files generated by the IDE
.idea/
.vscode/
/src/api/.env
# Ignore compiled binaries and libraries
*.exe
*.dll
*.so
*.dylib

# Ignore build output directories
/bin/
/build/
/dist/
# Ignore build output directories
/bin/*
/build/*
/dist/*
/pycache/*
# Ignore package manager directories
/node_modules/
/vendor/

# Ignore log files
*.log
*.env
# Ignore temporary files
*.tmp
*.swp
*.bak

# Ignore system files
.DS_Store
Thumbs.db

# Ignore sensitive or personal information
config.ini
secrets.txt

# Ignore specific files or directories
/path/to/file.txt
/path/to/directory/
24 changes: 24 additions & 0 deletions DockerFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use an official Python runtime as a parent image
FROM python:3.8

# Set the working directory in the container to /app
WORKDIR /app

# Add the current directory contents into the container at /app
ADD . /app

# Install Poetry
RUN pip install poetry

# Install project dependencies
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi

# Make port 5000 available to the world outside this container
EXPOSE 5000

# # Run the command to start uWSGI
# CMD ["uwsgi", "app.ini"]

# # Run the GUI application
# CMD ["python", "gui.py"]
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# Real-Time-Face-Recognition-System
An End-to-End Real-Time Face recognition built using FaceNet Model Deployed with Streamlit UI and Flask API
# Real-Time-Face-Recognition
This repository is dedicated to the Project "End-to-End real-time face recognition system"

## Usage

### Install Dependencies
* Clone the Project Repository to your system
```bash
git clone <GITHUB_REPO_URL>
```
* Go to the Project Directory and Create a Virtual Environment in Python

```bash
python -m venv face-recognition-system
source face-recognition-system/bin/activate

```

```py
pip install requirements.txt
```

### Start the Flask API Server

* Navigate to src/api Folder
```bash
python endpoints.py
```
**Note:** This will start your Flask API at : **http://localhost:5000/**
The API has two endpoints (you can test the API using POSTMAN)
>* **http://localhost:5000/api/register**(for User FaceID registration)
>* **http://localhost:5000/api/recognize** (for recognizing user given face image)
### Start the Interactive UI(Streamlit)
* After Starting the API, We can use the Face Recognition system interactively through streamlit application

* Run the command
```bash
streamlit run streamlit_app.py
```

* Streamlit UI will start at : **http://localhost:8501**
11 changes: 11 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Import necessary modules or packages here

# Define any global variables or constants here

# Define any helper functions or classes here

# Define the main entry point of your module here

if __name__ == "__main__":
# Code to be executed when the module is run as a standalone script
pass
88 changes: 88 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
absl-py==2.1.0
aioice==0.9.0
aiortc==1.8.0
altair==5.3.0
astunparse==1.6.3
attrs==23.2.0
av==11.0.0
blinker==1.8.2
cachetools==5.3.3
certifi==2024.2.2
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
coloredlogs==15.0.1
cryptography==42.0.7
dnspython==2.6.1
Flask==3.0.3
flatbuffers==24.3.25
gast==0.5.4
gitdb==4.0.11
GitPython==3.1.43
google-crc32c==1.5.0
google-pasta==0.2.0
grpcio==1.63.0
h5py==3.11.0
humanfriendly==10.0
idna==3.7
ifaddr==0.2.0
itsdangerous==2.2.0
Jinja2==3.1.4
joblib==1.4.2
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
keras==3.3.3
libclang==18.1.1
Markdown==3.6
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
ml-dtypes==0.3.2
mpmath==1.3.0
mysql-connector-python==8.4.0
namex==0.0.8
numpy==1.26.4
onnxruntime==1.17.3
opencv-python==4.9.0.80
opt-einsum==3.3.0
optree==0.11.0
packaging==24.0
pandas==2.2.2
pillow==10.3.0
protobuf==4.25.3
pyarrow==16.0.0
pycparser==2.22
pydeck==0.9.1
pyee==11.1.0
Pygments==2.18.0
pylibsrtp==0.10.0
pyOpenSSL==24.1.0
python-dateutil==2.9.0.post0
pytz==2024.1
referencing==0.35.1
requests==2.31.0
rich==13.7.1
rpds-py==0.18.1
scikit-learn==1.4.2
scipy==1.13.0
six==1.16.0
smmap==5.0.1
streamlit==1.34.0
streamlit-webrtc==0.47.6
sympy==1.12
tenacity==8.3.0
tensorboard==2.16.2
tensorboard-data-server==0.7.2
tensorflow==2.16.1
termcolor==2.4.0
threadpoolctl==3.5.0
toml==0.10.2
toolz==0.12.1
tornado==6.4
typing_extensions==4.11.0
tzdata==2024.1
urllib3==2.2.1
watchdog==4.0.0
Werkzeug==3.0.3
wrapt==1.16.0
python-dotenv==1.0.1
Empty file added src/api/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions src/api/face_recognition_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cv2
import tensorflow as tf
import numpy as np
from preprocessing import Preprocessor


class FaceRecognizer():
def __init__(self, model_path):
self.interpreter = tf.lite.Interpreter(model_path=model_path)
self.interpreter.allocate_tensors()

def preprocess_image(self, img):
img = cv2.resize(img, (160, 160)) # Resize image to the size expected by the FaceNet model
img = img.astype('float32') / 255 # Normalize pixel values to [0, 1]
img = np.expand_dims(img, axis=0) # Add batch dimension
return img

def get_embedding(self, img):
img = self.preprocess_image(img)
input_details = self.interpreter.get_input_details()
output_details = self.interpreter.get_output_details()
self.interpreter.set_tensor(input_details[0]['index'], img)
self.interpreter.invoke()
return self.interpreter.get_tensor(output_details[0]['index'])



from tensorflow.keras.models import load_model

class FaceRecognizerH5():
def __init__(self, model_path):
self.model = load_model(model_path)

def preprocess_image(self, img):
img = cv2.resize(img, (160, 160)) # Resize image to the size expected by the FaceNet model
img = img.astype('float32') / 255 # Normalize pixel values to [0, 1]
img = np.expand_dims(img, axis=0) # Add batch dimension
return img

def get_embedding(self, img):
img = self.preprocess_image(img)
return self.model.predict(img)
63 changes: 63 additions & 0 deletions src/api/preprocessing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import cv2
import numpy as np
import tensorflow as tf
import sklearn.preprocessing
import tensorflow as tf
import numpy as np
import numpy as np
from scipy import ndimage
from scipy.spatial import distance


class Preprocessor():
def __init__(self, transforms, width, height, channels):
super().__init__()
self.transforms = transforms
self.width = width
self.height = height
self.channels = channels
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
self.eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')


def face_detect(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = self.face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
image = image[y:y+h, x:x+w]
return image

def face_align(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.3, 5)

for (x, y, w, h) in faces:
eyes = self.eye_cascade.detectMultiScale(gray[y:y+h, x:x+w])
if len(eyes) >= 2:
# Find the largest two eye regions
eyes = sorted(eyes, key=lambda x: -x[2])[:2]
# Sort the eye regions such that left_eye[0] <= right_eye[0]
eyes = sorted(eyes, key=lambda x: x[0])
left_eye, right_eye = eyes
# Calculate the angle between the two eyes
dy = right_eye[1] - left_eye[1]
dx = right_eye[0] - left_eye[0]
angle = np.degrees(np.arctan2(dy, dx))
# Rotate the image to align the eyes horizontally
M = cv2.getRotationMatrix2D(((left_eye[0] + right_eye[0]) // 2, (left_eye[1] + right_eye[1]) // 2), angle, 1)
image = cv2.warpAffine(image, M, (self.width, self.height))
return image

def crop_face_image(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
image = image[y:y+h, x:x+w]
return image

def scale_face_image(self, image):
return cv2.resize(image, (self.width, self.height))

def normalize_color(self, image):
return image / 255.0
Empty file added src/models/__init__.py
Empty file.
1 change: 1 addition & 0 deletions src/models/weights/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import face_recognition_model
Binary file added src/models/weights/facenet.tflite
Binary file not shown.
Loading

0 comments on commit 2b2e85a

Please sign in to comment.