Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,48 +3,48 @@ import gradio as gr
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
from PIL import Image
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load YOLOv8 model
|
| 7 |
-
model = YOLO(
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
test_images = sorted(os.listdir(test_images_folder))
|
| 12 |
|
| 13 |
-
# List of all tooth classes (
|
| 14 |
-
tooth_classes = ['11', '12', '13', '14', '15', '16', '17', '18',
|
| 15 |
-
'
|
|
|
|
|
|
|
| 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 (
|
| 22 |
-
pred_classes = results[0].boxes.cls.cpu().numpy().astype(int)
|
| 23 |
-
|
| 24 |
-
|
| 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)
|
| 41 |
elif selected_image is not None:
|
| 42 |
-
image_path = os.path.join(
|
| 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.")
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Adjust these paths based on where you uploaded the files in your HF repo
|
| 7 |
+
MODEL_PATH = 'best.pt' # Put your best.pt at repo root or adjust if in a folder
|
| 8 |
+
TEST_IMAGES_FOLDER = 'test_images' # Folder inside your repo with test images
|
| 9 |
+
|
| 10 |
# Load YOLOv8 model
|
| 11 |
+
model = YOLO(MODEL_PATH)
|
| 12 |
|
| 13 |
+
# List all test images available (make sure folder exists and has images)
|
| 14 |
+
test_images = sorted(os.listdir(TEST_IMAGES_FOLDER)) if os.path.exists(TEST_IMAGES_FOLDER) else []
|
|
|
|
| 15 |
|
| 16 |
+
# List of all tooth classes (your dataset classes)
|
| 17 |
+
tooth_classes = ['11', '12', '13', '14', '15', '16', '17', '18',
|
| 18 |
+
'21', '22', '23', '24', '25', '26', '27', '28',
|
| 19 |
+
'31', '32', '33', '34', '35', '36', '37', '38',
|
| 20 |
+
'41', '42', '43', '44', '45', '46', '47', '48']
|
| 21 |
|
| 22 |
def predict_image(image_path):
|
| 23 |
results = model(image_path)
|
| 24 |
img_array = results[0].plot(conf=False, labels=True, boxes=True)
|
| 25 |
+
|
| 26 |
+
# Extract predicted classes (indices)
|
| 27 |
+
pred_classes = results[0].boxes.cls.cpu().numpy().astype(int) if len(results[0].boxes) > 0 else []
|
| 28 |
+
|
| 29 |
+
detected_classes = sorted(set([tooth_classes[i] for i in pred_classes])) if len(pred_classes) > 0 else []
|
|
|
|
|
|
|
|
|
|
| 30 |
missing_classes = sorted(set(tooth_classes) - set(detected_classes))
|
| 31 |
+
|
| 32 |
detected_str = ", ".join(detected_classes) if detected_classes else "None"
|
| 33 |
missing_str = ", ".join(missing_classes) if missing_classes else "None"
|
| 34 |
+
|
| 35 |
info_text = f"Detected tooth classes:\n{detected_str}\n\nMissing tooth classes:\n{missing_str}"
|
| 36 |
+
|
| 37 |
return Image.fromarray(img_array), info_text
|
| 38 |
|
|
|
|
| 39 |
def run_prediction(uploaded_image, selected_image):
|
| 40 |
if uploaded_image is not None:
|
| 41 |
return predict_image(uploaded_image)
|
| 42 |
elif selected_image is not None:
|
| 43 |
+
image_path = os.path.join(TEST_IMAGES_FOLDER, selected_image)
|
| 44 |
return predict_image(image_path)
|
| 45 |
else:
|
| 46 |
return None, ""
|
| 47 |
|
|
|
|
| 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.")
|