HF / app.py
NaveenKumar5's picture
Update app.py
21e7505 verified
import gradio as gr
import requests
from PIL import Image
from io import BytesIO
# Roboflow config
API_URL = "https://detect.roboflow.com"
API_KEY = "dxkgGGHSZ3DI8XzVn29U"
WORKSPACE = "naveen-kumar-hnmil"
WORKFLOW = "detect-count-and-visualize-10"
def detect_image_with_log(image):
try:
print("πŸ”„ Converting image to JPEG bytes")
buffered = BytesIO()
image.save(buffered, format="JPEG")
image_bytes = buffered.getvalue()
endpoint = f"{API_URL}/{WORKSPACE}/{WORKFLOW}?api_key={API_KEY}"
print(f"πŸ“‘ Sending POST request to: {endpoint}")
response = requests.post(
endpoint,
files={"file": image_bytes},
data={"confidence": "0.4", "overlap": "0.3"}
)
print(f"πŸ“₯ Roboflow response: {response.status_code}")
if response.status_code != 200:
return None, f"❌ API error: {response.status_code}\n{response.text}"
result_json = response.json()
print(f"πŸ“Š Parsed response JSON: {result_json}")
annotated_url = result_json.get("image", {}).get("url")
if not annotated_url:
return None, f"❌ No 'image.url' found in response.\nFull Response: {result_json}"
print(f"🌐 Fetching annotated image from: {annotated_url}")
image_response = requests.get(annotated_url)
if image_response.status_code != 200:
return None, f"❌ Failed to load annotated image.\nURL: {annotated_url}"
result_image = Image.open(BytesIO(image_response.content))
return result_image, "βœ… Detection successful."
except Exception as e:
return None, f"❌ Exception: {str(e)}"
# Gradio app
interface = gr.Interface(
fn=detect_image_with_log,
inputs=gr.Image(type="pil", label="Upload a Solar Panel Image"),
outputs=[
gr.Image(label="Annotated Output"),
gr.Textbox(label="Detection Log")
],
title="Solar Panel Fault Detection",
description="Upload an image to detect cracks, dents, and faults using a Roboflow-trained YOLOv8 model.",
allow_flagging="never"
)
if __name__ == "__main__":
interface.launch()