NaveenKumar5 commited on
Commit
4bbd92f
Β·
verified Β·
1 Parent(s): 4daf344

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -3,52 +3,54 @@ import requests
3
  from PIL import Image
4
  from io import BytesIO
5
 
6
- # Roboflow model configuration
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 uploaded image to bytes
16
  buffered = BytesIO()
17
  image.save(buffered, format="JPEG")
18
  image_bytes = buffered.getvalue()
19
 
20
- # Roboflow 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.4", "overlap": "0.3"}
28
  )
29
 
30
- # Check response
31
  if response.status_code != 200:
32
- return None, f"❌ Error: {response.status_code} - {response.text}"
33
 
34
- json_data = response.json()
35
- image_url = json_data.get("image", {}).get("url")
 
36
 
37
- if not image_url:
38
- return None, f"❌ Error: No image URL in response.\nResponse: {json_data}"
39
 
40
- # Fetch annotated image
41
- annotated_response = requests.get(image_url)
42
- if annotated_response.status_code != 200:
43
- return None, f"❌ Failed to download result image.\nURL: {image_url}"
44
 
45
- annotated_img = Image.open(BytesIO(annotated_response.content))
46
- return annotated_img, "βœ… Detection successful."
 
47
 
48
  except Exception as e:
49
- return None, f"❌ Exception occurred: {str(e)}"
 
50
 
51
- # Gradio UI
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 faulty parts using a Roboflow-trained YOLOv8 model.",
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__":