File size: 2,173 Bytes
60c4cae
 
 
 
 
4bbd92f
60c4cae
 
 
21e7505
60c4cae
4daf344
d4548af
41466ba
d4548af
 
 
60c4cae
d4548af
41466ba
60c4cae
d4548af
 
 
4daf344
d4548af
60c4cae
41466ba
d4548af
4bbd92f
d4548af
4bbd92f
41466ba
d4548af
41466ba
4bbd92f
41466ba
d4548af
41466ba
4bbd92f
 
 
d4548af
4bbd92f
 
60c4cae
4daf344
4bbd92f
60c4cae
4bbd92f
4daf344
 
60c4cae
4daf344
 
 
 
60c4cae
4bbd92f
 
4daf344
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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()