jayn95 commited on
Commit
dea4987
·
verified ·
1 Parent(s): fc76dbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -2,18 +2,13 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  from PIL import Image, ImageOps, ImageEnhance
4
  import numpy as np
5
- import tempfile
6
 
7
  # Load models
8
  model_swelling = YOLO("models/swelling/swelling_final.pt")
9
  model_redness = YOLO("models/redness/redness_final.pt")
10
  model_bleeding = YOLO("models/bleeding/bleeding_final.pt")
11
 
12
- def save_temp_file(img):
13
- """Save PIL image to a temporary file and return the file path."""
14
- temp = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
15
- img.save(temp.name, format="JPEG")
16
- return temp.name
17
 
18
  def preprocess(image):
19
  if isinstance(image, np.ndarray):
@@ -33,6 +28,15 @@ def preprocess(image):
33
 
34
  return image
35
 
 
 
 
 
 
 
 
 
 
36
  def detect_gingivitis(image, conf=0.4, iou=0.5):
37
  image = preprocess(image)
38
 
@@ -41,10 +45,15 @@ def detect_gingivitis(image, conf=0.4, iou=0.5):
41
  bl_res = model_bleeding.predict(image, conf=conf, iou=iou)
42
 
43
  # Convert YOLO output → numpy RGB
44
- img_sw = sw_res[0].plot()[:, :, ::-1] # BGR → RGB
45
  img_rd = rd_res[0].plot()[:, :, ::-1]
46
  img_bl = bl_res[0].plot()[:, :, ::-1]
47
 
 
 
 
 
 
48
  has_sw = len(sw_res[0].boxes) > 0
49
  has_rd = len(rd_res[0].boxes) > 0
50
  has_bl = len(bl_res[0].boxes) > 0
@@ -58,7 +67,8 @@ def detect_gingivitis(image, conf=0.4, iou=0.5):
58
  else:
59
  diagnosis = "🟢 You don't have gingivitis."
60
 
61
- return [img_sw, img_rd, img_bl, diagnosis]
 
62
 
63
 
64
  # Gradio Interface
@@ -70,12 +80,12 @@ interface = gr.Interface(
70
  gr.Slider(0, 1, value=0.5, step=0.05, label="NMS IoU Threshold"),
71
  ],
72
  outputs=[
73
- gr.Image(label="Swelling Detection"),
74
- gr.Image(label="Redness Detection"),
75
- gr.Image(label="Bleeding Detection"),
76
  gr.Textbox(label="Diagnosis")
77
  ],
78
  title="Gingivitis Detection"
79
  )
80
 
81
- interface.launch()
 
2
  from ultralytics import YOLO
3
  from PIL import Image, ImageOps, ImageEnhance
4
  import numpy as np
5
+ import tempfile, io, base64
6
 
7
  # Load models
8
  model_swelling = YOLO("models/swelling/swelling_final.pt")
9
  model_redness = YOLO("models/redness/redness_final.pt")
10
  model_bleeding = YOLO("models/bleeding/bleeding_final.pt")
11
 
 
 
 
 
 
12
 
13
  def preprocess(image):
14
  if isinstance(image, np.ndarray):
 
28
 
29
  return image
30
 
31
+
32
+ def np_to_base64(img_np, format="JPEG"):
33
+ """Convert a numpy RGB image to Base64 string."""
34
+ pil_img = Image.fromarray(img_np)
35
+ buffer = io.BytesIO()
36
+ pil_img.save(buffer, format=format)
37
+ return base64.b64encode(buffer.getvalue()).decode("utf-8")
38
+
39
+
40
  def detect_gingivitis(image, conf=0.4, iou=0.5):
41
  image = preprocess(image)
42
 
 
45
  bl_res = model_bleeding.predict(image, conf=conf, iou=iou)
46
 
47
  # Convert YOLO output → numpy RGB
48
+ img_sw = sw_res[0].plot()[:, :, ::-1] # BGR → RGB
49
  img_rd = rd_res[0].plot()[:, :, ::-1]
50
  img_bl = bl_res[0].plot()[:, :, ::-1]
51
 
52
+ # Encode images to Base64
53
+ sw_b64 = np_to_base64(img_sw)
54
+ rd_b64 = np_to_base64(img_rd)
55
+ bl_b64 = np_to_base64(img_bl)
56
+
57
  has_sw = len(sw_res[0].boxes) > 0
58
  has_rd = len(rd_res[0].boxes) > 0
59
  has_bl = len(bl_res[0].boxes) > 0
 
67
  else:
68
  diagnosis = "🟢 You don't have gingivitis."
69
 
70
+ # Return Base64 images + diagnosis text
71
+ return [sw_b64, rd_b64, bl_b64, diagnosis]
72
 
73
 
74
  # Gradio Interface
 
80
  gr.Slider(0, 1, value=0.5, step=0.05, label="NMS IoU Threshold"),
81
  ],
82
  outputs=[
83
+ gr.Image(label="Swelling Detection", type="auto"),
84
+ gr.Image(label="Redness Detection", type="auto"),
85
+ gr.Image(label="Bleeding Detection", type="auto"),
86
  gr.Textbox(label="Diagnosis")
87
  ],
88
  title="Gingivitis Detection"
89
  )
90
 
91
+ interface.launch()