Upload 3 files
Browse files- app.py +52 -0
- examples/1.jpg +0 -0
- examples/2.jpg +0 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
def image_to_base64(image):
|
| 9 |
+
buffered = BytesIO()
|
| 10 |
+
image.save(buffered, format="PNG")
|
| 11 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 12 |
+
|
| 13 |
+
def base64_to_image(base64_str):
|
| 14 |
+
return Image.open(BytesIO(base64.b64decode(base64_str + '=' * (-len(base64_str) % 4))))
|
| 15 |
+
|
| 16 |
+
def search_face(file):
|
| 17 |
+
url = os.environ.get("SERVER_URL")
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
image = Image.open(file)
|
| 21 |
+
image_base64 = image_to_base64(image)
|
| 22 |
+
r = requests.post(url=url, json={"token": os.environ.get("ACCESS_TOKEN"), "type": "novip", "image": image_base64})
|
| 23 |
+
except:
|
| 24 |
+
raise gr.Error("Please select image file!")
|
| 25 |
+
|
| 26 |
+
if r.text == "No matches":
|
| 27 |
+
gr.Info("No images found.")
|
| 28 |
+
return []
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
res = r.json().get('img_array')
|
| 32 |
+
out_array = []
|
| 33 |
+
for item in res:
|
| 34 |
+
out_array.append((base64_to_image(item["image"]), item["url"] + "*********"))
|
| 35 |
+
return out_array
|
| 36 |
+
except:
|
| 37 |
+
raise gr.Error("Try again.")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
with gr.Row():
|
| 42 |
+
with gr.Column(scale=1):
|
| 43 |
+
image = gr.Image(type='filepath', height=480)
|
| 44 |
+
gr.Examples(['examples/1.jpg', 'examples/2.jpg'],
|
| 45 |
+
inputs=image)
|
| 46 |
+
search_face_button = gr.Button("Search Face")
|
| 47 |
+
with gr.Column(scale=2):
|
| 48 |
+
output = gr.Gallery(label="Found Images", columns=[4], object_fit="contain", height="auto")
|
| 49 |
+
|
| 50 |
+
search_face_button.click(search_face, inputs=[image], outputs=[output])
|
| 51 |
+
|
| 52 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
examples/1.jpg
ADDED
|
examples/2.jpg
ADDED
|