Darius Morawiec commited on
Commit
7b4b54b
·
1 Parent(s): c8d2dc0

Add image scaling functionality and target size slider

Browse files
Files changed (1) hide show
  1. app.py +27 -2
app.py CHANGED
@@ -33,6 +33,21 @@ model_ids = [
33
  ]
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def image_to_base64(image):
37
  buffered = BytesIO()
38
  image.save(buffered, format="PNG")
@@ -43,7 +58,7 @@ def image_to_base64(image):
43
  with gr.Blocks() as demo:
44
  gr.Markdown("# Qwen-VL Object-Detection")
45
  gr.Markdown(
46
- "Compare [Qwen3-VL](https://huggingface.co/collections/Qwen/qwen3-vl), [Qwen2.5-VL](https://huggingface.co/collections/Qwen/qwen25-vl) and [Qwen2-VL](https://huggingface.co/collections/Qwen/qwen2-vl) models by [Qwen](https://huggingface.co/Qwen) for object detection in images."
47
  )
48
 
49
  if DEVICE != "cuda":
@@ -86,6 +101,14 @@ with gr.Blocks() as demo:
86
  step=32,
87
  interactive=True,
88
  )
 
 
 
 
 
 
 
 
89
 
90
  with gr.Column():
91
  gr.Markdown("## Outputs")
@@ -114,6 +137,7 @@ with gr.Blocks() as demo:
114
 
115
  def run(
116
  image,
 
117
  system_prompt: str,
118
  user_prompt: str,
119
  model_id: str,
@@ -164,7 +188,7 @@ with gr.Blocks() as demo:
164
  "content": [
165
  {
166
  "type": "image",
167
- "image": f"data:image;base64,{image_to_base64(image)}",
168
  },
169
  {"type": "text", "text": system_prompt},
170
  {"type": "text", "text": user_prompt},
@@ -230,6 +254,7 @@ with gr.Blocks() as demo:
230
  fn=run,
231
  inputs=[
232
  image_input,
 
233
  system_prompt,
234
  user_prompt,
235
  input_model_id,
 
33
  ]
34
 
35
 
36
+ def scale_image(image, target_size=1000):
37
+ width, height = image.size
38
+ if max(width, height) <= target_size:
39
+ return image
40
+
41
+ if width >= height:
42
+ new_width = target_size
43
+ new_height = int((target_size / width) * height)
44
+ else:
45
+ new_height = target_size
46
+ new_width = int((target_size / height) * width)
47
+
48
+ return image.resize((new_width, new_height))
49
+
50
+
51
  def image_to_base64(image):
52
  buffered = BytesIO()
53
  image.save(buffered, format="PNG")
 
58
  with gr.Blocks() as demo:
59
  gr.Markdown("# Qwen-VL Object-Detection")
60
  gr.Markdown(
61
+ "Compare [Qwen3-VL](https://huggingface.co/collections/Qwen/qwen3-vl), [Qwen2.5-VL](https://huggingface.co/collections/Qwen/qwen25-vl) and [Qwen2-VL](https://huggingface.co/collections/Qwen/qwen2-vl) models by [Qwen](https://huggingface.co/Qwen) for object detection."
62
  )
63
 
64
  if DEVICE != "cuda":
 
101
  step=32,
102
  interactive=True,
103
  )
104
+ image_target_size = gr.Slider(
105
+ label="Image Target Size (longest side)",
106
+ minimum=256,
107
+ maximum=3072,
108
+ value=1024,
109
+ step=1,
110
+ interactive=True,
111
+ )
112
 
113
  with gr.Column():
114
  gr.Markdown("## Outputs")
 
137
 
138
  def run(
139
  image,
140
+ image_target_size: int,
141
  system_prompt: str,
142
  user_prompt: str,
143
  model_id: str,
 
188
  "content": [
189
  {
190
  "type": "image",
191
+ "image": f"data:image;base64,{image_to_base64(scale_image(image, image_target_size))}",
192
  },
193
  {"type": "text", "text": system_prompt},
194
  {"type": "text", "text": user_prompt},
 
254
  fn=run,
255
  inputs=[
256
  image_input,
257
+ image_target_size,
258
  system_prompt,
259
  user_prompt,
260
  input_model_id,