Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load YOLOv8 model
|
| 7 |
+
model = YOLO('/content/drive/MyDrive/yolov8_models/best (4).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 |
+
# Prediction function
|
| 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 |
+
# Logic: use uploaded image if available, otherwise selected image
|
| 20 |
+
def run_prediction(uploaded_image, selected_image):
|
| 21 |
+
if uploaded_image is not None:
|
| 22 |
+
return predict_image(uploaded_image)
|
| 23 |
+
elif selected_image is not None:
|
| 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 choose a test image from the list below.")
|
| 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 select a test image")
|
| 37 |
+
|
| 38 |
+
gr.Markdown("### Prediction Result")
|
| 39 |
+
output_image = gr.Image(label="Predicted Image")
|
| 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()
|