SkalskiP commited on
Commit
e317241
·
1 Parent(s): 377e9f4

allow users to pick inference resolution

Browse files
Files changed (4) hide show
  1. .gitignore +3 -0
  2. app.py +21 -11
  3. utils/__init__.py +0 -0
  4. utils/video.py +26 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .idea/
2
+ venv/
3
+ *.pth
app.py CHANGED
@@ -4,6 +4,8 @@ import supervision as sv
4
  from rfdetr import RFDETRBase, RFDETRLarge
5
  from rfdetr.util.coco_classes import COCO_CLASSES
6
 
 
 
7
  MARKDOWN = """
8
  # RF-DETR 🔥
9
 
@@ -24,9 +26,9 @@ by [Roboflow](https://roboflow.com/) and released under the Apache 2.0 license.
24
  """
25
 
26
  IMAGE_EXAMPLES = [
27
- ['https://media.roboflow.com/supervision/image-examples/people-walking.png', 0.3, "large"],
28
- ['https://media.roboflow.com/supervision/image-examples/vehicles.png', 0.3, "large"],
29
- ['https://media.roboflow.com/notebooks/examples/dog-2.jpeg', 0.5, "base"],
30
  ]
31
 
32
  COLOR = sv.ColorPalette.from_hex([
@@ -34,15 +36,16 @@ COLOR = sv.ColorPalette.from_hex([
34
  "#9999ff", "#3399ff", "#66ffff", "#33ff99", "#66ff66", "#99ff00"
35
  ])
36
 
37
- MODEL_BASE = RFDETRBase(resolution=728)
38
- MODEL_LARGE = RFDETRLarge(resolution=728)
 
39
 
40
 
41
  @spaces.GPU()
42
- def inference(image, confidence: float, checkpoint: str):
43
- detections = MODEL_BASE.predict(image, threshold=confidence) \
44
- if checkpoint == "base" \
45
- else MODEL_LARGE.predict(image, threshold=confidence)
46
 
47
  text_scale = sv.calculate_optimal_text_scale(resolution_wh=image.size)
48
  thickness = sv.calculate_optimal_line_thickness(resolution_wh=image.size)
@@ -83,6 +86,13 @@ with gr.Blocks() as demo:
83
  step=0.05,
84
  value=0.5,
85
  )
 
 
 
 
 
 
 
86
  with gr.Row():
87
  checkpoint_dropdown = gr.Dropdown(
88
  label="Checkpoint",
@@ -100,13 +110,13 @@ with gr.Blocks() as demo:
100
  gr.Examples(
101
  fn=inference,
102
  examples=IMAGE_EXAMPLES,
103
- inputs=[input_image, confidence_slider, checkpoint_dropdown],
104
  outputs=output_image
105
  )
106
 
107
  submit_button.click(
108
  inference,
109
- inputs=[input_image, confidence_slider, checkpoint_dropdown],
110
  outputs=output_image
111
  )
112
 
 
4
  from rfdetr import RFDETRBase, RFDETRLarge
5
  from rfdetr.util.coco_classes import COCO_CLASSES
6
 
7
+ from utils.video import create_directory
8
+
9
  MARKDOWN = """
10
  # RF-DETR 🔥
11
 
 
26
  """
27
 
28
  IMAGE_EXAMPLES = [
29
+ ['https://media.roboflow.com/supervision/image-examples/people-walking.png', 0.3, 728, "large"],
30
+ ['https://media.roboflow.com/supervision/image-examples/vehicles.png', 0.3, 728, "large"],
31
+ ['https://media.roboflow.com/notebooks/examples/dog-2.jpeg', 0.5, 560, "base"],
32
  ]
33
 
34
  COLOR = sv.ColorPalette.from_hex([
 
36
  "#9999ff", "#3399ff", "#66ffff", "#33ff99", "#66ff66", "#99ff00"
37
  ])
38
 
39
+ VIDEO_SCALE_FACTOR = 0.5
40
+ VIDEO_TARGET_DIRECTORY = "tmp"
41
+ create_directory(directory_path=VIDEO_TARGET_DIRECTORY)
42
 
43
 
44
  @spaces.GPU()
45
+ def inference(image, confidence: float, resolution: int, checkpoint: str):
46
+ model_class = RFDETRBase if checkpoint == "base" else RFDETRLarge
47
+ model = model_class(resolution=resolution)
48
+ detections = model.predict(image, threshold=confidence)
49
 
50
  text_scale = sv.calculate_optimal_text_scale(resolution_wh=image.size)
51
  thickness = sv.calculate_optimal_line_thickness(resolution_wh=image.size)
 
86
  step=0.05,
87
  value=0.5,
88
  )
89
+ resolution_slider = gr.Slider(
90
+ label="Inference resolution",
91
+ minimum=560,
92
+ maximum=1120,
93
+ step=56,
94
+ value=728,
95
+ )
96
  with gr.Row():
97
  checkpoint_dropdown = gr.Dropdown(
98
  label="Checkpoint",
 
110
  gr.Examples(
111
  fn=inference,
112
  examples=IMAGE_EXAMPLES,
113
+ inputs=[input_image, confidence_slider, resolution_slider, checkpoint_dropdown],
114
  outputs=output_image
115
  )
116
 
117
  submit_button.click(
118
  inference,
119
+ inputs=[input_image, confidence_slider, resolution_slider, checkpoint_dropdown],
120
  outputs=output_image
121
  )
122
 
utils/__init__.py ADDED
File without changes
utils/video.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ import os
3
+ import shutil
4
+ import uuid
5
+
6
+
7
+ def create_directory(directory_path: str) -> None:
8
+ if not os.path.exists(directory_path):
9
+ os.makedirs(directory_path)
10
+
11
+
12
+ def delete_directory(directory_path: str) -> None:
13
+ if not os.path.exists(directory_path):
14
+ raise FileNotFoundError(f"Directory '{directory_path}' does not exist.")
15
+
16
+ try:
17
+ shutil.rmtree(directory_path)
18
+ except PermissionError:
19
+ raise PermissionError(
20
+ f"Permission denied: Unable to delete '{directory_path}'.")
21
+
22
+
23
+ def generate_unique_name():
24
+ current_datetime = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
25
+ unique_id = uuid.uuid4()
26
+ return f"{current_datetime}_{unique_id}"