Upload 4 files
Browse files- embeddings_app.py +42 -0
- image_app.py +65 -0
- utils.py +15 -0
- webcam_app.py +66 -0
embeddings_app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from tqdm import tqdm
|
| 3 |
+
from glob import glob
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2 as cv2
|
| 6 |
+
import insightface
|
| 7 |
+
from insightface.app import FaceAnalysis
|
| 8 |
+
from insightface.data import get_image as ins_get_image
|
| 9 |
+
import cv2
|
| 10 |
+
from insightface.app import FaceAnalysis
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
import utils
|
| 13 |
+
import streamlit as st
|
| 14 |
+
from utils import app
|
| 15 |
+
|
| 16 |
+
def get_embeddings(db_dir):
|
| 17 |
+
names = []
|
| 18 |
+
embeddings = []
|
| 19 |
+
|
| 20 |
+
# Traverse through each subfolder
|
| 21 |
+
for root, dirs, files in os.walk(db_dir):
|
| 22 |
+
for folder in dirs:
|
| 23 |
+
if folder == ".ipynb_checkpoints":
|
| 24 |
+
continue
|
| 25 |
+
img_paths = glob(os.path.join(root, folder, '*'))
|
| 26 |
+
for img_path in img_paths:
|
| 27 |
+
img = cv2.imread(img_path)
|
| 28 |
+
if img is None:
|
| 29 |
+
continue
|
| 30 |
+
faces = app.get(img)
|
| 31 |
+
if len(faces) != 1:
|
| 32 |
+
continue
|
| 33 |
+
face = faces[0]
|
| 34 |
+
names.append(folder)
|
| 35 |
+
embeddings.append(face.normed_embedding)
|
| 36 |
+
|
| 37 |
+
if embeddings:
|
| 38 |
+
embeddings = np.stack(embeddings, axis=0)
|
| 39 |
+
np.save(os.path.join(db_dir, "embeddings.npy"), embeddings)
|
| 40 |
+
np.save(os.path.join(db_dir, "names.npy"), names)
|
| 41 |
+
else:
|
| 42 |
+
st.warning("No embeddings generated. Please ensure that there are valid images with detected faces.")
|
image_app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from embeddings import app
|
| 5 |
+
import os
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
from glob import glob
|
| 8 |
+
import numpy as np
|
| 9 |
+
import cv2 as cv2
|
| 10 |
+
import insightface
|
| 11 |
+
from insightface.app import FaceAnalysis
|
| 12 |
+
from insightface.data import get_image as ins_get_image
|
| 13 |
+
import cv2
|
| 14 |
+
from insightface.app import FaceAnalysis
|
| 15 |
+
import matplotlib.pyplot as plt
|
| 16 |
+
import utils
|
| 17 |
+
from utils import app
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Define function to recognize faces and display bounding boxes
|
| 21 |
+
def recognize_and_display(input_img, known_embeddings, names, app):
|
| 22 |
+
# Perform face analysis on the input image
|
| 23 |
+
faces = app.get(input_img)
|
| 24 |
+
|
| 25 |
+
# Check if any face is detected
|
| 26 |
+
if len(faces) == 0:
|
| 27 |
+
# If no face detected, draw bounding box with "unknown" text for the whole image
|
| 28 |
+
input_img_with_bb = input_img.copy()
|
| 29 |
+
cv2.putText(input_img_with_bb, "Unknown", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
| 30 |
+
st.image(cv2.cvtColor(input_img_with_bb, cv2.COLOR_BGR2RGB), caption='No face detected', use_column_width=True)
|
| 31 |
+
return "No face detected"
|
| 32 |
+
else:
|
| 33 |
+
# Process each detected face separately
|
| 34 |
+
for face in faces:
|
| 35 |
+
# Retrieve the embedding for the detected face
|
| 36 |
+
detected_embedding = face.normed_embedding
|
| 37 |
+
|
| 38 |
+
# Calculate similarity scores with known embeddings
|
| 39 |
+
scores = np.dot(detected_embedding, np.array(known_embeddings).T)
|
| 40 |
+
scores = np.clip(scores, 0., 1.)
|
| 41 |
+
|
| 42 |
+
# Find the index with the highest score
|
| 43 |
+
idx = np.argmax(scores)
|
| 44 |
+
max_score = scores[idx]
|
| 45 |
+
|
| 46 |
+
# Check if the maximum score is above a certain threshold (adjust as needed)
|
| 47 |
+
threshold = 0.7
|
| 48 |
+
if max_score >= threshold:
|
| 49 |
+
recognized_name = names[idx]
|
| 50 |
+
else:
|
| 51 |
+
recognized_name = "Unknown"
|
| 52 |
+
|
| 53 |
+
# Draw bounding box around the detected face
|
| 54 |
+
bbox = face.bbox.astype(int)
|
| 55 |
+
cv2.rectangle(input_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 10)
|
| 56 |
+
# Write recognized name within the bounding box
|
| 57 |
+
cv2.putText(input_img, recognized_name, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 5.0, (0, 255, 0), 10)
|
| 58 |
+
|
| 59 |
+
# Display the image with bounding boxes using Streamlit
|
| 60 |
+
st.image(cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB), caption='Face Recognition Result', use_column_width=True)
|
| 61 |
+
|
| 62 |
+
if "Unknown" in names:
|
| 63 |
+
return "Face not recognized"
|
| 64 |
+
else:
|
| 65 |
+
return f"All faces recognized"
|
utils.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from tqdm import tqdm
|
| 3 |
+
from glob import glob
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2 as cv2
|
| 6 |
+
import insightface
|
| 7 |
+
from insightface.app import FaceAnalysis
|
| 8 |
+
from insightface.data import get_image as ins_get_image
|
| 9 |
+
import cv2
|
| 10 |
+
from insightface.app import FaceAnalysis
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
app = FaceAnalysis(name='buffalo_l')
|
| 15 |
+
app.prepare(ctx_id=0, det_size=(640, 640),)
|
webcam_app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import insightface
|
| 6 |
+
from insightface.app import FaceAnalysis
|
| 7 |
+
from insightface.data import get_image as ins_get_image
|
| 8 |
+
from glob import glob
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
|
| 11 |
+
import shutil
|
| 12 |
+
import zipfile
|
| 13 |
+
import image_app
|
| 14 |
+
from utils import app
|
| 15 |
+
from embeddings_app import get_embeddings
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class FaceRecognitionTransformer(VideoTransformerBase):
|
| 19 |
+
def __init__(self):
|
| 20 |
+
self.app = FaceAnalysis(name='buffalo_l')
|
| 21 |
+
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
| 22 |
+
self.names = None
|
| 23 |
+
self.embeddings = None
|
| 24 |
+
|
| 25 |
+
def _recognize_faces(self, frame):
|
| 26 |
+
if self.names is None or self.embeddings is None:
|
| 27 |
+
return frame
|
| 28 |
+
|
| 29 |
+
# Perform face analysis on the frame
|
| 30 |
+
faces = self.app.get(frame)
|
| 31 |
+
|
| 32 |
+
# Process each detected face separately
|
| 33 |
+
for face in faces:
|
| 34 |
+
# Retrieve the embedding for the detected face
|
| 35 |
+
detected_embedding = face.normed_embedding
|
| 36 |
+
|
| 37 |
+
# Calculate similarity scores with known embeddings
|
| 38 |
+
scores = np.dot(detected_embedding, np.array(self.embeddings).T)
|
| 39 |
+
scores = np.clip(scores, 0., 1.)
|
| 40 |
+
|
| 41 |
+
# Find the index with the highest score
|
| 42 |
+
idx = np.argmax(scores)
|
| 43 |
+
max_score = scores[idx]
|
| 44 |
+
|
| 45 |
+
# Check if the maximum score is above a certain threshold (adjust as needed)
|
| 46 |
+
threshold = 0.7
|
| 47 |
+
if max_score >= threshold:
|
| 48 |
+
recognized_name = self.names[idx]
|
| 49 |
+
else:
|
| 50 |
+
recognized_name = "Unknown"
|
| 51 |
+
|
| 52 |
+
# Draw bounding box around the detected face
|
| 53 |
+
bbox = face.bbox.astype(int)
|
| 54 |
+
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
|
| 55 |
+
# Write recognized name within the bounding box
|
| 56 |
+
cv2.putText(frame, recognized_name, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 57 |
+
|
| 58 |
+
# Debug print
|
| 59 |
+
print("Detected face:", recognized_name, "with confidence:", max_score)
|
| 60 |
+
|
| 61 |
+
return frame
|
| 62 |
+
|
| 63 |
+
def transform(self, frame):
|
| 64 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 65 |
+
frame = self._recognize_faces(frame)
|
| 66 |
+
return frame
|