Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
+
import face_recognition
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load known face encodings and names
|
7 |
+
ruthchild_image = face_recognition.load_image_file("picturesofknownpeople/ruthchild.jpg")
|
8 |
+
ruthchild_face_encoding = face_recognition.face_encodings(ruthchild_image)[0]
|
9 |
+
|
10 |
+
harryking_image = face_recognition.load_image_file("picturesofknownpeople/harryking.jpg")
|
11 |
+
harryking_face_encoding = face_recognition.face_encodings(harryking_image)[0]
|
12 |
+
|
13 |
+
barackobama_image = face_recognition.load_image_file("picturesofknownpeople/barack_obama.jpg")
|
14 |
+
barackobama_face_encoding = face_recognition.face_encodings(barackobama_image)[0]
|
15 |
+
|
16 |
+
michelleobama_image = face_recognition.load_image_file("picturesofknownpeople/michelle_obama.jpg")
|
17 |
+
michelleobama_face_encoding = face_recognition.face_encodings(michelleobama_image)[0]
|
18 |
+
|
19 |
+
biden_image = face_recognition.load_image_file("picturesofknownpeople/biden.jpg")
|
20 |
+
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
|
21 |
+
|
22 |
+
known_face_encodings = [
|
23 |
+
ruthchild_face_encoding,
|
24 |
+
harryking_face_encoding,
|
25 |
+
barackobama_face_encoding,
|
26 |
+
michelleobama_face_encoding,
|
27 |
+
biden_face_encoding
|
28 |
+
]
|
29 |
+
known_face_names = [
|
30 |
+
"ruthchild",
|
31 |
+
"harryking",
|
32 |
+
"barackobama",
|
33 |
+
"michelleobama",
|
34 |
+
"biden"
|
35 |
+
]
|
36 |
+
|
37 |
+
def main():
|
38 |
+
st.set_page_config(page_title="Insightly", page_icon="https://i.ibb.co/bX6GdqG/insightly-wbg.png")
|
39 |
+
st.sidebar.image("https://i.ibb.co/bX6GdqG/insightly-wbg.png", use_column_width=True)
|
40 |
+
st.title("Face Recognition with Streamlit")
|
41 |
+
|
42 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
43 |
+
|
44 |
+
if uploaded_image is not None:
|
45 |
+
unknown_image = face_recognition.load_image_file(uploaded_image)
|
46 |
+
|
47 |
+
# Find faces in the uploaded image
|
48 |
+
face_locations = face_recognition.face_locations(unknown_image)
|
49 |
+
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
|
50 |
+
|
51 |
+
# Create a Pillow ImageDraw Draw instance to draw on the image
|
52 |
+
pil_image = Image.fromarray(unknown_image)
|
53 |
+
draw = ImageDraw.Draw(pil_image)
|
54 |
+
|
55 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
56 |
+
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
|
57 |
+
|
58 |
+
name = "Unknown"
|
59 |
+
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
|
60 |
+
best_match_index = np.argmin(face_distances)
|
61 |
+
|
62 |
+
if matches[best_match_index]:
|
63 |
+
name = known_face_names[best_match_index]
|
64 |
+
|
65 |
+
# Draw a box and label on the image
|
66 |
+
draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))
|
67 |
+
text_width, text_height = draw.textsize(name)
|
68 |
+
draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
|
69 |
+
draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))
|
70 |
+
|
71 |
+
st.image(pil_image, caption="Processed Image", use_column_width=True)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
main()
|