Shafeek Saleem commited on
Commit
14979bd
·
1 Parent(s): 641a529

added face detection

Browse files
Files changed (2) hide show
  1. .sessions/.gitkeep +0 -0
  2. app.py +26 -13
.sessions/.gitkeep ADDED
File without changes
app.py CHANGED
@@ -2,22 +2,31 @@ import streamlit as st
2
  from PIL import Image
3
  import face_recognition
4
 
5
- st.title("Face Detection")
 
 
6
 
7
- # Load the jpg file into a numpy array
8
- input_image = st.file_uploader("Upload a candidate image",type=['jpg','png','jpeg'],accept_multiple_files=False)
9
- if input_image is not None:
10
- image = face_recognition.load_image_file(input_image)
 
 
 
 
 
 
 
 
11
  st.image(image)
12
 
13
  # Find all the faces in the image using the default HOG-based model.
14
  # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
15
  # See also: find_faces_in_picture_cnn.py
16
  face_locations = face_recognition.face_locations(image)
17
- data_base = []
18
- st.write("I found {} face(s) in this photograph.".format(len(face_locations)))
19
-
20
- cols= st.columns(len(face_locations))
21
  for i in range(len(face_locations)):
22
  col = cols[i]
23
  face = face_locations[i]
@@ -30,8 +39,12 @@ if input_image is not None:
30
  face_image = image[top:bottom, left:right]
31
  pil_image = Image.fromarray(face_image)
32
  st.image(pil_image)
33
- face_name = st.text_input('Specify name', "This is a placeholder")
34
- st.write(face_name)
35
- data_base.append(face_name)
 
 
 
 
36
  else:
37
- st.write("Please upload an image to proceed.")
 
2
  from PIL import Image
3
  import face_recognition
4
 
5
+ import streamlit as st
6
+ from PIL import Image
7
+ import face_recognition
8
 
9
+ st.header("Face Detection")
10
+ st.write(
11
+ "Now it's time to collect the pictures we need to create our known-faces data base for our face recognition model. "
12
+ "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!"
13
+ )
14
+
15
+ img_dir = os.path.join(".sessions", "known_faces")
16
+ os.makedirs(img_dir, exist_ok=True)
17
+
18
+ picture = st.file_uploader("Upload a candidate image",type=['jpg','png','jpeg'],accept_multiple_files=False)
19
+ if picture:
20
+ image = face_recognition.load_image_file(picture)
21
  st.image(image)
22
 
23
  # Find all the faces in the image using the default HOG-based model.
24
  # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
25
  # See also: find_faces_in_picture_cnn.py
26
  face_locations = face_recognition.face_locations(image)
27
+ st.write("Algorithm found {} face(s) in this photograph.".format(len(face_locations)))
28
+
29
+ cols = st.columns(len(face_locations))
 
30
  for i in range(len(face_locations)):
31
  col = cols[i]
32
  face = face_locations[i]
 
39
  face_image = image[top:bottom, left:right]
40
  pil_image = Image.fromarray(face_image)
41
  st.image(pil_image)
42
+ face_name = st.text_input('Specify name', "This is a placeholder", key=str(i))
43
+ if st.button("Save", key=str(i)):
44
+ img_name = str(uuid.uuid4()) + f"{face_name}" + ".jpg"
45
+ img_path = os.path.join(img_dir, img_name)
46
+ with open(img_path, "wb") as f:
47
+ f.write(pil_image)
48
+ st.success("Face added successfully!")
49
  else:
50
+ st.write("Please upload an image to proceed.")