Spaces:
Runtime error
Runtime error
Shafeek Saleem
commited on
Commit
·
3a2c2e3
1
Parent(s):
3b8dcd6
bug fixed image read
Browse files
pages/2_Face Detection and Creating Database.py
CHANGED
@@ -56,7 +56,8 @@ def step2_page():
|
|
56 |
But remember, we should always ask for permission before taking someone's picture. We can use a smartphone or a digital camera to capture pictures, and it's important to take pictures of different people. This will help our application to have a good known-faces database!
|
57 |
"""
|
58 |
)
|
59 |
-
st.info("
|
|
|
60 |
input_type = st.radio("Select the Input Type", ["Image", "Camera"])
|
61 |
|
62 |
if input_type == "Camera":
|
@@ -66,36 +67,74 @@ def step2_page():
|
|
66 |
if picture:
|
67 |
image = face_recognition.load_image_file(picture)
|
68 |
st.image(image)
|
|
|
|
|
|
|
69 |
|
70 |
# Find all the faces in the image using the default HOG-based model.
|
71 |
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
|
72 |
# See also: find_faces_in_picture_cnn.py
|
73 |
face_locations = face_recognition.face_locations(image)
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
# display faces
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
94 |
pil_image.save(img_path)
|
95 |
st.success("Face added successfully!")
|
96 |
|
|
|
97 |
images = os.listdir(img_dir)
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
if st.button("Clear All"):
|
100 |
for img in images:
|
101 |
os.remove(os.path.join(img_dir, img))
|
|
|
56 |
But remember, we should always ask for permission before taking someone's picture. We can use a smartphone or a digital camera to capture pictures, and it's important to take pictures of different people. This will help our application to have a good known-faces database!
|
57 |
"""
|
58 |
)
|
59 |
+
st.info("Who is taking the picture?")
|
60 |
+
face_name = st.text_input('Specify name to save it in the known-face database', "This is a placeholder", key="name")
|
61 |
input_type = st.radio("Select the Input Type", ["Image", "Camera"])
|
62 |
|
63 |
if input_type == "Camera":
|
|
|
67 |
if picture:
|
68 |
image = face_recognition.load_image_file(picture)
|
69 |
st.image(image)
|
70 |
+
my_bar = st.progress(0, text="Detecting faces in given images...")
|
71 |
+
for i in range(100):
|
72 |
+
my_bar.progress(i, text="Detecting faces in given images...")
|
73 |
|
74 |
# Find all the faces in the image using the default HOG-based model.
|
75 |
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
|
76 |
# See also: find_faces_in_picture_cnn.py
|
77 |
face_locations = face_recognition.face_locations(image)
|
78 |
+
my_bar.progress(100, text="Algorithm found {} face(s) in this photograph.".format(len(face_locations)))
|
79 |
+
|
80 |
+
if len(face_locations) <= 0:
|
81 |
+
st.error("No faces have been detected, please upload a valid/ clear photo!")
|
82 |
+
elif len(face_locations) > 1:
|
83 |
+
st.info("More than one face has been detected, please identify each face by providing a name.")
|
84 |
+
cols = st.columns(len(face_locations))
|
85 |
+
for i in range(len(face_locations)):
|
86 |
+
col = cols[i]
|
87 |
+
face = face_locations[i]
|
88 |
+
# display faces
|
89 |
+
with col:
|
90 |
+
# Print the location of each face in this image
|
91 |
+
top, right, bottom, left = face
|
92 |
+
# You can access the actual face itself like this:
|
93 |
+
face_image = image[top:bottom, left:right]
|
94 |
+
pil_image = Image.fromarray(face_image)
|
95 |
+
col.image(pil_image, use_column_width=True)
|
96 |
+
face_name = st.text_input('Who is this?',
|
97 |
+
"This is a placeholder", key="text_" + str(i))
|
98 |
+
if st.button("Save", key="button_" + str(i)):
|
99 |
+
img_name = str(uuid.uuid4()) + f"_{face_name}_{i}" + ".jpg"
|
100 |
+
img_path = os.path.join(img_dir, img_name)
|
101 |
+
if os.path.exists(img_path):
|
102 |
+
st.error("Face already added!")
|
103 |
+
else:
|
104 |
+
pil_image.save(img_path)
|
105 |
+
st.success("Face added successfully!")
|
106 |
+
|
107 |
+
else:
|
108 |
+
st.info("Your face is identified successfully!")
|
109 |
+
face = face_locations[0]
|
110 |
# display faces
|
111 |
+
# Print the location of each face in this image
|
112 |
+
top, right, bottom, left = face
|
113 |
+
# You can access the actual face itself like this:
|
114 |
+
face_image = image[top:bottom, left:right]
|
115 |
+
pil_image = Image.fromarray(face_image)
|
116 |
+
st.image(pil_image, use_column_width="auto")
|
117 |
+
st.write(face_name)
|
118 |
+
if st.button("Save", key="button"):
|
119 |
+
img_name = str(uuid.uuid4()) + f"_{face_name}_0" + ".jpg"
|
120 |
+
img_path = os.path.join(img_dir, img_name)
|
121 |
+
if os.path.exists(img_path):
|
122 |
+
st.error("Face already added!")
|
123 |
+
else:
|
124 |
pil_image.save(img_path)
|
125 |
st.success("Face added successfully!")
|
126 |
|
127 |
+
st.write("Now lets see your known-faces database!")
|
128 |
images = os.listdir(img_dir)
|
129 |
+
if len(images) <= 0:
|
130 |
+
st.error("No faces have been added yet.")
|
131 |
+
else:
|
132 |
+
cols = st.columns(len(images))
|
133 |
+
for i, img in enumerate(images):
|
134 |
+
face_name = img.split("_")[1]
|
135 |
+
cols[i].image(os.path.join(img_dir, img), use_column_width="auto")
|
136 |
+
cols[i].write(face_name)
|
137 |
+
|
138 |
if st.button("Clear All"):
|
139 |
for img in images:
|
140 |
os.remove(os.path.join(img_dir, img))
|
utils/levels.py
CHANGED
@@ -33,4 +33,4 @@ def complete_level(level):
|
|
33 |
with open(f".sessions/{get_login()['username']}/level.txt", "w") as f:
|
34 |
f.write(str(st.session_state['level']))
|
35 |
st.balloons()
|
36 |
-
st.success(f'You have completed Level {level}! You can now move on to the next level.')
|
|
|
33 |
with open(f".sessions/{get_login()['username']}/level.txt", "w") as f:
|
34 |
f.write(str(st.session_state['level']))
|
35 |
st.balloons()
|
36 |
+
st.success(f'You have completed Level {level}! You can now move on to the next level.')
|