CoolFace
Apppublic

npc0/TestingAI

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py91 linesDownload Raw Back to root
1import os2import urllib.request3 4if not os.path.exists("data"):5    os.mkdir("data")6    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")7    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/d/dc/Chaplin_The_Champion.jpg", "data/卓別林.jpg")8    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")9    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/2/2c/Woody_Allen_Cannes_2015.jpg", "data/伍迪·艾伦.jpg")10    urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/0/09/RussellPeters08TIFF.jpg", "data/罗素·彼得斯.jpg")11    opener = urllib.request.URLopener()12    opener.addheader('User-Agent', 'whatever')13    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")14    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/122403-122403-IMG_4831-481x600-1.jpeg", "data/張曼玉.jpeg")15    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")16    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")17    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/433403-433403-w644-4.jpeg", "data/邱淑貞.jpeg")18    opener.retrieve("https://cfcdn.she.com/media/she.com/2023/03/333503-333503-20210621104344-80d11f2a.jpeg", "data/李嘉欣.jpeg")19 20 21import face_recognition22 23# Often instead of just checking if two faces match or not (True or False), it's helpful to see how similar they are.24# You can do that by using the face_distance function.25 26# 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 to27# be more strict, you can look for a smaller face distance. For example, using a 0.55 cutoff would reduce false28# positive matches at the risk of more false negatives.29 30# Note: This isn't exactly the same as a "percent match". The scale isn't linear. But you can assume that images with a31# smaller distance are more similar to each other than ones with a larger distance.32 33# Load some images to compare against34known_encodings = []35known_persons = []36valid_images = [".jpg",".jpeg",".png"]37for f in os.listdir("data"):38    ext = os.path.splitext(f)[1]39    if ext.lower() not in valid_images:40        continue41 42    # Get the face encodings for the known images43    known_image = face_recognition.load_image_file(os.path.join("data",f))44    face_encoding = face_recognition.face_encodings(known_image)[0]45    known_encodings.append(face_encoding)46    # known_persons.append(os.path.splitext(os.path.basename(f))[0])47    known_persons.append(os.path.basename(f))48 49import tempfile50import faceSym51from PIL import Image52import numpy as np53 54def left_right_sim(img):55    tmpf = tempfile.NamedTemporaryFile(delete=False)56    im = Image.fromarray(img)57    im.save(tmpf.name, format='png')58    f = faceSym.FaceSym(tmpf.name)59    _, left, _, _, right, _ = f.get_symmetrized_images(idx=0)60    tmpf.close()61    os.unlink(tmpf.name)62    left_encoding = face_recognition.face_encodings(np.asarray(left))[0]63    right_encoding = face_recognition.face_encodings(np.asarray(right))[0]64    diff = face_recognition.face_distance([left_encoding], right_encoding)65    return 100 * (1 - diff)66 67import gradio as gr68 69def greet(image_to_test):70    # # Load a test image and get encondings for it71    # image_to_test = face_recognition.load_image_file(filepath)72    image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]73    74    # See how far apart the test image is from the known faces75    face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)76    idx = face_distances.argmin()77    filepath = known_persons[idx]78    face_distance = face_distances[idx]79    ret = "The most similar person is of {} with score {:.3}".format(80        os.path.splitext(filepath)[0],81        100 * (1 - face_distance))82    img = face_recognition.load_image_file(os.path.join("data", filepath))83    ret += "\n\n \84    The similarity (symmetry score) of \85    left and right face = {:.3}%".format(86        left_right_sim(image_to_test).item())87    return img, ret88 89iface = gr.Interface(fn=greet, inputs=gr.Image(source="webcam", streaming=True, type="numpy"), outputs=["image", "text"])90iface.launch()91