Turing311 commited on
Commit
629e59e
1 Parent(s): abbc3ea
Files changed (2) hide show
  1. app.py +110 -0
  2. push.bat +3 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ from PIL import Image
5
+
6
+ def compare_face(frame1, frame2):
7
+ url = "https://recognito.p.rapidapi.com/api/face"
8
+ files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
9
+ headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
10
+
11
+ r = requests.post(url=url, files=files)
12
+ faces = None
13
+
14
+ try:
15
+ image1 = Image.open(frame1)
16
+ image2 = Image.open(frame2)
17
+
18
+ face1 = None
19
+ face2 = None
20
+ image1 = r.json().get('image1')
21
+ if image1.get('detection') is not None:
22
+ face = image1.get('face1')
23
+ x1 = face.get('x')
24
+ y1 = face.get('y')
25
+ x2 = x1 + face.get('w')
26
+ y2 = y1 + face.get('h')
27
+ if x1 < 0:
28
+ x1 = 0
29
+ if y1 < 0:
30
+ y1 = 0
31
+ if x2 >= image1.width:
32
+ x2 = image1.width - 1
33
+ if y2 >= image1.height:
34
+ y2 = image1.height - 1
35
+
36
+ face1 = image1.crop((x1, y1, x2, y2))
37
+ face_image_ratio = face1.width / float(face1.height)
38
+ resized_w = int(face_image_ratio * 150)
39
+ resized_h = 150
40
+
41
+ face1 = face1.resize((int(resized_w), int(resized_h)))
42
+
43
+ image2 = r.json().get('image1')
44
+ if image2.get('face2') is not None:
45
+ face = image2.get('face2')
46
+ x1 = face.get('x')
47
+ y1 = face.get('y')
48
+ x2 = x1 + face.get('w')
49
+ y2 = y1 + face.get('h')
50
+
51
+ if x1 < 0:
52
+ x1 = 0
53
+ if y1 < 0:
54
+ y1 = 0
55
+ if x2 >= image2.width:
56
+ x2 = image2.width - 1
57
+ if y2 >= image2.height:
58
+ y2 = image2.height - 1
59
+
60
+ face2 = image2.crop((x1, y1, x2, y2))
61
+ face_image_ratio = face2.width / float(face2.height)
62
+ resized_w = int(face_image_ratio * 150)
63
+ resized_h = 150
64
+
65
+ face2 = face2.resize((int(resized_w), int(resized_h)))
66
+
67
+ if face1 is not None and face2 is not None:
68
+ new_image = Image.new('RGB',(face1.width + face2.width + 10, 150), (80,80,80))
69
+
70
+ new_image.paste(face1,(0,0))
71
+ new_image.paste(face2,(face1.width + 10, 0))
72
+ faces = new_image.copy()
73
+ elif face1 is not None and face2 is None:
74
+ new_image = Image.new('RGB',(face1.width + face1.width + 10, 150), (80,80,80))
75
+
76
+ new_image.paste(face1,(0,0))
77
+ faces = new_image.copy()
78
+ elif face1 is None and face2 is not None:
79
+ new_image = Image.new('RGB',(face2.width + face2.width + 10, 150), (80,80,80))
80
+
81
+ new_image.paste(face2,(face2.width + 10, 0))
82
+ faces = new_image.copy()
83
+ except:
84
+ pass
85
+
86
+ return [r.json(), faces]
87
+
88
+ with gr.Blocks() as demo:
89
+ gr.Markdown(
90
+ """
91
+ # Recognito Face Analysis
92
+ """
93
+ )
94
+ with gr.Row():
95
+ with gr.Column():
96
+ compare_face_input1 = gr.Image(type='filepath', height=480)
97
+ gr.Examples(['gradio/examples/1.jpg', 'gradio/examples/2.jpg'],
98
+ inputs=compare_face_input1)
99
+ compare_face_button = gr.Button("Compare Face")
100
+ with gr.Column():
101
+ compare_face_input2 = gr.Image(type='filepath', height=480)
102
+ gr.Examples(['gradio/examples/3.jpg', 'gradio/examples/4.jpg'],
103
+ inputs=compare_face_input2)
104
+ with gr.Column():
105
+ compare_face_output = gr.Image(type="pil", height=150)
106
+ compare_result_output = gr.JSON(label='Result')
107
+
108
+ compare_face_button.click(compare_face, inputs=[compare_face_input1, compare_face_input2], outputs=[compare_result_output, compare_face_output])
109
+
110
+ demo.launch(server_name="0.0.0.0", server_port=7860)
push.bat ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ git add .
2
+ git commit -m %1
3
+ git push