Tejeshwar commited on
Commit
d1735aa
Β·
verified Β·
1 Parent(s): cb0e20d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -1,37 +1,35 @@
1
  import gradio as gr
2
- import numpy as np
3
  from PIL import Image
4
- from ultralytics import YOLO
5
- from pathlib import Path
6
 
7
- # βœ… Load YOLOv12 model (make sure this path matches your repo)
8
- model_path = Path("best.pt") # Or change to "weights/best.pt" if uploaded in subfolder
9
- assert model_path.exists(), f"Model not found at {model_path}"
10
- model = YOLO(str(model_path))
 
 
 
11
 
12
- # βœ… Damage detection using YOLOv12
13
  def detect_damage(image: Image.Image):
14
  results = model(image)
15
-
16
  is_damaged = False
 
17
  for result in results:
18
  for box in result.boxes:
19
  class_id = int(box.cls[0])
20
  label = model.names[class_id]
21
- print("Detected label:", label)
22
  if "apple_damaged" in label.lower():
23
  is_damaged = True
24
  break
25
 
26
  return {"is_damaged": is_damaged}
27
 
28
- # βœ… Gradio interface
29
- demo = gr.Interface(
30
- fn=detect_damage,
31
- inputs=gr.Image(type="pil"),
32
- outputs=gr.JSON(label="Detection Result"),
33
- title="🍎 Apple Damage Detector",
34
- description="Upload an apple image to detect whether it is damaged using a YOLOv12 model."
35
- )
36
 
37
  demo.launch()
 
1
  import gradio as gr
 
2
  from PIL import Image
3
+ import sys
4
+ import os
5
 
6
+ # πŸ‘‡ Add your custom YOLOv12 path to Python path
7
+ sys.path.append("yolov12")
8
+
9
+ # πŸ‘‡ Import your custom model loader
10
+ from models.yolo import YOLO # adjust based on your repo
11
+
12
+ model = YOLO("yolov12/best.pt")
13
 
 
14
  def detect_damage(image: Image.Image):
15
  results = model(image)
 
16
  is_damaged = False
17
+
18
  for result in results:
19
  for box in result.boxes:
20
  class_id = int(box.cls[0])
21
  label = model.names[class_id]
22
+ print("Detected:", label)
23
  if "apple_damaged" in label.lower():
24
  is_damaged = True
25
  break
26
 
27
  return {"is_damaged": is_damaged}
28
 
29
+ demo = gr.Interface(fn=detect_damage,
30
+ inputs=gr.Image(type="pil"),
31
+ outputs=gr.JSON(label="Detection Result"),
32
+ title="🍎 Apple Damage Detector (YOLOv12)",
33
+ description="Upload an image of an apple to check if it's damaged.")
 
 
 
34
 
35
  demo.launch()