diff --git a/gradio/app.py b/gradio/app.py index 551affa..32fd9db 100644 --- a/gradio/app.py +++ b/gradio/app.py @@ -1,142 +1,130 @@ import gradio as gr import os import requests +import json +from PIL import Image -def id_check(frame): - url = "http://127.0.0.1:8082/api/check_id" - files = {'image': open(frame, 'rb')} - r = requests.post(url=url, files=files) +import requests +import base64 +from PIL import Image +from io import BytesIO - html = None - images = None - mrz = None - - table_value = "" - - if r.json().get('MRZ') is not None: - mrz = r.json().get('MRZ') - - for key, value in r.json().items(): - if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position': - continue - - mrz_value = '' - if mrz is not None and mrz.get(key) is not None: - mrz_value = mrz[key] - del mrz[key] - - row_value = ("" - "{key}" - "{value}" - "{mrz_value}" - "".format(key=key, value=value, mrz_value=mrz_value)) - table_value = table_value + row_value - - - if mrz is not None: - for key, value in mrz.items(): - if key == 'MRZ': - value = value.replace('<', '<') - value = value.replace(',', '

') - - row_value = ("" - "{key}" - "{value}" - "{mrz_value}" - "".format(key=key, value='', mrz_value=value)) - table_value = table_value + row_value - - - html = ("" - "" - "" - "" - "" - "" - "{table_value}" - "
FieldValueMRZ
".format(table_value=table_value)) - - table_value = "" - for key, value in r.json().items(): - if key == 'Images': - for image_key, image_value in value.items(): - row_value = ("" - "{key}" - "" - "".format(key=image_key, base64_image=image_value)) - table_value = table_value + row_value - - images = ("" - "" - "" - "" - "" - "{table_value}" - "
FieldImage
".format(table_value=table_value)) - - return [html, images] - -def bank_credit_check(frame): - url = 'http://127.0.0.1:8082/api/check_credit' +def face_detect(frame): + url = "http://127.0.0.1:8083/api/face_detect" files = {'image': open(frame, 'rb')} r = requests.post(url=url, files=files) + response = r.json() + + detections = response.get("detections", {}) + table_rows = "" + face_images = [] + + for face_id, details in detections.items(): + attributes = details.get("attributes", {}) + # landmarks = details.get("landmarks", []) + # position = details.get("position", []) + face_base64 = details.get("face", "") - html = None - table_value = "" - - for key, value in r.json().items(): - if key == 'Status' or key == 'Images': - continue - - row_value = ("" - "{key}" - "{value}" - "".format(key=key, value=value)) - table_value = table_value + row_value - - html = ("" - "" - "" - "" - "" - "{table_value}" - "
FieldValue
".format(table_value=table_value)) - + # Decode face image + face_image = f"" if face_base64 else "N/A" + + # Prepare attributes text without specific keys + keys_to_remove = {"Emotion", "ForeheadCovering", "HeadCovering", "Occlusion", "StrongMakeup"} + filtered_attributes = {key: value for key, value in attributes.items() if key not in keys_to_remove} + + attributes_text = "
".join(f"{key}: {value}" for key, value in filtered_attributes.items()) + + # # Prepare landmarks text + # landmarks_text = ", ".join(str(landmark) for landmark in landmarks) + + # Add table row for the face + table_rows += f""" + + {face_id} + {face_image} + {attributes_text} + + """ + + # Create final HTML table + html = f""" + + + + + + + {table_rows} +
Face IDFace ImageAttributes
+ """ return html -def mrz_barcode_check(frame): - url = 'http://127.0.0.1:8082/api/check_mrz' - files = {'image': open(frame, 'rb')} +def face_match(frame1, frame2): + url = "http://127.0.0.1:8083/api/face_match" + files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')} r = requests.post(url=url, files=files) + response = r.json() + + detections = response.get("detections", []) + matches = response.get("match", []) + detection_rows = "" + match_rows = "" + + # Process detections + for detection in detections: + face_image = detection.get("face", "") + face_img_tag = f"" if face_image else "N/A" + first_face_index = detection.get("firstFaceIndex", "N/A") + second_face_index = detection.get("secondFaceIndex", "N/A") + + detection_rows += f""" + + {first_face_index} + {second_face_index} + {face_img_tag} + + """ - html = None - mrz = None - - table_value = "" - - if r.json().get('MRZ') is not None: - mrz = r.json().get('MRZ') - - # Iterate through the MRZ data and print each key and item - for key, value in mrz.items(): - if key == 'MRZ Code': - value = value.replace('<', '<') - value = value.replace(',', '

') - row_value = ("" - "{key}" - "{value}" - "".format(key=key, value=value)) - table_value = table_value + row_value - - html = ("" - "" - "" - "" - "" - "{table_value}" - "
FieldValue
".format(table_value=table_value)) - - return html + # Process matches + for match in matches: + first_face_index = match.get("firstFaceIndex", "N/A") + second_face_index = match.get("secondFaceIndex", "N/A") + similarity = match.get("similarity", "N/A") + + match_rows += f""" + + {first_face_index} + {second_face_index} + {similarity:.6f} + + """ + + # Create HTML tables + detections_table = f""" +

Face Detection

+ + + + + + + {detection_rows} +
First Face IndexSecond Face IndexFace Image
+ """ + + matches_table = f""" +

Matching Results

+ + + + + + + {match_rows} +
First Face IndexSecond Face IndexSimilarity
+ """ + + return detections_table + matches_table # APP Interface with gr.Blocks() as MiniAIdemo: @@ -145,70 +133,71 @@ def mrz_barcode_check(frame):
-

IDSDK Web Online Demo

+

FaceRecognition SDK Demo

+

Experience our NIST FRVT Top Ranked FaceRecognition, iBeta 2 Certified Face Liveness Detection Engine

-
- +
+ + + + + + + + + + + +
+

""" ) with gr.Tabs(): - with gr.TabItem("ID Card Recognition"): + with gr.TabItem("Face Detection"): with gr.Row(): - with gr.Column(scale=3): - im_id_input = gr.Image(type='filepath', height=300) + with gr.Column(): + im_detect_input = gr.Image(type='filepath', height=300) gr.Examples( [ - os.path.join(os.path.dirname(__file__), "images/id/demo1.jpg"), - os.path.join(os.path.dirname(__file__), "images/id/demo2.png"), - os.path.join(os.path.dirname(__file__), "images/id/demo3.png"), + os.path.join(os.path.dirname(__file__), "images/img1.jpg"), + os.path.join(os.path.dirname(__file__), "images/img2.jpg"), + os.path.join(os.path.dirname(__file__), "images/img3.jpg"), ], - inputs=im_id_input + inputs=im_detect_input ) - btn_f_id = gr.Button("Analysis Document", variant='primary') - with gr.Column(scale=5): - txt_id_output = gr.HTML() - with gr.Column(scale=2): - im_id_output = gr.HTML() - btn_f_id.click(id_check, inputs=im_id_input, outputs=[txt_id_output, im_id_output]) - with gr.TabItem("Bank & Credit Card Recognition"): + btn_f_detect = gr.Button("Detect", variant='primary') + with gr.Column(): + txt_detect_output = gr.HTML() + btn_f_detect.click(face_detect, inputs=im_detect_input, outputs=txt_detect_output) + with gr.Tab("Face Recognition"): with gr.Row(): - with gr.Column(scale=3): - im_card_input = gr.Image(type='filepath', height=300) + with gr.Column(): + im_match_in1 = gr.Image(type='filepath', height=300) gr.Examples( [ - os.path.join(os.path.dirname(__file__), "images/band_credit_card/demo1.jpg"), - os.path.join(os.path.dirname(__file__), "images/band_credit_card/demo2.png"), - os.path.join(os.path.dirname(__file__), "images/band_credit_card/demo3.png"), + "images/img2.jpg", + "images/img3.jpg", + "images/img4.jpg", ], - inputs=im_card_input + inputs=im_match_in1 ) - btn_f_card = gr.Button("Analysis Document", variant='primary') - with gr.Column(scale=5): - txt_card_output = gr.HTML() - btn_f_card.click(bank_credit_check, inputs=im_card_input, outputs=txt_card_output) - with gr.TabItem("MRZ & Barcode Recognition"): - with gr.Row(): - with gr.Column(scale=3): - im_mrz_input = gr.Image(type='filepath', height=300) + with gr.Column(): + im_match_in2 = gr.Image(type='filepath', height=300) gr.Examples( [ - os.path.join(os.path.dirname(__file__), "images/mrz_barcode/demo1.png"), - os.path.join(os.path.dirname(__file__), "images/mrz_barcode/demo2.png"), + "images/img5.jpg", + "images/img6.jpg", + "images/img7.jpg", ], - inputs=im_mrz_input + inputs=im_match_in2 ) - btn_f_mrz = gr.Button("Analysis Document", variant='primary') - with gr.Column(scale=5): - txt_mrz_output = gr.HTML() - btn_f_mrz.click(mrz_barcode_check, inputs=im_mrz_input, outputs=txt_mrz_output) - + with gr.Column(): + txt_match_out = gr.HTML() + btn_f_match = gr.Button("Check Comparing!", variant='primary') + btn_f_match.click(face_match, inputs=[im_match_in1, im_match_in2], outputs=txt_match_out) + if __name__ == "__main__": - MiniAIdemo.launch(server_port=8083, server_name="0.0.0.0") \ No newline at end of file + MiniAIdemo.launch(server_port=8085, server_name="0.0.0.0") \ No newline at end of file diff --git a/gradio/images/band_credit_card/demo1.jpg b/gradio/images/band_credit_card/demo1.jpg deleted file mode 100644 index eacf462..0000000 Binary files a/gradio/images/band_credit_card/demo1.jpg and /dev/null differ diff --git a/gradio/images/band_credit_card/demo2.png b/gradio/images/band_credit_card/demo2.png deleted file mode 100644 index e174659..0000000 Binary files a/gradio/images/band_credit_card/demo2.png and /dev/null differ diff --git a/gradio/images/band_credit_card/demo3.png b/gradio/images/band_credit_card/demo3.png deleted file mode 100644 index 1b92782..0000000 Binary files a/gradio/images/band_credit_card/demo3.png and /dev/null differ diff --git a/gradio/images/id/demo1.jpg b/gradio/images/id/demo1.jpg deleted file mode 100644 index 9294a5e..0000000 Binary files a/gradio/images/id/demo1.jpg and /dev/null differ diff --git a/gradio/images/id/demo2.png b/gradio/images/id/demo2.png deleted file mode 100644 index 92888cb..0000000 Binary files a/gradio/images/id/demo2.png and /dev/null differ diff --git a/gradio/images/id/demo3.png b/gradio/images/id/demo3.png deleted file mode 100644 index e7accd6..0000000 Binary files a/gradio/images/id/demo3.png and /dev/null differ diff --git a/gradio/images/img1.jpg b/gradio/images/img1.jpg new file mode 100644 index 0000000..6f728d5 Binary files /dev/null and b/gradio/images/img1.jpg differ diff --git a/gradio/images/img2.jpg b/gradio/images/img2.jpg new file mode 100644 index 0000000..a0eb3c7 Binary files /dev/null and b/gradio/images/img2.jpg differ diff --git a/gradio/images/img3.jpg b/gradio/images/img3.jpg new file mode 100644 index 0000000..222d9f7 Binary files /dev/null and b/gradio/images/img3.jpg differ diff --git a/gradio/images/img4.jpg b/gradio/images/img4.jpg new file mode 100644 index 0000000..0e28917 Binary files /dev/null and b/gradio/images/img4.jpg differ diff --git a/gradio/images/img5.jpg b/gradio/images/img5.jpg new file mode 100644 index 0000000..21363e5 Binary files /dev/null and b/gradio/images/img5.jpg differ diff --git a/gradio/images/img6.jpg b/gradio/images/img6.jpg new file mode 100644 index 0000000..7ec1253 Binary files /dev/null and b/gradio/images/img6.jpg differ diff --git a/gradio/images/img7.jpg b/gradio/images/img7.jpg new file mode 100644 index 0000000..f689180 Binary files /dev/null and b/gradio/images/img7.jpg differ diff --git a/gradio/images/mrz_barcode/demo1.png b/gradio/images/mrz_barcode/demo1.png deleted file mode 100644 index e7accd6..0000000 Binary files a/gradio/images/mrz_barcode/demo1.png and /dev/null differ diff --git a/gradio/images/mrz_barcode/demo2.png b/gradio/images/mrz_barcode/demo2.png deleted file mode 100644 index 92888cb..0000000 Binary files a/gradio/images/mrz_barcode/demo2.png and /dev/null differ diff --git a/python/check_mrz.py b/python/check_mrz.py deleted file mode 100644 index 1261f7c..0000000 --- a/python/check_mrz.py +++ /dev/null @@ -1,27 +0,0 @@ -import requests - -# URL of the API endpoint -url = 'http://127.0.0.1:8082/api/check_mrz' - -# Path to the image file you want to send -image_path = './test_image/mrz_image.png' - -# Read the image file and encode it as a base64 string -files = {'image': open(image_path, 'rb')} - -try: - # Send POST request - response = requests.post(url, files=files) - - # Check if the request was successful - if response.status_code == 200: - print('Request was successful!') - # Parse the JSON response - response_data = response.content - print('Response Data :', response_data) - else: - print('Request failed with status code:', response.status_code) - print('Response content:', response.text) - -except requests.exceptions.RequestException as e: - print('An error occurred:', e) diff --git a/python/check_id.py b/python/face_detect.py similarity index 64% rename from python/check_id.py rename to python/face_detect.py index 3cc2308..ab451b2 100644 --- a/python/check_id.py +++ b/python/face_detect.py @@ -1,12 +1,12 @@ import requests -# URL of the API endpoint -url = 'http://127.0.0.1:8082/api/check_id' +# URL of the web API endpoint +url = 'http://127.0.0.1:8083/api/face_detect' # Path to the image file you want to send -image_path = './test_image/id_image.jpg' +image_path = './test_image/img1.jpg' -# Read the image file and encode it as a base64 string +# Read the image file and send it as form data files = {'image': open(image_path, 'rb')} try: @@ -17,11 +17,11 @@ if response.status_code == 200: print('Request was successful!') # Parse the JSON response - response_data = response.content - print('Response Data :', response_data) + response_data = response.json() + print('Response Data:', response_data) else: print('Request failed with status code:', response.status_code) print('Response content:', response.text) except requests.exceptions.RequestException as e: - print('An error occurred:', e) + print('An error occurred:', e) \ No newline at end of file diff --git a/python/check_credit.py b/python/face_match.py similarity index 53% rename from python/check_credit.py rename to python/face_match.py index 986e9aa..6616b3a 100644 --- a/python/check_credit.py +++ b/python/face_match.py @@ -1,13 +1,17 @@ import requests -# URL of the API endpoint -url = 'http://127.0.0.1:8082/api/check_credit' +# URL of the web API endpoint +url = 'http://127.0.0.1:8083/api/face_match' # Path to the image file you want to send -image_path = './test_image/bank_card_image.png' +image_path_1 = './test_image/img1.jpg' +image_path_2 = './test_image/img2.jpg' -# Read the image file and encode it as a base64 string -files = {'image': open(image_path, 'rb')} +# Read the image file and send it as form data +files = { + 'image1': open(image_path_1, 'rb'), + 'image2': open(image_path_2, 'rb') + } try: # Send POST request @@ -17,11 +21,11 @@ if response.status_code == 200: print('Request was successful!') # Parse the JSON response - response_data = response.content - print('Response Data :', response_data) + response_data = response.json() + print('Response Data:', response_data) else: print('Request failed with status code:', response.status_code) print('Response content:', response.text) except requests.exceptions.RequestException as e: - print('An error occurred:', e) + print('An error occurred:', e) \ No newline at end of file diff --git a/python/test_image/bank_card_image.png b/python/test_image/bank_card_image.png deleted file mode 100644 index e174659..0000000 Binary files a/python/test_image/bank_card_image.png and /dev/null differ diff --git a/python/test_image/id_image.jpg b/python/test_image/id_image.jpg deleted file mode 100644 index 9294a5e..0000000 Binary files a/python/test_image/id_image.jpg and /dev/null differ diff --git a/python/test_image/img1.jpg b/python/test_image/img1.jpg new file mode 100644 index 0000000..21363e5 Binary files /dev/null and b/python/test_image/img1.jpg differ diff --git a/python/test_image/img2.jpg b/python/test_image/img2.jpg new file mode 100644 index 0000000..a0eb3c7 Binary files /dev/null and b/python/test_image/img2.jpg differ diff --git a/python/test_image/img3.jpg b/python/test_image/img3.jpg new file mode 100644 index 0000000..222d9f7 Binary files /dev/null and b/python/test_image/img3.jpg differ diff --git a/python/test_image/img4.jpg b/python/test_image/img4.jpg new file mode 100644 index 0000000..7ec1253 Binary files /dev/null and b/python/test_image/img4.jpg differ diff --git a/python/test_image/mrz_image.png b/python/test_image/mrz_image.png deleted file mode 100644 index e7accd6..0000000 Binary files a/python/test_image/mrz_image.png and /dev/null differ