File size: 4,773 Bytes
66e2753
39c1508
66e2753
5e883fe
 
39c1508
 
 
 
 
c875d02
 
 
 
 
 
 
 
978fb4c
 
66e2753
 
 
 
 
 
 
 
 
 
 
 
 
5e883fe
 
 
 
 
 
 
 
 
2955d91
5e883fe
 
 
 
66e2753
255703a
d84a5e2
2c3edb3
42235ef
255703a
 
 
174ea44
 
d84a5e2
255703a
 
 
65b5fd8
 
255703a
 
66e2753
 
 
5e883fe
 
 
 
 
 
 
 
 
 
fc9bb68
 
 
662c339
255703a
 
42235ef
 
5e883fe
 
ddb6238
66e2753
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import urllib.request

if not os.path.exists("data"):
    os.mkdir("data")
    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/b/bb/Andy_Lau_%E5%88%98%E5%BE%B7%E5%8D%8E%2C_Beijing_International_Film_Festival_%E5%8C%97%E4%BA%AC%E7%94%B5%E5%BD%B1%E8%8A%82%2C_2013_%28cropped%29.jpg", "data/劉德華.jpg")
    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/d/dc/Chaplin_The_Champion.jpg", "data/卓別林.jpg")
    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Dayo_Wong_at_Olympian_City.jpg/800px-Dayo_Wong_at_Olympian_City.jpg", "data/黃子華.jpg")
    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/2/2c/Woody_Allen_Cannes_2015.jpg", "data/伍迪·艾伦.jpg")
    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/0/09/RussellPeters08TIFF.jpg", "data/罗素·彼得斯.jpg")
    opener = urllib.request.URLopener()
    opener.addheader('User-Agent', 'whatever')
    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/411903-411903-Screenshot-2023-03-10-at-11.19.32-940x1024.png", "data/林青霞.png")
    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/122403-122403-IMG_4831-481x600-1.jpeg", "data/張曼玉.jpeg")
    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/092803-092803-Screenshot-2023-03-10-at-11.28.01-687x1024.png", "data/關之琳.png")
    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/233103-233103-Screenshot-2023-03-10-at-11.31.08-783x1024.png", "data/王祖賢.png")
    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/433403-433403-w644-4.jpeg", "data/邱淑貞.jpeg")
    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/333503-333503-20210621104344-80d11f2a.jpeg", "data/李嘉欣.jpeg")


import face_recognition

# Often instead of just checking if two faces match or not (True or False), it's helpful to see how similar they are.
# You can do that by using the face_distance function.

# 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
# be more strict, you can look for a smaller face distance. For example, using a 0.55 cutoff would reduce false
# positive matches at the risk of more false negatives.

# Note: This isn't exactly the same as a "percent match". The scale isn't linear. But you can assume that images with a
# smaller distance are more similar to each other than ones with a larger distance.

# Load some images to compare against
known_encodings = []
known_persons = []
valid_images = [".jpg",".jpeg",".png"]
for f in os.listdir("data"):
    ext = os.path.splitext(f)[1]
    if ext.lower() not in valid_images:
        continue

    # Get the face encodings for the known images
    known_image = face_recognition.load_image_file(os.path.join("data",f))
    face_encoding = face_recognition.face_encodings(known_image)[0]
    known_encodings.append(face_encoding)
    # known_persons.append(os.path.splitext(os.path.basename(f))[0])
    known_persons.append(os.path.basename(f))

import tempfile
import faceSym
from PIL import Image
import numpy as np

def left_right_sim(img):
    tmpf = tempfile.NamedTemporaryFile(delete=False)
    im = Image.fromarray(img)
    im.save(tmpf.name, format='png')
    f = faceSym.FaceSym(tmpf.name)
    _, left, _, _, right, _ = f.get_symmetrized_images(idx=0)
    tmpf.close()
    os.unlink(tmpf.name)
    left_encoding = face_recognition.face_encodings(np.asarray(left))[0]
    right_encoding = face_recognition.face_encodings(np.asarray(right))[0]
    diff = face_recognition.face_distance([left_encoding], right_encoding)
    return 100 * (1 - diff)

import gradio as gr

def greet(image_to_test):
    # # Load a test image and get encondings for it
    # image_to_test = face_recognition.load_image_file(filepath)
    image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
    
    # See how far apart the test image is from the known faces
    face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
    idx = face_distances.argmin()
    filepath = known_persons[idx]
    face_distance = face_distances[idx]
    ret = "The most similar person is of {} with score {:.3}".format(
        os.path.splitext(filepath)[0],
        100 * (1 - face_distance))
    img = face_recognition.load_image_file(os.path.join("data", filepath))
    ret += "\n\n \
    The similarity (symmetry score) of \
    left and right face = {:.3}%".format(
        left_right_sim(image_to_test).item())
    return img, ret

iface = gr.Interface(fn=greet, inputs=gr.Image(source="webcam", streaming=True, type="numpy"), outputs=["image", "text"])
iface.launch()