Noursine commited on
Commit
1d10091
·
verified ·
1 Parent(s): 337b41b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -3,20 +3,38 @@ import gradio as gr
3
  from ultralytics import YOLO
4
  from PIL import Image
5
 
6
- # Load YOLOv8 model
7
- model = YOLO('best.pt')
8
 
9
- # Folder with example test images
10
- test_images_folder = 'test_images'
11
  test_images = sorted(os.listdir(test_images_folder))
12
 
13
- # Predict using model
 
 
 
14
  def predict_image(image_path):
15
  results = model(image_path)
16
  img_array = results[0].plot(conf=False, labels=True, boxes=True)
17
- return Image.fromarray(img_array)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Run prediction logic
20
  def run_prediction(uploaded_image, selected_image):
21
  if uploaded_image is not None:
22
  return predict_image(uploaded_image)
@@ -24,24 +42,25 @@ def run_prediction(uploaded_image, selected_image):
24
  image_path = os.path.join(test_images_folder, selected_image)
25
  return predict_image(image_path)
26
  else:
27
- return None
28
 
29
- # Gradio Interface
30
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
31
  gr.Markdown("## 🦷 Dental Segmentation with YOLOv8")
32
- gr.Markdown("Upload your own image or select a test image to run detection.")
33
 
34
  with gr.Column():
35
  uploaded_image = gr.Image(label="Upload your image (optional)", type="filepath")
36
- selected_image = gr.Dropdown(choices=test_images, label="Or choose a test image")
37
 
38
- gr.Markdown("### Prediction Output")
39
- output_image = gr.Image(label="Predicted Output")
 
40
 
41
  gr.Button("Run prediction").click(
42
  fn=run_prediction,
43
  inputs=[uploaded_image, selected_image],
44
- outputs=output_image
45
  )
46
 
47
  demo.launch()
 
3
  from ultralytics import YOLO
4
  from PIL import Image
5
 
6
+ # Load YOLOv8 model
7
+ model = YOLO('/content/drive/MyDrive/yolov8_models/best.pt')
8
 
9
+ # Folder with test images
10
+ test_images_folder = '/content/Instance_seg_teeth/Dataset/yolo_test_dataset/test/images'
11
  test_images = sorted(os.listdir(test_images_folder))
12
 
13
+ # List of all tooth classes (from your dataset)
14
+ tooth_classes = ['11', '12', '13', '14', '15', '16', '17', '18', '21', '22', '23', '24', '25', '26', '27', '28',
15
+ '31', '32', '33', '34', '35', '36', '37', '38', '41', '42', '43', '44', '45', '46', '47', '48']
16
+
17
  def predict_image(image_path):
18
  results = model(image_path)
19
  img_array = results[0].plot(conf=False, labels=True, boxes=True)
20
+
21
+ # Extract predicted classes (as numbers)
22
+ pred_classes = results[0].boxes.cls.cpu().numpy().astype(int) # class indices
23
+
24
+ # Map indices to class names
25
+ detected_classes = sorted(set([tooth_classes[i] for i in pred_classes]))
26
+
27
+ # Classes not detected
28
+ missing_classes = sorted(set(tooth_classes) - set(detected_classes))
29
+
30
+ detected_str = ", ".join(detected_classes) if detected_classes else "None"
31
+ missing_str = ", ".join(missing_classes) if missing_classes else "None"
32
+
33
+ info_text = f"Detected tooth classes:\n{detected_str}\n\nMissing tooth classes:\n{missing_str}"
34
+
35
+ return Image.fromarray(img_array), info_text
36
 
37
+ # Logic: use uploaded image if available, otherwise selected image
38
  def run_prediction(uploaded_image, selected_image):
39
  if uploaded_image is not None:
40
  return predict_image(uploaded_image)
 
42
  image_path = os.path.join(test_images_folder, selected_image)
43
  return predict_image(image_path)
44
  else:
45
+ return None, ""
46
 
47
+ # Gradio interface
48
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
49
  gr.Markdown("## 🦷 Dental Segmentation with YOLOv8")
50
+ gr.Markdown("Upload your own image or choose a test image from the list below.")
51
 
52
  with gr.Column():
53
  uploaded_image = gr.Image(label="Upload your image (optional)", type="filepath")
54
+ selected_image = gr.Dropdown(choices=test_images, label="...or select a test image")
55
 
56
+ gr.Markdown("### Prediction Result")
57
+ output_image = gr.Image(label="Predicted Image")
58
+ output_text = gr.Textbox(label="Detected & Missing Tooth Classes", lines=5)
59
 
60
  gr.Button("Run prediction").click(
61
  fn=run_prediction,
62
  inputs=[uploaded_image, selected_image],
63
+ outputs=[output_image, output_text]
64
  )
65
 
66
  demo.launch()