Spaces:
Sleeping
Sleeping
LovnishVerma
commited on
Commit
•
f57d510
1
Parent(s):
1c4942b
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,44 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import face_recognition
|
3 |
import cv2
|
4 |
import numpy as np
|
|
|
5 |
import os
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
images.append(cur_img)
|
18 |
-
st.write(os.path.splitext(file)[0])
|
19 |
-
classnames.append(os.path.splitext(file)[0])
|
20 |
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
26 |
-
facesCurFrame
|
27 |
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
28 |
|
29 |
name = "Unknown" # Default name for unknown faces
|
@@ -32,42 +47,21 @@ def recognize_faces(test_image, known_encodings, class_names):
|
|
32 |
# Checking if faces are detected
|
33 |
if len(encodesCurFrame) > 0:
|
34 |
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
35 |
-
|
36 |
-
|
|
|
37 |
matchIndex = np.argmin(faceDis)
|
38 |
|
39 |
if matches[matchIndex]:
|
40 |
-
name =
|
41 |
match_found = True # Set the flag to True
|
42 |
|
43 |
y1, x2, y2, x1 = faceLoc
|
44 |
y1, x2, y2, x1 = (y1 * 4), (x2 * 4), (y2 * 4) ,(x1 * 4)
|
45 |
-
cv2.rectangle(
|
46 |
-
cv2.rectangle(
|
47 |
-
cv2.putText(
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
st.
|
52 |
-
|
53 |
-
# Load images for face recognition
|
54 |
-
directory = "photos"
|
55 |
-
Images, classnames = load_images(directory)
|
56 |
-
|
57 |
-
# Load images for face recognition
|
58 |
-
encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
|
59 |
-
|
60 |
-
# Camera input to take photo of user in question
|
61 |
-
capture = cv2.VideoCapture(0)
|
62 |
-
|
63 |
-
if capture.isOpened():
|
64 |
-
ret, frame = capture.read()
|
65 |
-
if ret:
|
66 |
-
# Recognize faces in the captured frame
|
67 |
-
image_with_recognition = recognize_faces(frame, encodeListknown, classnames)
|
68 |
-
st.image(image_with_recognition, channels="BGR", use_column_width=True)
|
69 |
-
else:
|
70 |
-
st.error("Failed to open camera.")
|
71 |
-
|
72 |
-
capture.release()
|
73 |
-
cv2.destroyAllWindows()
|
|
|
1 |
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
import face_recognition
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
+
import requests
|
7 |
import os
|
8 |
|
9 |
+
st.title("AIMLJan24 - Face Recognition")
|
10 |
+
|
11 |
+
# create list of encoding of all images in photos folder
|
12 |
+
# Load images for face recognition
|
13 |
+
Images = [] # List to store Images
|
14 |
+
classnames = [] # List to store classnames
|
15 |
+
directory = "photos"
|
16 |
+
myList = os.listdir(directory)
|
17 |
+
|
18 |
+
st.write("Photographs found in folder : ")
|
19 |
+
for cls in myList:
|
20 |
+
if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]:
|
21 |
+
img_path = os.path.join(directory, cls)
|
22 |
+
curImg = cv2.imread(img_path)
|
23 |
+
Images.append(curImg)
|
24 |
+
st.write(os.path.splitext(cls)[0])
|
25 |
+
classnames.append(os.path.splitext(cls)[0])
|
26 |
|
27 |
+
# Load images for face recognition
|
28 |
+
encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
|
29 |
+
|
30 |
+
# camera to take photo of user in question
|
31 |
+
file_name = st.camera_input("Take a picture") #st.file_uploader("Upload image ")
|
|
|
|
|
|
|
32 |
|
33 |
+
if file_name is not None:
|
34 |
+
col1, col2 = st.columns(2)
|
35 |
|
36 |
+
test_image = Image.open(file_name)
|
37 |
+
image = np.asarray(test_image)
|
38 |
+
|
39 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
40 |
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
41 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
42 |
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
43 |
|
44 |
name = "Unknown" # Default name for unknown faces
|
|
|
47 |
# Checking if faces are detected
|
48 |
if len(encodesCurFrame) > 0:
|
49 |
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
50 |
+
# Assuming that encodeListknown is defined and populated in your code
|
51 |
+
matches = face_recognition.compare_faces(encodeListknown, encodeFace)
|
52 |
+
faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
|
53 |
matchIndex = np.argmin(faceDis)
|
54 |
|
55 |
if matches[matchIndex]:
|
56 |
+
name = classnames[matchIndex].upper()
|
57 |
match_found = True # Set the flag to True
|
58 |
|
59 |
y1, x2, y2, x1 = faceLoc
|
60 |
y1, x2, y2, x1 = (y1 * 4), (x2 * 4), (y2 * 4) ,(x1 * 4)
|
61 |
+
cv2.rectangle(image , (x1, y1), (x2, y2), (0, 255, 0), 2)
|
62 |
+
cv2.rectangle(image , (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
63 |
+
cv2.putText(image , name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
64 |
|
65 |
+
st.image(image , use_column_width=True, output_format="PNG")
|
66 |
+
else:
|
67 |
+
st.warning("No faces detected in the image. Face recognition failed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|