yoloKYS commited on
Commit
00d949a
·
verified ·
1 Parent(s): 430767a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -1,29 +1,27 @@
1
  import gradio as gr
2
  import torch
 
3
  from PIL import Image
4
  import numpy as np
 
5
 
6
- # Import YOLO
7
- from ultralytics import YOLO
8
 
9
- # ---- Monkey-patch for YOLOv12 fork ----
10
- # Make sure the yolov12 fork is in the repo folder
11
- from yolov12.models.modules.attn import Attn # forked class
12
  import ultralytics.nn.modules.block as block
13
- block.AAttn = Attn # patch original AAttn
14
 
15
- # ---- Load your trained model ----
16
- model = YOLO("best.pt") # path to your YOLOv12 weights
17
 
18
  # ---- Inference function ----
19
  def detect_objects(image):
20
- # Convert PIL image to array
21
  img = np.array(image)
22
- # Run YOLOv12 inference
23
  results = model(img)
24
- # Get annotated image
25
- annotated_img = results[0].plot()
26
- return Image.fromarray(annotated_img)
27
 
28
  # ---- Gradio interface ----
29
  iface = gr.Interface(
@@ -31,8 +29,7 @@ iface = gr.Interface(
31
  inputs=gr.Image(type="pil"),
32
  outputs=gr.Image(type="pil"),
33
  title="Ear Condition Detection (YOLOv12 Fork)",
34
- description="Upload an image of an ear. The model detects conditions using the YOLOv12 fork."
35
  )
36
 
37
- # Launch Gradio app
38
  iface.launch()
 
1
  import gradio as gr
2
  import torch
3
+ from ultralytics import YOLO
4
  from PIL import Image
5
  import numpy as np
6
+ import sys, os
7
 
8
+ # ---- Add yolov12 fork folder to path ----
9
+ sys.path.append(os.path.join(os.path.dirname(__file__), "yolov12-main"))
10
 
11
+ # ---- Import forked Attn and patch YOLO ----
12
+ from yolov12.models.modules.attn import Attn # inside yolov12-main
 
13
  import ultralytics.nn.modules.block as block
14
+ block.AAttn = Attn # replace original AAttn
15
 
16
+ # ---- Load model ----
17
+ model = YOLO("best.pt") # make sure best.pt is in same folder
18
 
19
  # ---- Inference function ----
20
  def detect_objects(image):
 
21
  img = np.array(image)
 
22
  results = model(img)
23
+ annotated = results[0].plot()
24
+ return Image.fromarray(annotated)
 
25
 
26
  # ---- Gradio interface ----
27
  iface = gr.Interface(
 
29
  inputs=gr.Image(type="pil"),
30
  outputs=gr.Image(type="pil"),
31
  title="Ear Condition Detection (YOLOv12 Fork)",
32
+ description="Uses forked YOLOv12 model to detect ear conditions."
33
  )
34
 
 
35
  iface.launch()