Update app.py
Browse files
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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
]
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 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(
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 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()
|