edeler commited on
Commit
2def4f2
·
verified ·
1 Parent(s): bb6edc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -1
app.py CHANGED
@@ -7,6 +7,7 @@ from ultralytics import YOLO
7
  import supervision as sv
8
  from PIL import Image
9
  from huggingface_hub import snapshot_download
 
10
 
11
  # Adjustable parameters for detection
12
  CONFIDENCE_THRESHOLD = 0.1 # Confidence threshold for detections
@@ -27,6 +28,7 @@ model_path = os.path.join(model_dir, "best.pt") # Adjust if filename differs
27
  model = YOLO(model_path).to(device)
28
 
29
  # Define the detection function for Gradio
 
30
  def detect_objects(image: np.ndarray) -> Image.Image:
31
  # Ensure the image is in BGR format if provided by PIL (Gradio gives us an RGB image)
32
  if image.shape[-1] == 3:
@@ -82,7 +84,7 @@ with gr.Blocks() as demo:
82
  with gr.Column():
83
  output_img = gr.Image(label="Detection Result", interactive=False)
84
 
85
- # Add Examples section with images from the root directory
86
  with gr.Accordion("Select an Example Image"):
87
  example_root = os.path.dirname(__file__) # Root directory
88
  example_images = [os.path.join(example_root, file) for file in os.listdir(example_root) if file.endswith(".jpg")]
 
7
  import supervision as sv
8
  from PIL import Image
9
  from huggingface_hub import snapshot_download
10
+ import spaces # Import spaces for ZeroGPU compatibility
11
 
12
  # Adjustable parameters for detection
13
  CONFIDENCE_THRESHOLD = 0.1 # Confidence threshold for detections
 
28
  model = YOLO(model_path).to(device)
29
 
30
  # Define the detection function for Gradio
31
+ @spaces.GPU # Decorator to allocate GPU for ZeroGPU-enabled Spaces
32
  def detect_objects(image: np.ndarray) -> Image.Image:
33
  # Ensure the image is in BGR format if provided by PIL (Gradio gives us an RGB image)
34
  if image.shape[-1] == 3:
 
84
  with gr.Column():
85
  output_img = gr.Image(label="Detection Result", interactive=False)
86
 
87
+ # Add Examples section with images from the root directory
88
  with gr.Accordion("Select an Example Image"):
89
  example_root = os.path.dirname(__file__) # Root directory
90
  example_images = [os.path.join(example_root, file) for file in os.listdir(example_root) if file.endswith(".jpg")]