Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -44,10 +44,6 @@ def process_sheet(sheet):
|
|
44 |
processed_lines.append(processed_line)
|
45 |
return '<br>'.join(processed_lines)
|
46 |
|
47 |
-
# Load a sample image and learn how to recognize it
|
48 |
-
known_image = face_recognition.load_image_file("known_face.jpg")
|
49 |
-
known_encoding = face_recognition.face_encodings(known_image)[0]
|
50 |
-
|
51 |
# Main Function
|
52 |
def main():
|
53 |
# Layout Configuration
|
@@ -74,17 +70,28 @@ def main():
|
|
74 |
face_locations = face_recognition.face_locations(rgb_image)
|
75 |
face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
# Iterate over detected faces and compare with known face
|
78 |
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
else:
|
86 |
-
# If no
|
87 |
-
cv2.rectangle(rgb_image, (left, top), (right, bottom), (
|
88 |
|
89 |
# Convert the RGB image back to BGR format for display
|
90 |
bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
|
@@ -96,6 +103,11 @@ def main():
|
|
96 |
st.session_state['captured_images'].append(filename)
|
97 |
st.session_state['last_captured'] = time.time()
|
98 |
|
|
|
|
|
|
|
|
|
|
|
99 |
sidebar_html = "<div style='display:flex;flex-direction:column;'>"
|
100 |
for img_file in st.session_state['captured_images']:
|
101 |
image_base64 = get_image_base64(img_file)
|
|
|
44 |
processed_lines.append(processed_line)
|
45 |
return '<br>'.join(processed_lines)
|
46 |
|
|
|
|
|
|
|
|
|
47 |
# Main Function
|
48 |
def main():
|
49 |
# Layout Configuration
|
|
|
70 |
face_locations = face_recognition.face_locations(rgb_image)
|
71 |
face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
|
72 |
|
73 |
+
# Check if the known face image exists
|
74 |
+
if os.path.isfile("known_face.jpg"):
|
75 |
+
known_image = face_recognition.load_image_file("known_face.jpg")
|
76 |
+
known_encoding = face_recognition.face_encodings(known_image)[0]
|
77 |
+
else:
|
78 |
+
known_encoding = None
|
79 |
+
|
80 |
# Iterate over detected faces and compare with known face
|
81 |
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
82 |
+
if known_encoding is not None:
|
83 |
+
matches = face_recognition.compare_faces([known_encoding], face_encoding)
|
84 |
+
|
85 |
+
if True in matches:
|
86 |
+
# If a match is found, draw a green rectangle and label
|
87 |
+
cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 255, 0), 2)
|
88 |
+
cv2.putText(rgb_image, "Known Face", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
89 |
+
else:
|
90 |
+
# If no match, draw a red rectangle
|
91 |
+
cv2.rectangle(rgb_image, (left, top), (right, bottom), (0, 0, 255), 2)
|
92 |
else:
|
93 |
+
# If no known face is registered, draw a blue rectangle
|
94 |
+
cv2.rectangle(rgb_image, (left, top), (right, bottom), (255, 0, 0), 2)
|
95 |
|
96 |
# Convert the RGB image back to BGR format for display
|
97 |
bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
|
|
|
103 |
st.session_state['captured_images'].append(filename)
|
104 |
st.session_state['last_captured'] = time.time()
|
105 |
|
106 |
+
if st.button("Register Known Face"):
|
107 |
+
if image is not None:
|
108 |
+
cv2.imwrite("known_face.jpg", np.array(image))
|
109 |
+
st.success("Known face registered successfully!")
|
110 |
+
|
111 |
sidebar_html = "<div style='display:flex;flex-direction:column;'>"
|
112 |
for img_file in st.session_state['captured_images']:
|
113 |
image_base64 = get_image_base64(img_file)
|