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()