Noursine commited on
Commit
dcd4328
·
verified ·
1 Parent(s): ac4a58a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -3,20 +3,20 @@ 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.pt')
8
 
9
- # Folder with test images
10
  test_images_folder = '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)
@@ -26,17 +26,17 @@ def run_prediction(uploaded_image, selected_image):
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,
 
3
  from ultralytics import YOLO
4
  from PIL import Image
5
 
6
+ # Load YOLOv8 model (make sure best.pt is in the same folder as this script)
7
+ model = YOLO('best.pt')
8
 
9
+ # Folder with example test images (must be inside your repo for HF Spaces)
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)
 
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,