Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
| 6 |
+
urllib.request.urlretrieve("https://github.com/ageitgey/face_recognition/blob/master/examples/obama.jpg?raw=true")
|
| 7 |
+
urllib.request.urlretrieve("https://github.com/ageitgey/face_recognition/blob/master/examples/obama2.jpg?raw=true")
|
| 8 |
+
|
| 9 |
+
import face_recognition
|
| 10 |
+
|
| 11 |
+
# Often instead of just checking if two faces match or not (True or False), it's helpful to see how similar they are.
|
| 12 |
+
# You can do that by using the face_distance function.
|
| 13 |
+
|
| 14 |
+
# The model was trained in a way that faces with a distance of 0.6 or less should be a match. But if you want to
|
| 15 |
+
# be more strict, you can look for a smaller face distance. For example, using a 0.55 cutoff would reduce false
|
| 16 |
+
# positive matches at the risk of more false negatives.
|
| 17 |
+
|
| 18 |
+
# Note: This isn't exactly the same as a "percent match". The scale isn't linear. But you can assume that images with a
|
| 19 |
+
# smaller distance are more similar to each other than ones with a larger distance.
|
| 20 |
+
|
| 21 |
+
# Load some images to compare against
|
| 22 |
+
known_obama_image = face_recognition.load_image_file("obama.jpg")
|
| 23 |
+
known_biden_image = face_recognition.load_image_file("biden.jpg")
|
| 24 |
+
|
| 25 |
+
# Get the face encodings for the known images
|
| 26 |
+
obama_face_encoding = face_recognition.face_encodings(known_obama_image)[0]
|
| 27 |
+
biden_face_encoding = face_recognition.face_encodings(known_biden_image)[0]
|
| 28 |
+
|
| 29 |
+
known_encodings = [
|
| 30 |
+
obama_face_encoding,
|
| 31 |
+
biden_face_encoding
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Load a test image and get encondings for it
|
| 35 |
+
image_to_test = face_recognition.load_image_file("obama2.jpg")
|
| 36 |
+
image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
|
| 37 |
+
|
| 38 |
+
# See how far apart the test image is from the known faces
|
| 39 |
+
face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
import gradio as gr
|
| 43 |
+
|
| 44 |
+
def greet(name):
|
| 45 |
+
ret = ""
|
| 46 |
+
for i, face_distance in enumerate(face_distances):
|
| 47 |
+
ret += "The test image has a distance of {:.2} from known image #{}".format(face_distance, i)
|
| 48 |
+
ret += "\n"
|
| 49 |
+
ret += "- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6)
|
| 50 |
+
ret += "\n"
|
| 51 |
+
ret += "- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5)
|
| 52 |
+
ret += "\n\n"
|
| 53 |
+
|
| 54 |
+
return ret
|
| 55 |
+
|
| 56 |
+
iface = gr.Interface(fn=greet, inputs="image", outputs="text")
|
| 57 |
+
iface.launch()
|