npc0 commited on
Commit
5e883fe
·
1 Parent(s): 36ea2a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -1,11 +1,7 @@
1
  import os
2
- import urllib.request
3
-
4
- if not os.path.exists("biden.jpg"):
5
- urllib.request.urlretrieve("https://github.com/ageitgey/face_recognition/blob/master/examples/biden.jpg?raw=true", "biden.jpg")
6
- urllib.request.urlretrieve("https://github.com/ageitgey/face_recognition/blob/master/examples/obama.jpg?raw=true", "obama.jpg")
7
- urllib.request.urlretrieve("https://github.com/ageitgey/face_recognition/blob/master/examples/obama2.jpg?raw=true", "obama2.jpg")
8
 
 
 
9
 
10
  import face_recognition
11
 
@@ -20,39 +16,38 @@ import face_recognition
20
  # smaller distance are more similar to each other than ones with a larger distance.
21
 
22
  # Load some images to compare against
23
- known_obama_image = face_recognition.load_image_file("obama.jpg")
24
- known_biden_image = face_recognition.load_image_file("biden.jpg")
25
-
26
- # Get the face encodings for the known images
27
- obama_face_encoding = face_recognition.face_encodings(known_obama_image)[0]
28
- biden_face_encoding = face_recognition.face_encodings(known_biden_image)[0]
29
-
30
- known_encodings = [
31
- obama_face_encoding,
32
- biden_face_encoding
33
- ]
34
-
35
- # Load a test image and get encondings for it
36
- image_to_test = face_recognition.load_image_file("obama2.jpg")
37
- image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
38
-
39
- # See how far apart the test image is from the known faces
40
- face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
41
 
42
 
43
  import gradio as gr
44
 
45
- def greet(name):
46
- ret = ""
47
- for i, face_distance in enumerate(face_distances):
48
- ret += "The test image has a distance of {:.2} from known image #{}".format(face_distance, i)
49
- ret += "\n"
50
- ret += "- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6)
51
- ret += "\n"
52
- ret += "- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5)
53
- ret += "\n\n"
54
-
55
- return ret
56
-
57
- iface = gr.Interface(fn=greet, inputs="image", outputs="text")
 
 
 
58
  iface.launch()
 
1
  import os
 
 
 
 
 
 
2
 
3
+ if not os.path.exists("data"):
4
+ os.mkdir("data")
5
 
6
  import face_recognition
7
 
 
16
  # smaller distance are more similar to each other than ones with a larger distance.
17
 
18
  # Load some images to compare against
19
+ known_encodings = []
20
+ known_persons = []
21
+ valid_images = [".jpg",".jpeg",".png"]
22
+ for f in os.listdir("data"):
23
+ ext = os.path.splitext(f)[1]
24
+ if ext.lower() not in valid_images:
25
+ continue
26
+
27
+ # Get the face encodings for the known images
28
+ known_image = face_recognition.load_image_file(os.path.join(path,f))
29
+ face_encoding = face_recognition.face_encodings(known_image)[0]
30
+ known_encodings.append(face_encoding)
31
+ # known_persons.append(os.path.splitext(os.path.basename(f))[0])
32
+ known_persons.append(os.path.basename(f))
 
 
 
 
33
 
34
 
35
  import gradio as gr
36
 
37
+ def greet(image_to_test):
38
+ # # Load a test image and get encondings for it
39
+ # image_to_test = face_recognition.load_image_file(filepath)
40
+ image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
41
+
42
+ # See how far apart the test image is from the known faces
43
+ face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
44
+ idx = face_distances.argmin()
45
+ filepath = known_persons[idx]
46
+ face_distance = face_distances[idx]
47
+ ret = "The most similar person is of {} with score {:.3}".format(os.path.splitext(filenamme)[0],
48
+ 100 * (1 - face_distance))
49
+ img = face_recognition.load_image_file(filepath)
50
+ return img, ret
51
+
52
+ iface = gr.Interface(fn=greet, inputs=gr.Image(type="numpy"), outputs=["image", "text"])
53
  iface.launch()