Tejeshwar commited on
Commit
6aea0c0
·
verified ·
1 Parent(s): 81e9e78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -3,36 +3,34 @@ from PIL import Image
3
  import os
4
  import sys
5
 
6
- # Make yolov12 folder importable
7
  sys.path.append(os.path.abspath("yolov12"))
8
 
9
- # Import your YOLO class
10
- from yolo import YOLO # Now from yolov12/yolo.py
11
 
12
- # Load your custom YOLOv12 model
13
- model = YOLO("yolov12/best.pt")
 
14
 
15
  def detect_damage(image: Image.Image):
16
- results = model(image)
17
-
18
- is_damaged = False
19
- for result in results:
20
- for box in result.boxes:
21
- class_id = int(box.cls[0])
22
- label = model.names[class_id]
23
- print("Detected:", label)
24
- if "apple_damaged" in label.lower():
25
- is_damaged = True
26
- break
27
 
28
  return {"is_damaged": is_damaged}
29
 
 
30
  demo = gr.Interface(
31
  fn=detect_damage,
32
  inputs=gr.Image(type="pil"),
33
  outputs=gr.JSON(label="Detection Result"),
34
  title="🍎 Apple Damage Detector (YOLOv12)",
35
- description="Upload an image of an apple to detect if it's damaged."
36
  )
37
 
38
  demo.launch()
 
3
  import os
4
  import sys
5
 
6
+ # Add yolov12 folder to import path
7
  sys.path.append(os.path.abspath("yolov12"))
8
 
9
+ # Import your inference function
10
+ from yolo import run_yolov12_inference
11
 
12
+ # Create temp upload dir
13
+ UPLOAD_DIR = "static/uploaded"
14
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
15
 
16
  def detect_damage(image: Image.Image):
17
+ # Save image to temp path
18
+ img_name = "test_image.jpg"
19
+ img_path = os.path.join(UPLOAD_DIR, img_name)
20
+ image.save(img_path)
21
+
22
+ # Run inference using your script
23
+ is_damaged = run_yolov12_inference(img_path)
 
 
 
 
24
 
25
  return {"is_damaged": is_damaged}
26
 
27
+ # Gradio interface
28
  demo = gr.Interface(
29
  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 apple image to check if it's damaged using a custom YOLOv12 model."
34
  )
35
 
36
  demo.launch()