Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,43 +9,54 @@ load_dotenv()
|
|
| 9 |
|
| 10 |
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY")
|
| 11 |
FLAREAI_API = os.getenv("FLAREAI_API")
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
try:
|
| 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 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
except requests.exceptions.RequestException as e:
|
| 45 |
return f"An error occurred: {e}", ""
|
| 46 |
|
| 47 |
-
|
| 48 |
-
# Professional CSS to style the Gradio UI
|
| 49 |
css = """
|
| 50 |
.gradio-container {
|
| 51 |
font-family: 'Roboto', sans-serif;
|
|
@@ -114,14 +125,13 @@ css = """
|
|
| 114 |
}
|
| 115 |
"""
|
| 116 |
|
| 117 |
-
# Gradio Interface
|
| 118 |
iface = gr.Interface(
|
| 119 |
-
fn=
|
| 120 |
-
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 121 |
outputs=[gr.Image(type="pil", label="Processed Image"), gr.Textbox(label="Face Details")],
|
| 122 |
-
title="FlareAI Face Detection",
|
| 123 |
-
description="
|
| 124 |
-
theme="huggingface",
|
| 125 |
css=css
|
| 126 |
)
|
| 127 |
|
|
|
|
| 9 |
|
| 10 |
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY")
|
| 11 |
FLAREAI_API = os.getenv("FLAREAI_API")
|
| 12 |
+
VIDEO_API_URL = os.getenv("VIDEO_API")
|
| 13 |
|
| 14 |
+
def detect_faces_or_landmarks(input_data, is_video=False):
|
| 15 |
try:
|
| 16 |
+
if is_video:
|
| 17 |
+
|
| 18 |
+
video_url = input_data
|
| 19 |
+
payload = {"video_url": video_url}
|
| 20 |
+
|
| 21 |
+
response = requests.post(VIDEO_API_URL, json=payload)
|
| 22 |
+
if response.status_code != 200:
|
| 23 |
+
return f"Video processing failed with status code {response.status_code}: {response.text}"
|
| 24 |
|
| 25 |
+
video_url = response.json().get("video_url", "")
|
| 26 |
+
return gr.Video.update(value=video_url), "Landmarks detected on video!"
|
| 27 |
+
|
| 28 |
+
else:
|
| 29 |
+
|
| 30 |
+
buffered = BytesIO()
|
| 31 |
+
input_data.save(buffered, format="JPEG")
|
| 32 |
+
buffered.seek(0)
|
| 33 |
|
| 34 |
+
upload_url = "https://api.imgbb.com/1/upload"
|
| 35 |
+
files = {'image': buffered}
|
| 36 |
+
data = {"key": IMGBB_API_KEY}
|
| 37 |
|
| 38 |
+
response = requests.post(upload_url, data=data, files=files)
|
| 39 |
+
if response.status_code != 200:
|
| 40 |
+
return f"Image upload failed with status code {response.status_code}: {response.text}"
|
| 41 |
|
| 42 |
+
image_url = response.json()["data"]["url"]
|
| 43 |
+
response = requests.post(FLAREAI_API, json={"image_url": image_url})
|
| 44 |
+
response.raise_for_status()
|
| 45 |
|
| 46 |
+
result = response.json()
|
| 47 |
+
face_data = result.get("face_data", [])
|
| 48 |
+
detected_image_url = result.get("image_url", "")
|
| 49 |
|
| 50 |
+
if face_data:
|
| 51 |
+
face_details = "\n".join([f"Face {index + 1}: Age: {face['age']}, Emotion: {face['emotion']}, Gender: {face['gender']}, Race: {face['race']}" for index, face in enumerate(face_data)])
|
| 52 |
+
else:
|
| 53 |
+
face_details = "No faces detected."
|
| 54 |
|
| 55 |
+
result_image = Image.open(BytesIO(requests.get(detected_image_url).content))
|
| 56 |
+
return result_image, face_details
|
| 57 |
except requests.exceptions.RequestException as e:
|
| 58 |
return f"An error occurred: {e}", ""
|
| 59 |
|
|
|
|
|
|
|
| 60 |
css = """
|
| 61 |
.gradio-container {
|
| 62 |
font-family: 'Roboto', sans-serif;
|
|
|
|
| 125 |
}
|
| 126 |
"""
|
| 127 |
|
|
|
|
| 128 |
iface = gr.Interface(
|
| 129 |
+
fn=detect_faces_or_landmarks,
|
| 130 |
+
inputs=[gr.Image(type="pil", label="Upload Image"), gr.Video(type="url", label="Upload Video URL")],
|
| 131 |
outputs=[gr.Image(type="pil", label="Processed Image"), gr.Textbox(label="Face Details")],
|
| 132 |
+
title="FlareAI Face Detection & Video Landmark Detection",
|
| 133 |
+
description="Upload an image or video URL to detect faces and landmarks using AI.",
|
| 134 |
+
theme="huggingface",
|
| 135 |
css=css
|
| 136 |
)
|
| 137 |
|