Spaces:
Sleeping
Sleeping
File size: 10,409 Bytes
927579f 629e59e 7f652ec cce7489 935a1c0 2ce2fb6 96356a4 5db6885 9a8e951 5db6885 9e7fe95 5db6885 950e710 5db6885 950e710 5db6885 64f37dd ed0f16c 96356a4 5db6885 dc8a9fa 96356a4 5db6885 96356a4 5db6885 96356a4 5db6885 96356a4 5db6885 96356a4 cce7489 2fb073c 2f061f8 629e59e 092da24 629e59e 092da24 fe41ca6 629e59e f56c3b3 dc8a9fa 4785f0b de60222 8d6f814 261fdf7 8d6f814 629e59e 2f061f8 629e59e 8d6f814 261fdf7 8d6f814 629e59e 2f061f8 629e59e 3356aa3 828408f 629e59e 9dff503 2fb073c 2f061f8 d950955 966303e 629e59e f4d3482 b1c3552 65c25c6 a2e58a8 b1c3552 65c25c6 a2e58a8 93dfc13 fc49140 133fce2 f4c338e 4b9f8d6 3356aa3 133fce2 7be351f 74d3a4b 6012c79 74d3a4b 9a8e951 74d3a4b 7be351f 74d3a4b 133fce2 17e0a61 4bee2af 9a8e951 4bee2af 7be351f 4bee2af 7be351f 950e710 629e59e 3356aa3 84f3f91 629e59e 4cacc60 |
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
import os
import gradio as gr
import requests
import json
from PIL import Image
def get_attributes(json):
liveness = "GENUINE" if json.get('liveness') >= 0.5 else "FAKE"
attr = json.get('attribute')
age = attr.get('age')
gender = attr.get('gender')
emotion = attr.get('emotion')
ethnicity = attr.get('ethnicity')
mask = [attr.get('face_mask')]
if attr.get('glasses') == 'USUAL':
mask.append('GLASSES')
if attr.get('glasses') == 'DARK':
mask.append('SUNGLASSES')
eye = []
if attr.get('eye_left') >= 0.3:
eye.append('LEFT')
if attr.get('eye_right') >= 0.3:
eye.append('RIGHT')
facehair = attr.get('facial_hair')
haircolor = attr.get('hair_color')
hairtype = attr.get('hair_type')
headwear = attr.get('headwear')
activity = []
if attr.get('food_consumption') >= 0.5:
activity.append('EATING')
if attr.get('phone_recording') >= 0.5:
activity.append('PHONE_RECORDING')
if attr.get('phone_use') >= 0.5:
activity.append('PHONE_USE')
if attr.get('seatbelt') >= 0.5:
activity.append('SEATBELT')
if attr.get('smoking') >= 0.5:
activity.append('SMOKING')
pitch = attr.get('pitch')
roll = attr.get('roll')
yaw = attr.get('yaw')
quality = attr.get('quality')
return liveness, age, gender, emotion, ethnicity, mask, eye, facehair, haircolor, hairtype, headwear, activity, pitch, roll, yaw, quality
def compare_face(frame1, frame2):
url = "https://recognito.p.rapidapi.com/api/face"
try:
files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
r = requests.post(url=url, files=files, headers=headers)
except:
raise gr.Error("Please select images files!")
faces = None
try:
image1 = Image.open(frame1)
image2 = Image.open(frame2)
face1 = Image.new('RGBA',(150, 150), (80,80,80,0))
face2 = Image.new('RGBA',(150, 150), (80,80,80,0))
liveness1, age1, gender1, emotion1, ethnicity1, mask1, eye1, facehair1, haircolor1, hairtype1, headwear1, activity1, pitch1, roll1, yaw1, quality1 = [None] * 16
liveness2, age2, gender2, emotion2, ethnicity2, mask2, eye2, facehair2, haircolor2, hairtype2, headwear2, activity2, pitch2, roll2, yaw2, quality2 = [None] * 16
res1 = r.json().get('image1')
if res1 is not None and res1:
face = res1.get('detection')
x1 = face.get('x')
y1 = face.get('y')
x2 = x1 + face.get('w')
y2 = y1 + face.get('h')
if x1 < 0:
x1 = 0
if y1 < 0:
y1 = 0
if x2 >= image1.width:
x2 = image1.width - 1
if y2 >= image1.height:
y2 = image1.height - 1
face1 = image1.crop((x1, y1, x2, y2))
face_image_ratio = face1.width / float(face1.height)
resized_w = int(face_image_ratio * 150)
resized_h = 150
face1 = face1.resize((int(resized_w), int(resized_h)))
liveness1, age1, gender1, emotion1, ethnicity1, mask1, eye1, facehair1, haircolor1, hairtype1, headwear1, activity1, pitch1, roll1, yaw1, quality1 = get_attributes(res1)
res2 = r.json().get('image2')
if res2 is not None and res2:
face = res2.get('detection')
x1 = face.get('x')
y1 = face.get('y')
x2 = x1 + face.get('w')
y2 = y1 + face.get('h')
if x1 < 0:
x1 = 0
if y1 < 0:
y1 = 0
if x2 >= image2.width:
x2 = image2.width - 1
if y2 >= image2.height:
y2 = image2.height - 1
face2 = image2.crop((x1, y1, x2, y2))
face_image_ratio = face2.width / float(face2.height)
resized_w = int(face_image_ratio * 150)
resized_h = 150
face2 = face2.resize((int(resized_w), int(resized_h)))
liveness2, age2, gender2, emotion2, ethnicity2, mask2, eye2, facehair2, haircolor2, hairtype2, headwear2, activity2, pitch2, roll2, yaw2, quality2 = get_attributes(res2)
except:
pass
matching_result = ""
if face1 is not None and face2 is not None:
matching_score = r.json().get('matching_score')
if matching_score is not None:
matching_result = """<br/><br/><br/><h1 style="text-align: center;color: #05ee3c;">SAME<br/>PERSON</h1>""" if matching_score >= 0.7 else """<br/><br/><br/><h1 style="text-align: center;color: red;">DIFFERENT<br/>PERSON</h1>"""
return [r.json(), [face1, face2], matching_result,
liveness1, age1, gender1, emotion1, ethnicity1, mask1, eye1, facehair1, haircolor1, hairtype1, headwear1, activity1, pitch1, roll1, yaw1, quality1,
liveness2, age2, gender2, emotion2, ethnicity2, mask2, eye2, facehair2, haircolor2, hairtype2, headwear2, activity2, pitch2, roll2, yaw2, quality2]
# with gr.Blocks(theme='aliabid94/new-theme') as demo:
primary_color = gr.themes.utils.colors.Color(name="recognito",
c50="#05ee3c",
c100="#05ee3c",
c200="#05ee3c",
c300="#05ee3c",
c400="#05ee3c",
c500="#05ee3c",
c600="#05ee3c",
c700="#05ee3c",
c800="#05ee3c",
c900="#05ee3c",
c950="#05ee3c",)
with gr.Blocks(theme=gr.themes.Default(primary_hue=primary_color, secondary_hue=primary_color, radius_size="none")) as demo:
with gr.Row():
with gr.Column(scale=1):
compare_face_input1 = gr.Image(label="Image1", type='filepath', height=270)
gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg', 'examples/4.jpg'],
inputs=compare_face_input1)
compare_face_input2 = gr.Image(label="Image2", type='filepath', height=270)
gr.Examples(['examples/5.jpg', 'examples/6.jpg', 'examples/7.jpg', 'examples/8.jpg'],
inputs=compare_face_input2)
compare_face_button = gr.Button("Face Analysis & Verification", variant="primary", size="lg")
with gr.Column(scale=2):
with gr.Row():
compare_face_output = gr.Gallery(label="Faces", height=230, columns=[2], rows=[1])
with gr.Column(variant="panel"):
compare_result = gr.Markdown("")
with gr.Row():
with gr.Column(variant="panel"):
gr.Markdown("<b>Image 1<b/>")
liveness1 = gr.CheckboxGroup(["GENUINE", "FAKE"], label="Liveness")
age1 = gr.Number(0, label="Age")
gender1 = gr.CheckboxGroup(["MALE", "FEMALE"], label="Gender")
emotion1 = gr.CheckboxGroup(["HAPPINESS", "ANGER", "FEAR", "NEUTRAL", "SADNESS", "SURPRISE"], label="Emotion")
ethnicity1 = gr.CheckboxGroup(["ASIAN", "BLACK", "CAUCASIAN", "EAST_INDIAN"], label="Ethnicity")
mask1 = gr.CheckboxGroup(["LOWER_FACE_MASK", "FULL_FACE_MASK", "OTHER_MASK", "GLASSES", "SUNGLASSES"], label="Mask & Glasses")
eye1 = gr.CheckboxGroup(["LEFT", "RIGHT"], label="Eye Open")
facehair1 = gr.CheckboxGroup(["BEARD", "BRISTLE", "MUSTACHE", "SHAVED"], label="Facial Hair")
haircolor1 = gr.CheckboxGroup(["BLACK", "BLOND", "BROWN"], label="Hair Color")
hairtype1 = gr.CheckboxGroup(["BALD", "SHORT", "MEDIUM", "LONG"], label="Hair Type")
headwear1 = gr.CheckboxGroup(["B_CAP", "CAP", "HAT", "HELMET", "HOOD"], label="Head Wear")
activity1 = gr.CheckboxGroup(["EATING", "PHONE_RECORDING", "PHONE_USE", "SMOKING", "SEATBELT"], label="Activity")
with gr.Row():
pitch1 = gr.Number(0, label="Pitch")
roll1 = gr.Number(0, label="Roll")
yaw1 = gr.Number(0, label="Yaw")
quality1 = gr.Number(0, label="Quality")
with gr.Column(variant="panel"):
gr.Markdown("<b>Image 2<b/>")
liveness2 = gr.CheckboxGroup(["GENUINE", "FAKE"], label="Liveness")
age2 = gr.Number(0, label="Age")
gender2 = gr.CheckboxGroup(["MALE", "FEMALE"], label="Gender")
emotion2 = gr.CheckboxGroup(["HAPPINESS", "ANGER", "FEAR", "NEUTRAL", "SADNESS", "SURPRISE"], label="Emotion")
ethnicity2 = gr.CheckboxGroup(["ASIAN", "BLACK", "CAUCASIAN", "EAST_INDIAN"], label="Ethnicity")
mask2 = gr.CheckboxGroup(["LOWER_FACE_MASK", "FULL_FACE_MASK", "OTHER_MASK", "GLASSES", "SUNGLASSES"], label="Mask & Glasses")
eye2 = gr.CheckboxGroup(["LEFT", "RIGHT"], label="Eye Open")
facehair2 = gr.CheckboxGroup(["BEARD", "BRISTLE", "MUSTACHE", "SHAVED"], label="Facial Hair")
haircolor2 = gr.CheckboxGroup(["BLACK", "BLOND", "BROWN"], label="Hair Color")
hairtype2 = gr.CheckboxGroup(["BALD", "SHORT", "MEDIUM", "LONG"], label="Hair Type")
headwear2 = gr.CheckboxGroup(["B_CAP", "CAP", "HAT", "HELMET", "HOOD"], label="Head Wear")
activity2 = gr.CheckboxGroup(["EATING", "PHONE_RECORDING", "PHONE_USE", "SMOKING", "SEATBELT"], label="Activity")
with gr.Row():
pitch2 = gr.Number(0, label="Pitch")
roll2 = gr.Number(0, label="Roll")
yaw2 = gr.Number(0, label="Yaw")
quality2 = gr.Number(0, label="Quality")
compare_result_output = gr.JSON(label='Result', visible=False)
compare_face_button.click(compare_face, inputs=[compare_face_input1, compare_face_input2], outputs=[compare_result_output, compare_face_output, compare_result,
liveness1, age1, gender1, emotion1, ethnicity1, mask1, eye1, facehair1, haircolor1, hairtype1, headwear1, activity1, pitch1, roll1, yaw1, quality1,
liveness2, age2, gender2, emotion2, ethnicity2, mask2, eye2, facehair2, haircolor2, hairtype2, headwear2, activity2, pitch2, roll2, yaw2, quality2])
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False) |