File size: 1,518 Bytes
c96690b ecaf129 6f21a14 c96690b 6f21a14 c96690b 6f21a14 c96690b 6f21a14 c96690b 6f21a14 ecaf129 c96690b ecaf129 c96690b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import tempfile
from fastapi import FastAPI, File, UploadFile
from gradio_client import Client, handle_file
app = FastAPI()
client = Client("kevansoon/FaceRecognition-LivenessDetection-Demo")
@app.post("/face-recognition")
async def face_recognition_post(image: UploadFile = File(...)):
# Save uploaded image to temp file
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
content = await image.read()
temp_file.write(content)
temp_filepath = temp_file.name
# Hardcoded second image URL
image2_url = "https://qvnhhditkzzeudppuezf.supabase.co/storage/v1/object/public/post-images/post-images/1752289670997-kevan.jpg"
# Call Gradio client with temp file path and URL wrapped in handle_file
result = client.predict(
frame1=handle_file(temp_filepath), # Pass local file path wrapped with handle_file
frame2=handle_file(image2_url), # Pass URL wrapped with handle_file
api_name="/face_compare"
)
# Optional: remove temp file after use if desired
# import os
# os.unlink(temp_filepath)
# Parse the result string as before
str_ = result
start_index = str_.find("Similarity")
sliced_str = str_[start_index:] if start_index != -1 else ""
import re
decimal_match = re.search(r"<td>(0?\.\d+)<\/td>", sliced_str)
decimal_number = float(decimal_match.group(1)) if decimal_match else None
print("Similarity decimal:", decimal_number)
return {"result":str(decimal_number)}
|