Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,52 +3,54 @@ import requests
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
-
# Roboflow
|
| 7 |
API_URL = "https://detect.roboflow.com"
|
| 8 |
API_KEY = "dxkgGGHSZ3DI8XzVn29U"
|
| 9 |
WORKSPACE = "naveen-kumar-hnmil"
|
| 10 |
WORKFLOW = "detect-count-and-visualize-5"
|
| 11 |
|
| 12 |
-
# Detection function with logging
|
| 13 |
def detect_image_with_log(image):
|
| 14 |
try:
|
| 15 |
-
# Convert
|
| 16 |
buffered = BytesIO()
|
| 17 |
image.save(buffered, format="JPEG")
|
| 18 |
image_bytes = buffered.getvalue()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
endpoint = f"{API_URL}/{WORKSPACE}/{WORKFLOW}?api_key={API_KEY}"
|
| 22 |
|
| 23 |
-
#
|
| 24 |
response = requests.post(
|
| 25 |
endpoint,
|
| 26 |
files={"file": image_bytes},
|
| 27 |
data={"confidence": "0.4", "overlap": "0.3"}
|
| 28 |
)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
if response.status_code != 200:
|
| 32 |
-
return None, f"β
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
if not
|
| 38 |
-
return None, f"β
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
if
|
| 43 |
-
return None, f"β Failed to
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
# Gradio
|
| 52 |
interface = gr.Interface(
|
| 53 |
fn=detect_image_with_log,
|
| 54 |
inputs=gr.Image(type="pil", label="Upload a Solar Panel Image"),
|
|
@@ -57,9 +59,8 @@ interface = gr.Interface(
|
|
| 57 |
gr.Textbox(label="Detection Log")
|
| 58 |
],
|
| 59 |
title="Solar Panel Fault Detection",
|
| 60 |
-
description="Upload an image to detect cracks, dents, and
|
| 61 |
-
allow_flagging="never"
|
| 62 |
-
theme="default"
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
# Roboflow config
|
| 7 |
API_URL = "https://detect.roboflow.com"
|
| 8 |
API_KEY = "dxkgGGHSZ3DI8XzVn29U"
|
| 9 |
WORKSPACE = "naveen-kumar-hnmil"
|
| 10 |
WORKFLOW = "detect-count-and-visualize-5"
|
| 11 |
|
|
|
|
| 12 |
def detect_image_with_log(image):
|
| 13 |
try:
|
| 14 |
+
# Convert to JPEG bytes
|
| 15 |
buffered = BytesIO()
|
| 16 |
image.save(buffered, format="JPEG")
|
| 17 |
image_bytes = buffered.getvalue()
|
| 18 |
|
| 19 |
+
# Build API endpoint
|
| 20 |
endpoint = f"{API_URL}/{WORKSPACE}/{WORKFLOW}?api_key={API_KEY}"
|
| 21 |
|
| 22 |
+
# POST to Roboflow
|
| 23 |
response = requests.post(
|
| 24 |
endpoint,
|
| 25 |
files={"file": image_bytes},
|
| 26 |
data={"confidence": "0.4", "overlap": "0.3"}
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# If API call failed
|
| 30 |
if response.status_code != 200:
|
| 31 |
+
return None, f"β API error: {response.status_code}\n{response.text}"
|
| 32 |
|
| 33 |
+
# Parse response
|
| 34 |
+
result_json = response.json()
|
| 35 |
+
annotated_url = result_json.get("image", {}).get("url")
|
| 36 |
|
| 37 |
+
if not annotated_url:
|
| 38 |
+
return None, f"β No 'image.url' found in response.\nResponse: {result_json}"
|
| 39 |
|
| 40 |
+
# Download annotated image
|
| 41 |
+
image_response = requests.get(annotated_url)
|
| 42 |
+
if image_response.status_code != 200:
|
| 43 |
+
return None, f"β Failed to load annotated image.\nURL: {annotated_url}"
|
| 44 |
|
| 45 |
+
# Show result
|
| 46 |
+
result_image = Image.open(BytesIO(image_response.content))
|
| 47 |
+
return result_image, "β
Detection successful."
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
+
# Catch and return all exceptions
|
| 51 |
+
return None, f"β Exception: {str(e)}"
|
| 52 |
|
| 53 |
+
# Gradio app
|
| 54 |
interface = gr.Interface(
|
| 55 |
fn=detect_image_with_log,
|
| 56 |
inputs=gr.Image(type="pil", label="Upload a Solar Panel Image"),
|
|
|
|
| 59 |
gr.Textbox(label="Detection Log")
|
| 60 |
],
|
| 61 |
title="Solar Panel Fault Detection",
|
| 62 |
+
description="Upload an image to detect cracks, dents, and faults using a Roboflow-trained YOLOv8 model.",
|
| 63 |
+
allow_flagging="never"
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|