Spaces:
Sleeping
Sleeping
Commit
·
37ca0d6
1
Parent(s):
8159d29
OpenCV Face Recognition model using DeepFace
Browse files- FaceRecognition.py +92 -0
FaceRecognition.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from deepface import DeepFace
|
| 2 |
+
import cv2
|
| 3 |
+
import time
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
windowsHolder = st.empty()
|
| 8 |
+
value = st.empty()
|
| 9 |
+
|
| 10 |
+
def main():
|
| 11 |
+
cap = cv2.VideoCapture(0)
|
| 12 |
+
while True:
|
| 13 |
+
time.sleep(0.5)
|
| 14 |
+
_, img = cap.read()
|
| 15 |
+
|
| 16 |
+
if img is None:
|
| 17 |
+
break
|
| 18 |
+
|
| 19 |
+
#cv2.imshow("Window", img)
|
| 20 |
+
windowsHolder.image(img, channels="BGR")
|
| 21 |
+
extract_faces(img)
|
| 22 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
| 23 |
+
break
|
| 24 |
+
cap.release()
|
| 25 |
+
cv2.destroyAllWindows()
|
| 26 |
+
|
| 27 |
+
return
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def extract_faces(raw_img):
|
| 31 |
+
test = DeepFace.extract_faces(raw_img, detector_backend="mtcnn", align=True)
|
| 32 |
+
|
| 33 |
+
if test:
|
| 34 |
+
faces = []
|
| 35 |
+
|
| 36 |
+
for face_obj in test:
|
| 37 |
+
facial_area = face_obj["facial_area"]
|
| 38 |
+
faces.append(
|
| 39 |
+
(
|
| 40 |
+
facial_area["x"],
|
| 41 |
+
facial_area["y"],
|
| 42 |
+
facial_area["w"],
|
| 43 |
+
facial_area["h"],
|
| 44 |
+
)
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
detected_faces = []
|
| 48 |
+
for x, y, w, h in faces:
|
| 49 |
+
detected_face = raw_img[int(y) : int(y + h), int(x) : int(x + w)]
|
| 50 |
+
detected_faces.append(detected_face)
|
| 51 |
+
recognition(detected_face)
|
| 52 |
+
print("Face detected")
|
| 53 |
+
time.sleep(0.6)
|
| 54 |
+
return
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def recognition(img):
|
| 58 |
+
print("Starting recognition")
|
| 59 |
+
dfs = DeepFace.find(
|
| 60 |
+
img_path=img,
|
| 61 |
+
db_path="/Users/futuregadgetlab/Desktop/DB",
|
| 62 |
+
detector_backend="mtcnn",
|
| 63 |
+
model_name="VGG-Face",
|
| 64 |
+
distance_metric="euclidean_l2",
|
| 65 |
+
enforce_detection=False,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
found_non_empty_df = False
|
| 69 |
+
|
| 70 |
+
for df in dfs:
|
| 71 |
+
if len(df) != 0:
|
| 72 |
+
for _, row in df.iterrows():
|
| 73 |
+
if row["VGG-Face_euclidean_l2"] < 0.6:
|
| 74 |
+
print("Matched!")
|
| 75 |
+
with value.container():
|
| 76 |
+
st.write("Matched! Welcome to expo 2023")
|
| 77 |
+
st.balloons()
|
| 78 |
+
found_non_empty_df = True
|
| 79 |
+
break
|
| 80 |
+
|
| 81 |
+
if found_non_empty_df:
|
| 82 |
+
break # Exit the outer loop if a match is found in any DataFrame
|
| 83 |
+
|
| 84 |
+
if not found_non_empty_df:
|
| 85 |
+
print("SIke")
|
| 86 |
+
with value.container():
|
| 87 |
+
st.warning("You don't exist, my friend!")
|
| 88 |
+
time.sleep(1)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
main()
|