Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,25 +11,43 @@ WORKSPACE = "naveen-kumar-hnmil"
|
|
| 11 |
WORKFLOW = "detect-count-and-visualize-5"
|
| 12 |
|
| 13 |
def detect_image(image):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
return result_img
|
| 33 |
|
| 34 |
# Gradio UI
|
| 35 |
gr.Interface(
|
|
|
|
| 11 |
WORKFLOW = "detect-count-and-visualize-5"
|
| 12 |
|
| 13 |
def detect_image(image):
|
| 14 |
+
try:
|
| 15 |
+
# Convert PIL to bytes
|
| 16 |
+
buffered = BytesIO()
|
| 17 |
+
image.save(buffered, format="JPEG")
|
| 18 |
+
image_bytes = buffered.getvalue()
|
| 19 |
|
| 20 |
+
# Build full API endpoint
|
| 21 |
+
endpoint = f"{API_URL}/{WORKSPACE}/{WORKFLOW}?api_key={API_KEY}"
|
| 22 |
|
| 23 |
+
# Make POST request to Roboflow
|
| 24 |
+
response = requests.post(
|
| 25 |
+
endpoint,
|
| 26 |
+
files={"file": image_bytes},
|
| 27 |
+
data={"confidence": "0.5", "overlap": "0.4"}
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
# Log raw response
|
| 31 |
+
print("Roboflow status:", response.status_code)
|
| 32 |
+
print("Roboflow response:", response.text)
|
| 33 |
|
| 34 |
+
if response.status_code != 200:
|
| 35 |
+
return f"Roboflow error: {response.text}"
|
| 36 |
+
|
| 37 |
+
json_response = response.json()
|
| 38 |
+
if "image" not in json_response or "url" not in json_response["image"]:
|
| 39 |
+
return f"Unexpected response format: {json_response}"
|
| 40 |
+
|
| 41 |
+
# Download result image
|
| 42 |
+
image_url = json_response["image"]["url"]
|
| 43 |
+
result = requests.get(image_url)
|
| 44 |
+
result_img = Image.open(BytesIO(result.content))
|
| 45 |
+
|
| 46 |
+
return result_img
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error: {str(e)}"
|
| 50 |
|
|
|
|
| 51 |
|
| 52 |
# Gradio UI
|
| 53 |
gr.Interface(
|