atlury commited on
Commit
e134b51
·
verified ·
1 Parent(s): ec23149

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -1,28 +1,34 @@
1
  import gradio as gr
2
  from ultralytics import YOLO
3
- import spaces # Import the `spaces` library
4
 
5
  # Load pre-trained YOLOv8 model
6
  model = YOLO("yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt")
7
 
 
 
 
8
  # Decorate the `process_image` function with `@spaces.GPU`
9
- @spaces.GPU(duration=60) # Optional: Set the duration if needed
10
  def process_image(image):
11
  try:
12
  # Process the image
13
  results = model(source=image, save=False, show_labels=True, show_conf=True, show_boxes=True)
14
- result = results[0]
15
 
16
- # Extract the annotated image and the labels/confidence scores
17
  annotated_image = result.plot()
 
 
18
  detected_areas_labels = "\n".join(
19
- [f"{box.label.upper()}: {box.conf:.2f}" for box in result.boxes]
20
  )
21
 
22
  return annotated_image, detected_areas_labels
23
  except Exception as e:
24
  return None, f"Error processing image: {e}"
25
 
 
26
  # Create the Gradio Interface
27
  with gr.Blocks() as demo:
28
  gr.Markdown("# Document Segmentation Demo (ZeroGPU)")
 
1
  import gradio as gr
2
  from ultralytics import YOLO
3
+ import spaces
4
 
5
  # Load pre-trained YOLOv8 model
6
  model = YOLO("yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt")
7
 
8
+ # Get class names from model
9
+ class_names = model.names
10
+
11
  # Decorate the `process_image` function with `@spaces.GPU`
12
+ @spaces.GPU(duration=60)
13
  def process_image(image):
14
  try:
15
  # Process the image
16
  results = model(source=image, save=False, show_labels=True, show_conf=True, show_boxes=True)
17
+ result = results[0] # Get the first result
18
 
19
+ # Extract annotated image and labels with class names
20
  annotated_image = result.plot()
21
+
22
+ # Use cls attribute for labels and get class name from model
23
  detected_areas_labels = "\n".join(
24
+ [f"{class_names[int(box.cls)].upper()}: {box.conf:.2f}" for box in result.boxes]
25
  )
26
 
27
  return annotated_image, detected_areas_labels
28
  except Exception as e:
29
  return None, f"Error processing image: {e}"
30
 
31
+
32
  # Create the Gradio Interface
33
  with gr.Blocks() as demo:
34
  gr.Markdown("# Document Segmentation Demo (ZeroGPU)")