Shafeek Saleem commited on
Commit
caa436d
1 Parent(s): 19127c5

face recog fixed

Browse files
Files changed (1) hide show
  1. pages/4_Face Recognition.py +48 -57
pages/4_Face Recognition.py CHANGED
@@ -7,6 +7,7 @@ import time
7
  import face_recognition
8
  import cv2
9
  import numpy as np
 
10
 
11
  initialize_login()
12
  initialize_level()
@@ -14,18 +15,6 @@ initialize_level()
14
  LEVEL = 4
15
 
16
 
17
- def infer(image):
18
- time.sleep(1)
19
- output = query(image)
20
- cols = st.columns(2)
21
- cols[0].image(image, use_column_width=True)
22
- with cols[1]:
23
- for item in output:
24
- st.progress(item["score"], text=item["label"])
25
-
26
- # Get a reference to webcam #0 (the default one)
27
- video_capture = cv2.VideoCapture(0)
28
-
29
  def step4_page():
30
  st.header("Face Recognition: Trying It Out")
31
  st.write(
@@ -50,51 +39,53 @@ def step4_page():
50
  known_face_encodings.append(known_face_encoding)
51
  known_face_names.append(face_name)
52
 
53
- while True:
54
- # Grab a single frame of video
55
- ret, frame = video_capture.read()
56
-
57
- # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
58
- rgb_frame = frame[:, :, ::-1]
59
-
60
- # Find all the faces and face encodings in the frame of video
61
- face_locations = face_recognition.face_locations(rgb_frame)
62
- face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
63
-
64
- # Loop through each face in this frame of video
65
- for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
66
- # See if the face is a match for the known face(s)
67
- matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
68
-
69
- name = "Unknown"
70
-
71
- # If a match was found in known_face_encodings, just use the first one.
72
- # if True in matches:
73
- # first_match_index = matches.index(True)
74
- # name = known_face_names[first_match_index]
75
-
76
- # Or instead, use the known face with the smallest distance to the new face
77
- face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
78
- best_match_index = np.argmin(face_distances)
79
- if matches[best_match_index]:
80
- name = known_face_names[best_match_index]
81
-
82
- # Draw a box around the face
83
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
84
-
85
- # Draw a label with a name below the face
86
- cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
87
- font = cv2.FONT_HERSHEY_DUPLEX
88
- cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
89
-
90
- # Display the resulting image
91
- cv2.imshow('Video', frame)
92
- # Hit 'q' on the keyboard to quit!
93
- if cv2.waitKey(1) & 0xFF == ord('q'):
94
- break
95
- # Release handle to the webcam
96
- video_capture.release()
97
- cv2.destroyAllWindows()
 
 
98
 
99
  st.info("Click on the button below to complete this level!")
100
  if st.button("Complete Level"):
 
7
  import face_recognition
8
  import cv2
9
  import numpy as np
10
+ from PIL import Image
11
 
12
  initialize_login()
13
  initialize_level()
 
15
  LEVEL = 4
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def step4_page():
19
  st.header("Face Recognition: Trying It Out")
20
  st.write(
 
39
  known_face_encodings.append(known_face_encoding)
40
  known_face_names.append(face_name)
41
 
42
+ st.info("Select an image to analyze!")
43
+ input_type = st.radio("Select the Input Type", ["Image", "Camera"])
44
+
45
+ if input_type == "Camera":
46
+ picture = st.camera_input("Take a picture")
47
+ else:
48
+ picture = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
49
+ if picture:
50
+ image = face_recognition.load_image_file(picture)
51
+ face_locations = face_recognition.face_locations(image)
52
+ face_encodings = face_recognition.face_encodings(image, face_locations)
53
+
54
+ # Loop through each face in this image
55
+ cols = st.columns(len(face_encodings))
56
+ i = 0
57
+ for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
58
+ # See if the face is a match for the known face(s)
59
+ matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
60
+
61
+ name = "Unknown"
62
+ # If a match was found in known_face_encodings, just use the first one.
63
+ if True in matches:
64
+ first_match_index = matches.index(True)
65
+ name = known_face_names[first_match_index]
66
+
67
+ face_image = image[top:bottom, left:right]
68
+ pil_image = Image.fromarray(face_image)
69
+ cols[i].image(pil_image, use_column_width=True)
70
+ cols[i].write("Person name: " +name)
71
+ i+=1
72
+
73
+ # # Draw a box around the face
74
+ # cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
75
+ #
76
+ # # Draw a label with a name below the face
77
+ # cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
78
+ # font = cv2.FONT_HERSHEY_DUPLEX
79
+ # cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
80
+ #
81
+ # # Display the resulting image
82
+ # cv2.imshow('Video', frame)
83
+ # # Hit 'q' on the keyboard to quit!
84
+ # if cv2.waitKey(1) & 0xFF == ord('q'):
85
+ # break
86
+ # # Release handle to the webcam
87
+ # video_capture.release()
88
+ # cv2.destroyAllWindows()
89
 
90
  st.info("Click on the button below to complete this level!")
91
  if st.button("Complete Level"):