edreesi commited on
Commit
17316c3
·
verified ·
1 Parent(s): 5ffafb9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +311 -0
app.py ADDED
@@ -0,0 +1,311 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import os
3
+ import io
4
+ import gradio as gr
5
+ import json
6
+ import requests
7
+ from PIL import Image
8
+
9
+ css = """
10
+ .example-image img{
11
+ display: flex; /* Use flexbox to align items */
12
+ justify-content: center; /* Center the image horizontally */
13
+ align-items: center; /* Center the image vertically */
14
+ height: 300px; /* Set the height of the container */
15
+ object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
16
+ }
17
+ .example-image img{
18
+ display: flex; /* Use flexbox to align items */
19
+ text-align: center;
20
+ justify-content: center; /* Center the image horizontally */
21
+ align-items: center; /* Center the image vertically */
22
+ height: 350px; /* Set the height of the container */
23
+ object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
24
+ }
25
+ .markdown-success-container {
26
+ background-color: #F6FFED;
27
+ padding: 20px;
28
+ margin: 20px;
29
+ border-radius: 1px;
30
+ border: 2px solid green;
31
+ text-align: center;
32
+ }
33
+ .markdown-fail-container {
34
+ background-color: #FFF1F0;
35
+ padding: 20px;
36
+ margin: 20px;
37
+ border-radius: 1px;
38
+ border: 2px solid red;
39
+ text-align: center;
40
+ }
41
+ .block-background {
42
+ # background-color: #202020; /* Set your desired background color */
43
+ border-radius: 5px;
44
+ }
45
+ """
46
+
47
+ screenReplayThreshold = 0.5
48
+ portraitReplaceThreshold = 0.5
49
+ printedCopyThreshold = 0.5
50
+
51
+ def find_key_in_dict(d, target_key):
52
+ for key, value in d.items():
53
+ if key == target_key:
54
+ return value
55
+ elif isinstance(value, dict): # If the value is a dictionary, search recursively
56
+ result = find_key_in_dict(value, target_key)
57
+ if result is not None:
58
+ return result
59
+ return None
60
+
61
+ def json_to_html_table(data, image_keys):
62
+ html = "<table border='1' style='border-collapse: collapse; width: 100%;'>"
63
+ for key, value in data.items():
64
+ if isinstance(value, dict):
65
+ html += f"<tr><td colspan='2'><strong>{key}</strong></td></tr>"
66
+ for sub_key, sub_value in value.items():
67
+ if sub_key in image_keys:
68
+ html += f"<tr><td>{sub_key}</td><td><img src='data:image/png;base64,{sub_value}' width = '200' height= '100' /></td></tr>"
69
+ else:
70
+ html += f"<tr><td>{sub_key}</td><td>{sub_value}</td></tr>"
71
+ else:
72
+ if key in image_keys:
73
+ html += f"<tr><td>{key}</td><td><img src='data:image/png;base64,{value}' width = '200' height= '100' /></td></tr>"
74
+ else:
75
+ html += f"<tr><td>{key}</td><td>{value}</td></tr>"
76
+
77
+ html += "</table>"
78
+ return html
79
+
80
+ def check_liveness(frame):
81
+ if frame is None:
82
+ liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
83
+ return [liveness_result, {"status": "error", "result": "select image file!"}]
84
+
85
+ img_bytes = io.BytesIO()
86
+ Image.open(frame).save(img_bytes, format="JPEG")
87
+ img_bytes.seek(0)
88
+
89
+ url = "https://recognito-iddocumentlivenessdetection.p.rapidapi.com/process_image"
90
+
91
+ try:
92
+ files = {'image': img_bytes}
93
+ headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
94
+
95
+ result = requests.post(url=url, files=files, headers=headers)
96
+ except:
97
+ liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
98
+ return [liveness_result, {"status": "error", "result": "failed to open file!"}]
99
+
100
+ if result.ok:
101
+ json_result = result.json()
102
+ if json_result.get("resultCode") == "Error":
103
+ liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
104
+ return [liveness_result, {"status": "error", "result": "server error!"}]
105
+
106
+ process_results = json_result.get("result")
107
+ status = process_results.get("status")
108
+ if status == "Ok":
109
+ screenReply = process_results.get("screenReply")
110
+ portraitReplace = process_results.get("portraitReplace")
111
+ printedCopy = process_results.get("printedCopy")
112
+ liveness_result = f"""<div class="markdown-success-container"><p style="text-align: center; font-size: 20px; color: green;">Liveness Check: GENUINE</p></div>"""
113
+
114
+ # Check for "Spoof" condition
115
+ if screenReply < screenReplayThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold:
116
+ liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check: SPOOF</p></div>"""
117
+
118
+ json_output = {"Screen Replay Check": "Failed" if screenReply < screenReplayThreshold else "Success",
119
+ "Portrait Replace Check": "Failed" if portraitReplace < portraitReplaceThreshold else "Success",
120
+ "Printed Cutout Check": "Failed" if printedCopy < printedCopyThreshold else "Success"}
121
+ # Update json_result with the modified process_results
122
+ return [liveness_result, json_output]
123
+
124
+ liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
125
+ return [liveness_result, {"status": "error", "result": "document not found!"}]
126
+ else:
127
+ liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
128
+ return [liveness_result, {"status": "error", "result": f"{result.text}"}]
129
+
130
+ def idcard_recognition(frame1):
131
+ # url = "https://recognito-iddocumentrecognition.p.rapidapi.com/api/read_idcard"
132
+ url = "https://api.cortex.cerebrium.ai/v4/p-4f1d877e/my-first-project/process-image/"
133
+ # url = "https://edreesi-ocr-api.hf.space/process-image/"
134
+
135
+
136
+
137
+ files = None
138
+ if frame1 is not None:
139
+ # Open the image using Pillow
140
+ img = Image.open(frame1).convert("RGB") # Convert to RGB to remove alpha channels
141
+ img_bytes = io.BytesIO()
142
+
143
+ # Save the image in JPEG format with consistent quality
144
+ img.save(img_bytes, format="JPEG", quality=95, optimize=True, exif=b"") # Strip EXIF metadata
145
+ img_bytes.seek(0) # Reset the file pointer
146
+
147
+ # Log the file size for debugging
148
+ print("Gradio File Size:", len(img_bytes.getvalue()), "bytes")
149
+
150
+ # Prepare the files payload
151
+ files = [
152
+ ('file', ('image.jpg', img_bytes, 'image/jpeg'))
153
+ ]
154
+ else:
155
+ return ['', None, None]
156
+
157
+ # headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
158
+ headers = {}
159
+ # r = requests.post(url=url, files=files, headers=headers)
160
+
161
+ # r = requests.request("POST", url, headers=headers, data={}, files=files)
162
+ payload = json.dumps({"prompt": "your value here"})
163
+
164
+ headers = {
165
+ 'Authorization': 'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm9qZWN0SWQiOiJwLTRmMWQ4NzdlIiwiaWF0IjoxNzM5MjM2NjA5LCJleHAiOjIwNTQ4MTI2MDl9.0LH0iOnqHZfKTH4GF5iTZ4qNj5vylCryo8rBnErljsq2qD2cpVTetCqhKtnbstTUEjuv6MAJw9jt58z-QNJfYLK9sJcnBhawTR3iM2Ap_bFyjlzg2LbgkwRPjUVJkkcuCRhBKyebXwvqQBvWyOtMq6UekauumbmYBRbA2-T4u343YD4tO2xIfsTsTXznALp1SechjRuys-3xo3ZQbUs05_p38fOFucKI-abc91Eq6sIOkLFjYEM68yuV0UBWl-OSpPu66e0SClroAVlKFDMPS9MY0Jr7X1pBYX4jew6vozj9D8Y-HS-KkdPFqJ7HrZOfQd0wGUgYJHyC58yReWXaRQ',
166
+ # 'Content-Type': 'application/json'
167
+ }
168
+
169
+ r = requests.request("POST", url, headers=headers, data={}, files=files)
170
+ # print(r.text)
171
+ print("Status Code:", r.status_code)
172
+ print("r Body:", r.text)
173
+ # r = requests.post(url=url, files=files, headers=headers)
174
+ print('the result is', r.json())
175
+ images = None
176
+ rawValues = {}
177
+ image_table_value = ""
178
+ result_table_dict = {
179
+ 'portrait':'',
180
+ 'type':'',
181
+ 'score':'',
182
+ # 'countryName':'',
183
+ 'FullName':'',
184
+ 'Gender':'',
185
+ 'PlaceOfBirth':'',
186
+ 'DateOfBirth':'',
187
+ 'IdentityNumber':'',
188
+ 'dateOfIssue':'',
189
+ 'dateOfExpiry':'',
190
+ }
191
+
192
+ if 'data' in r.json():
193
+ data = r.json()['data']
194
+ for key, value in data.items():
195
+ if key == 'faceImage':
196
+ # Assign faceImage to the portrait field
197
+ result_table_dict['portrait'] = value
198
+ elif key == 'barcodeImage':
199
+ # Add barcodeImage to the result dictionary
200
+ result_table_dict['barcodeImage'] = value
201
+ else:
202
+ # Add other fields to the result dictionary
203
+ result_table_dict[key] = value
204
+
205
+ # Generate HTML for images
206
+ image_table_value = ""
207
+ if 'barcodeImage' in data:
208
+ image_table_value += (
209
+ "<tr>"
210
+ f"<td>barcodeImage</td>"
211
+ f"<td><img src='data:image/png;base64,{data['barcodeImage']}' width='200' height='100' /></td>"
212
+ "</tr>"
213
+ )
214
+
215
+ # Generate the final HTML table for images
216
+ images = (
217
+ "<table>"
218
+ "<tr>"
219
+ "<th>Field</th>"
220
+ "<th>Image</th>"
221
+ "</tr>"
222
+ f"{image_table_value}"
223
+ "</table>"
224
+ )
225
+
226
+ # Prepare raw values for JSON output
227
+ for key, value in r.json().items():
228
+ if key == 'data':
229
+ if 'faceImage' in value:
230
+ del value['faceImage']
231
+ if 'barcodeImage' in value:
232
+ del value['barcodeImage']
233
+ rawValues[key] = value
234
+ else:
235
+ rawValues[key] = value
236
+
237
+ # Generate the result HTML table
238
+ result = json_to_html_table(result_table_dict, {'portrait', 'barcodeImage'})
239
+ json_result = json.dumps(rawValues, indent=6)
240
+ return [result, json_result, images]
241
+
242
+ def launch_demo():
243
+ with gr.Blocks(css=css) as demo:
244
+ gr.Markdown(
245
+ f"""
246
+
247
+ <p style="font-size: 20px; font-weight: bold;">📘 Product Documentation</p>
248
+ <div style="display: flex; align-items: center;">
249
+ &emsp;&emsp;<a href="" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/05/book.png" style="width: 48px; margin-right: 5px;"/></a>
250
+ </div>
251
+ <p style="font-size: 20px; font-weight: bold;">🏠 Visit Recognito</p>
252
+
253
+ <br/>
254
+ """
255
+ )
256
+
257
+ with gr.Tabs():
258
+ with gr.Tab("ID Document Recognition"):
259
+ with gr.Row():
260
+ with gr.Column(scale=6):
261
+ with gr.Row():
262
+ with gr.Column(scale=6):
263
+ id_image_input1 = gr.Image(type='filepath', label='ID Card Image', elem_classes="example-image")
264
+ # with gr.Column(scale=3):
265
+ # id_image_input2 = gr.Image(type='filepath', label='Back', elem_classes="example-image")
266
+
267
+ with gr.Row():
268
+ id_examples = gr.Examples(
269
+ examples=[['examples/1_f.png', 'examples/1_b.png'],
270
+ ['examples/2_f.png', 'examples/2_b.png'],
271
+ ['examples/3_f.png', 'examples/3_b.png'],
272
+ ['examples/4.png', None]],
273
+ inputs=[id_image_input1, id_image_input1],
274
+ outputs=None,
275
+ fn=idcard_recognition
276
+ )
277
+
278
+ with gr.Blocks():
279
+ with gr.Column(scale=4, min_width=400, elem_classes="block-background"):
280
+ id_recognition_button = gr.Button("ID Card Recognition", variant="primary", size="lg")
281
+
282
+ with gr.Tab("Key Fields"):
283
+ id_result_output = gr.HTML()
284
+ with gr.Tab("Raw JSON"):
285
+ json_result_output = gr.JSON()
286
+ with gr.Tab("Images"):
287
+ image_result_output = gr.HTML()
288
+
289
+ id_recognition_button.click(idcard_recognition, inputs=id_image_input1, outputs=[id_result_output, json_result_output, image_result_output])
290
+
291
+ with gr.Tab("ID Document Liveness Detection"):
292
+ with gr.Row():
293
+ with gr.Column(scale=1):
294
+ id_image_input = gr.Image(label="Image", type='filepath', elem_classes="example-image")
295
+ gr.Examples(examples=['examples/1_f.png', 'examples/2_f.png', 'examples/3_f.png', 'examples/4.png'], inputs=id_image_input)
296
+
297
+ with gr.Blocks():
298
+ with gr.Column(scale=1, elem_classes="block-background"):
299
+ check_liveness_button = gr.Button("Check Document Liveness", variant="primary", size="lg")
300
+
301
+ liveness_result = gr.Markdown("")
302
+ json_output = gr.JSON()
303
+
304
+ check_liveness_button.click(check_liveness, inputs=id_image_input, outputs=[liveness_result, json_output])
305
+
306
+ gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification&labelColor=%2337d67a&countColor=%23263759&style=flat" /></a>')
307
+
308
+ demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
309
+
310
+ if __name__ == '__main__':
311
+ launch_demo()