Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,36 +5,28 @@ from PIL import Image
|
|
| 5 |
from io import BytesIO
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
|
| 11 |
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY")
|
| 12 |
FLAREAI_API = os.getenv("FLAREAI_API")
|
| 13 |
|
| 14 |
def detect_faces(image):
|
| 15 |
-
"""Send the image to the API and return the processed image and face data."""
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
buffered = BytesIO()
|
| 19 |
image.save(buffered, format="JPEG")
|
| 20 |
buffered.seek(0)
|
| 21 |
|
| 22 |
-
|
| 23 |
upload_url = "https://api.imgbb.com/1/upload"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
files = {'image': buffered}
|
| 27 |
data = {"key": IMGBB_API_KEY}
|
| 28 |
|
| 29 |
response = requests.post(upload_url, data=data, files=files)
|
| 30 |
-
|
| 31 |
if response.status_code != 200:
|
| 32 |
return f"Image upload failed with status code {response.status_code}: {response.text}"
|
| 33 |
|
| 34 |
image_url = response.json()["data"]["url"]
|
| 35 |
print(f"Image uploaded successfully: {image_url}")
|
| 36 |
|
| 37 |
-
|
| 38 |
response = requests.post(FLAREAI_API, json={"image_url": image_url})
|
| 39 |
response.raise_for_status()
|
| 40 |
|
|
@@ -43,27 +35,68 @@ def detect_faces(image):
|
|
| 43 |
detected_image_url = result.get("image_url", "")
|
| 44 |
|
| 45 |
if face_data:
|
| 46 |
-
face_details = "\n".join([
|
| 47 |
-
f"Face {index + 1}: Age: {face['age']}, Emotion: {face['emotion']}, Gender: {face['gender']}, Race: {face['race']}"
|
| 48 |
-
for index, face in enumerate(face_data)
|
| 49 |
-
])
|
| 50 |
else:
|
| 51 |
face_details = "No faces detected."
|
| 52 |
|
| 53 |
-
|
| 54 |
result_image = Image.open(BytesIO(requests.get(detected_image_url).content))
|
| 55 |
-
|
| 56 |
return result_image, face_details
|
| 57 |
except requests.exceptions.RequestException as e:
|
| 58 |
return f"An error occurred: {e}", ""
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
iface = gr.Interface(
|
| 62 |
fn=detect_faces,
|
| 63 |
-
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 64 |
outputs=[gr.Image(type="pil", label="Processed Image"), gr.Textbox(label="Face Details")],
|
| 65 |
title="FlareAI Face Detection",
|
| 66 |
-
description="Made with ❤️ by
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
iface.launch()
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
+
load_dotenv()
|
|
|
|
| 9 |
|
| 10 |
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY")
|
| 11 |
FLAREAI_API = os.getenv("FLAREAI_API")
|
| 12 |
|
| 13 |
def detect_faces(image):
|
|
|
|
| 14 |
try:
|
|
|
|
| 15 |
buffered = BytesIO()
|
| 16 |
image.save(buffered, format="JPEG")
|
| 17 |
buffered.seek(0)
|
| 18 |
|
|
|
|
| 19 |
upload_url = "https://api.imgbb.com/1/upload"
|
|
|
|
|
|
|
| 20 |
files = {'image': buffered}
|
| 21 |
data = {"key": IMGBB_API_KEY}
|
| 22 |
|
| 23 |
response = requests.post(upload_url, data=data, files=files)
|
|
|
|
| 24 |
if response.status_code != 200:
|
| 25 |
return f"Image upload failed with status code {response.status_code}: {response.text}"
|
| 26 |
|
| 27 |
image_url = response.json()["data"]["url"]
|
| 28 |
print(f"Image uploaded successfully: {image_url}")
|
| 29 |
|
|
|
|
| 30 |
response = requests.post(FLAREAI_API, json={"image_url": image_url})
|
| 31 |
response.raise_for_status()
|
| 32 |
|
|
|
|
| 35 |
detected_image_url = result.get("image_url", "")
|
| 36 |
|
| 37 |
if face_data:
|
| 38 |
+
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)])
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
face_details = "No faces detected."
|
| 41 |
|
|
|
|
| 42 |
result_image = Image.open(BytesIO(requests.get(detected_image_url).content))
|
|
|
|
| 43 |
return result_image, face_details
|
| 44 |
except requests.exceptions.RequestException as e:
|
| 45 |
return f"An error occurred: {e}", ""
|
| 46 |
|
| 47 |
|
| 48 |
+
css = """
|
| 49 |
+
.gradio-container {
|
| 50 |
+
font-family: 'Arial', sans-serif;
|
| 51 |
+
background-color: #f9f9f9;
|
| 52 |
+
}
|
| 53 |
+
.gradio-title {
|
| 54 |
+
color: #4e5b66;
|
| 55 |
+
font-size: 30px;
|
| 56 |
+
font-weight: 600;
|
| 57 |
+
text-align: center;
|
| 58 |
+
margin-top: 30px;
|
| 59 |
+
}
|
| 60 |
+
.gradio-description {
|
| 61 |
+
color: #7a7a7a;
|
| 62 |
+
font-size: 16px;
|
| 63 |
+
text-align: center;
|
| 64 |
+
margin-bottom: 30px;
|
| 65 |
+
}
|
| 66 |
+
.gradio-input {
|
| 67 |
+
border-radius: 12px;
|
| 68 |
+
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
|
| 69 |
+
}
|
| 70 |
+
.gradio-output {
|
| 71 |
+
border-radius: 12px;
|
| 72 |
+
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
|
| 73 |
+
}
|
| 74 |
+
.gradio-button {
|
| 75 |
+
background-color: #4CAF50;
|
| 76 |
+
color: white;
|
| 77 |
+
font-weight: 600;
|
| 78 |
+
border-radius: 12px;
|
| 79 |
+
padding: 10px 20px;
|
| 80 |
+
margin-top: 20px;
|
| 81 |
+
border: none;
|
| 82 |
+
}
|
| 83 |
+
.gradio-button:hover {
|
| 84 |
+
background-color: #45a049;
|
| 85 |
+
}
|
| 86 |
+
.gradio-image {
|
| 87 |
+
border-radius: 12px;
|
| 88 |
+
border: 2px solid #ccc;
|
| 89 |
+
}
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
iface = gr.Interface(
|
| 93 |
fn=detect_faces,
|
| 94 |
+
inputs=gr.Image(type="pil", label="Upload Image", tool="editor"),
|
| 95 |
outputs=[gr.Image(type="pil", label="Processed Image"), gr.Textbox(label="Face Details")],
|
| 96 |
title="FlareAI Face Detection",
|
| 97 |
+
description="Made with ❤️ by Skycodee",
|
| 98 |
+
theme="huggingface",
|
| 99 |
+
css=css
|
| 100 |
)
|
| 101 |
|
| 102 |
iface.launch()
|