Spaces:
Runtime error
Runtime error
sarwansingh
commited on
Commit
•
c242c73
1
Parent(s):
0a9d895
Update app.py
Browse files
app.py
CHANGED
@@ -9,13 +9,64 @@ import os
|
|
9 |
|
10 |
st.title("AIMLJan24 - Face Recognition")
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
file_name = st.camera_input("Take a picture") #st.file_uploader("Upload image ")
|
13 |
|
14 |
if file_name is not None:
|
15 |
col1, col2 = st.columns(2)
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
21 |
|
|
|
9 |
|
10 |
st.title("AIMLJan24 - Face Recognition")
|
11 |
|
12 |
+
# create list of encoding of all images in photos folder
|
13 |
+
# Load images for face recognition
|
14 |
+
Images = [] # List to store Images
|
15 |
+
classnames = [] # List to store classnames
|
16 |
+
directory = "photos"
|
17 |
+
myList = os.listdir(directory)
|
18 |
+
|
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 |
+
classnames.append(os.path.splitext(cls)[0])
|
25 |
+
|
26 |
+
# Load images for face recognition
|
27 |
+
encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
|
28 |
+
|
29 |
+
# camera to take photo of user in question
|
30 |
file_name = st.camera_input("Take a picture") #st.file_uploader("Upload image ")
|
31 |
|
32 |
if file_name is not None:
|
33 |
col1, col2 = st.columns(2)
|
34 |
|
35 |
+
test_image = Image.open(file_name)
|
36 |
+
image = np.asarray(test_image)
|
37 |
+
|
38 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
39 |
+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
40 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
41 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
42 |
+
|
43 |
+
name = "Unknown" # Default name for unknown faces
|
44 |
+
match_found = False # Flag to track if a match is found
|
45 |
+
|
46 |
+
# Checking if faces are detected
|
47 |
+
if len(encodesCurFrame) > 0:
|
48 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
49 |
+
# Assuming that encodeListknown is defined and populated in your code
|
50 |
+
matches = face_recognition.compare_faces(encodeListknown, encodeFace)
|
51 |
+
faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
|
52 |
+
matchIndex = np.argmin(faceDis)
|
53 |
+
|
54 |
+
if matches[matchIndex]:
|
55 |
+
name = classnames[matchIndex].upper()
|
56 |
+
match_found = True # Set the flag to True
|
57 |
+
|
58 |
+
y1, x2, y2, x1 = faceLoc
|
59 |
+
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
|
60 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
61 |
+
cv2.rectangle(image, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
62 |
+
cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
63 |
+
|
64 |
+
st.image(image, use_column_width=True, output_format="PNG")
|
65 |
+
else:
|
66 |
+
st.warning("No faces detected in the image. Face recognition failed.")
|
67 |
+
|
68 |
+
# image = Image.open(file_name)
|
69 |
+
# col1.image(image, use_column_width=True)
|
70 |
|
71 |
# pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
72 |
|