Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
# Roboflow API config
|
| 8 |
+
API_URL = "https://detect.roboflow.com"
|
| 9 |
+
API_KEY = "dxkgGGHSZ3DI8XzVn29U"
|
| 10 |
+
WORKSPACE = "naveen-kumar-hnmil"
|
| 11 |
+
WORKFLOW = "detect-count-and-visualize-5"
|
| 12 |
+
|
| 13 |
+
def detect_image(image):
|
| 14 |
+
# Convert PIL to bytes
|
| 15 |
+
buffered = BytesIO()
|
| 16 |
+
image.save(buffered, format="JPEG")
|
| 17 |
+
image_bytes = buffered.getvalue()
|
| 18 |
+
|
| 19 |
+
# Build full API endpoint
|
| 20 |
+
endpoint = f"{API_URL}/{WORKSPACE}/{WORKFLOW}?api_key={API_KEY}"
|
| 21 |
+
|
| 22 |
+
# Make the POST request to Roboflow
|
| 23 |
+
response = requests.post(endpoint, files={"file": image_bytes}, data={"confidence": "0.5", "overlap": "0.4"})
|
| 24 |
+
|
| 25 |
+
if response.status_code != 200:
|
| 26 |
+
return f"Error from Roboflow: {response.text}"
|
| 27 |
+
|
| 28 |
+
# Roboflow returns image as bytes
|
| 29 |
+
result = requests.get(response.json()["image"]["url"])
|
| 30 |
+
result_img = Image.open(BytesIO(result.content))
|
| 31 |
+
|
| 32 |
+
return result_img
|
| 33 |
+
|
| 34 |
+
# Gradio UI
|
| 35 |
+
gr.Interface(
|
| 36 |
+
fn=detect_image,
|
| 37 |
+
inputs=gr.Image(type="pil", label="Upload a Solar Panel Image"),
|
| 38 |
+
outputs=gr.Image(type="pil", label="Annotated Output"),
|
| 39 |
+
title="Solar Panel Fault Detection",
|
| 40 |
+
description="Upload an image to detect cracks, dents, and faulty parts using a Roboflow-trained YOLOv8 model.",
|
| 41 |
+
allow_flagging="never"
|
| 42 |
+
).launch()
|