| import gradio as gr | |
| import requests | |
| import json | |
| def face_liveness(frame): | |
| url = "http://127.0.0.1:8000/api/liveness" | |
| files = None | |
| if frame is None: | |
| return ['', None] | |
| files = {'image': open(frame, 'rb')} | |
| r = requests.post(url=url, files=files) | |
| return r.json() | |
| css = """ | |
| .example-image img{ | |
| display: flex; /* Use flexbox to align items */ | |
| justify-content: center; /* Center the image horizontally */ | |
| align-items: center; /* Center the image vertically */ | |
| height: 300px; /* Set the height of the container */ | |
| object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */ | |
| } | |
| .example-image{ | |
| display: flex; /* Use flexbox to align items */ | |
| justify-content: center; /* Center the image horizontally */ | |
| align-items: center; /* Center the image vertically */ | |
| height: 350px; /* Set the height of the container */ | |
| object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */ | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| gr.Markdown( | |
| """ | |
| # Face Liveness Detection | |
| Get your own Face Liveness Detection Server by duplicating this space.<br/> | |
| Or run on your own machine using docker.<br/> | |
| ```docker run -it -p 7860:7860 --platform=linux/amd64 \ | |
| -e LICENSE_KEY="YOUR_VALUE_HERE" \ | |
| registry.hf.space/faceonlive-face-liveness-detection-sdk:latest ```<br/><br/> | |
| Contact us at https://faceonlive.com for issues and support.<br/> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=5): | |
| image_input = gr.Image(type='filepath', elem_classes="example-image") | |
| gr.Examples(['gradio/examples/1.jpg', 'gradio/examples/2.jpg', 'gradio/examples/3.jpg', 'gradio/examples/4.jpg'], | |
| inputs=image_input) | |
| face_liveness_button = gr.Button("Check Liveness") | |
| with gr.Column(scale=5): | |
| liveness_result_output = gr.JSON() | |
| face_liveness_button.click(face_liveness, inputs=image_input, outputs=liveness_result_output, api_name=False) | |
| gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FFace-Liveness-Detection-SDK"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FFace-Liveness-Detection-SDK&labelColor=%23ff8a65&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>') | |
| demo.queue(api_open=False).launch(server_name="0.0.0.0", server_port=7860, show_api=False) |