Spaces:
Running
Running
update
Browse files- app.py +42 -1
- img_utils.py +0 -233
app.py
CHANGED
|
@@ -4,13 +4,54 @@ import base64
|
|
| 4 |
import os
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
from io import BytesIO
|
| 7 |
-
from img_utils import smart_resize
|
| 8 |
import backoff
|
| 9 |
import httpx
|
| 10 |
from loguru import logger
|
| 11 |
import time
|
| 12 |
from typing import List, Optional
|
| 13 |
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def image_to_base64(image):
|
| 16 |
buffered = BytesIO()
|
|
|
|
| 4 |
import os
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
from io import BytesIO
|
|
|
|
| 7 |
import backoff
|
| 8 |
import httpx
|
| 9 |
from loguru import logger
|
| 10 |
import time
|
| 11 |
from typing import List, Optional
|
| 12 |
import traceback
|
| 13 |
+
import math
|
| 14 |
+
|
| 15 |
+
def smart_resize(
|
| 16 |
+
height: int,
|
| 17 |
+
width: int,
|
| 18 |
+
factor: int = 28,
|
| 19 |
+
min_pixels: int = 3136,
|
| 20 |
+
max_pixels: int = 12845056,
|
| 21 |
+
max_aspect_ratio_allowed: float | None = None,
|
| 22 |
+
size_can_be_smaller_than_factor: bool = False,
|
| 23 |
+
):
|
| 24 |
+
"""Rescales the image so that the following conditions are met:
|
| 25 |
+
|
| 26 |
+
1. Both dimensions (height and width) are divisible by 'factor'.
|
| 27 |
+
|
| 28 |
+
2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].
|
| 29 |
+
|
| 30 |
+
3. The aspect ratio of the image is maintained as closely as possible.
|
| 31 |
+
|
| 32 |
+
"""
|
| 33 |
+
if not size_can_be_smaller_than_factor and (height < factor or width < factor):
|
| 34 |
+
raise ValueError(
|
| 35 |
+
f"height:{height} or width:{width} must be larger than factor:{factor} "
|
| 36 |
+
f"(when size_can_be_smaller_than_factor is False)"
|
| 37 |
+
)
|
| 38 |
+
elif max_aspect_ratio_allowed is not None and max(height, width) / min(height, width) > max_aspect_ratio_allowed:
|
| 39 |
+
raise ValueError(
|
| 40 |
+
f"absolute aspect ratio must be smaller than {max_aspect_ratio_allowed}, "
|
| 41 |
+
f"got {max(height, width) / min(height, width)}"
|
| 42 |
+
f"(when max_aspect_ratio_allowed is not None)"
|
| 43 |
+
)
|
| 44 |
+
h_bar = max(1, round(height / factor)) * factor
|
| 45 |
+
w_bar = max(1, round(width / factor)) * factor
|
| 46 |
+
if h_bar * w_bar > max_pixels:
|
| 47 |
+
beta = math.sqrt((height * width) / max_pixels)
|
| 48 |
+
h_bar = max(1, math.floor(height / beta / factor)) * factor
|
| 49 |
+
w_bar = max(1, math.floor(width / beta / factor)) * factor
|
| 50 |
+
elif h_bar * w_bar < min_pixels:
|
| 51 |
+
beta = math.sqrt(min_pixels / (height * width))
|
| 52 |
+
h_bar = math.ceil(height * beta / factor) * factor
|
| 53 |
+
w_bar = math.ceil(width * beta / factor) * factor
|
| 54 |
+
return h_bar, w_bar
|
| 55 |
|
| 56 |
def image_to_base64(image):
|
| 57 |
buffered = BytesIO()
|
img_utils.py
DELETED
|
@@ -1,233 +0,0 @@
|
|
| 1 |
-
import math
|
| 2 |
-
from typing import List, Union, Dict, Any
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
def round_by_factor(number: int, factor: int) -> int:
|
| 6 |
-
"""返回最接近 number 的且能被 factor 整除的整数"""
|
| 7 |
-
return round(number / factor) * factor
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def ceil_by_factor(number: int, factor: int) -> int:
|
| 11 |
-
"""返回大于等于 number 的且能被 factor 整除的整数"""
|
| 12 |
-
return math.ceil(number / factor) * factor
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def floor_by_factor(number: int, factor: int) -> int:
|
| 16 |
-
"""返回小于等于 number 的且能被 factor 整除的整数"""
|
| 17 |
-
return math.floor(number / factor) * factor
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def smart_resize(height, width, factor=28, min_pixels=56 * 56, max_pixels=14 * 14 * 4 * 1280, max_long_side=8192):
|
| 21 |
-
"""缩放后图片满足以下条件:
|
| 22 |
-
1. 长宽能被 factor 整除
|
| 23 |
-
2. pixels 总数被限制在 [min_pixels, max_pixels] 内
|
| 24 |
-
3. 最长边限制在 max_long_side 内
|
| 25 |
-
4. 保证其长宽比基本不变
|
| 26 |
-
"""
|
| 27 |
-
if height < 2 or width < 2:
|
| 28 |
-
raise ValueError(f"height:{height} or width:{width} must be larger than factor:{factor}")
|
| 29 |
-
elif max(height, width) / min(height, width) > 200:
|
| 30 |
-
raise ValueError(f"absolute aspect ratio must be smaller than 100, got {height} / {width}")
|
| 31 |
-
|
| 32 |
-
if max(height, width) > max_long_side:
|
| 33 |
-
beta = max(height, width) / max_long_side
|
| 34 |
-
height, width = int(height / beta), int(width / beta)
|
| 35 |
-
|
| 36 |
-
h_bar = round_by_factor(height, factor)
|
| 37 |
-
w_bar = round_by_factor(width, factor)
|
| 38 |
-
if h_bar * w_bar > max_pixels:
|
| 39 |
-
beta = math.sqrt((height * width) / max_pixels)
|
| 40 |
-
h_bar = floor_by_factor(height / beta, factor)
|
| 41 |
-
w_bar = floor_by_factor(width / beta, factor)
|
| 42 |
-
elif h_bar * w_bar < min_pixels:
|
| 43 |
-
beta = math.sqrt(min_pixels / (height * width))
|
| 44 |
-
h_bar = ceil_by_factor(height * beta, factor)
|
| 45 |
-
w_bar = ceil_by_factor(width * beta, factor)
|
| 46 |
-
return h_bar, w_bar
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def update_image_size_(image_ele: dict, min_tokens=1, max_tokens=12800, merge_base=2, patch_size=14):
|
| 50 |
-
"""根据 min_tokens, max_tokens 更新 image_ele 的尺寸信息
|
| 51 |
-
|
| 52 |
-
Args:
|
| 53 |
-
image_ele (dict):
|
| 54 |
-
- image_ele["image"]: str 图片路径
|
| 55 |
-
- image_ele["height"]: int 图片原始高度
|
| 56 |
-
- image_ele["width"]: int 图片原始宽度
|
| 57 |
-
|
| 58 |
-
Returns:
|
| 59 |
-
更新后的 image_ele, 新增如下 key-value pair
|
| 60 |
-
dict:
|
| 61 |
-
- image_ele["resized_height"]: int 输入到模型的真实高度
|
| 62 |
-
- image_ele["resized_width"]: int 输入到模型的真实宽度
|
| 63 |
-
- image_ele["seq_len"]: int 输入到模型所占的序列长度
|
| 64 |
-
"""
|
| 65 |
-
height, width = image_ele["height"], image_ele["width"]
|
| 66 |
-
pixels_per_token = patch_size * patch_size * merge_base * merge_base
|
| 67 |
-
resized_height, resized_width = smart_resize(
|
| 68 |
-
height,
|
| 69 |
-
width,
|
| 70 |
-
factor=merge_base * patch_size,
|
| 71 |
-
min_pixels=pixels_per_token * min_tokens,
|
| 72 |
-
max_pixels=pixels_per_token * max_tokens,
|
| 73 |
-
max_long_side=50000,
|
| 74 |
-
)
|
| 75 |
-
image_ele.update(
|
| 76 |
-
{
|
| 77 |
-
"resized_height": resized_height,
|
| 78 |
-
"resized_width": resized_width,
|
| 79 |
-
"seq_len": resized_height * resized_width // pixels_per_token + 2,
|
| 80 |
-
}
|
| 81 |
-
)
|
| 82 |
-
return image_ele
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def _convert_bbox_format_from_abs_origin(bbox, image_ele: dict, *, tgt_format: str):
|
| 86 |
-
x1, y1, x2, y2 = bbox
|
| 87 |
-
if tgt_format == "abs_origin":
|
| 88 |
-
new_bbox = [int(x1), int(y1), int(x2), int(y2)]
|
| 89 |
-
elif tgt_format == "abs_resized":
|
| 90 |
-
new_bbox = [
|
| 91 |
-
int(x1 / image_ele["width"] * image_ele["resized_width"]),
|
| 92 |
-
int(y1 / image_ele["height"] * image_ele["resized_height"]),
|
| 93 |
-
int(x2 / image_ele["width"] * image_ele["resized_width"]),
|
| 94 |
-
int(y2 / image_ele["height"] * image_ele["resized_height"]),
|
| 95 |
-
]
|
| 96 |
-
elif tgt_format == "qwen-vl":
|
| 97 |
-
new_bbox = [
|
| 98 |
-
int(x1 / image_ele["width"] * 999),
|
| 99 |
-
int(y1 / image_ele["height"] * 999),
|
| 100 |
-
int(x2 / image_ele["width"] * 999),
|
| 101 |
-
int(y2 / image_ele["height"] * 999),
|
| 102 |
-
]
|
| 103 |
-
elif tgt_format == "rel":
|
| 104 |
-
new_bbox = [
|
| 105 |
-
float(x1 / image_ele["width"]),
|
| 106 |
-
float(y1 / image_ele["height"]),
|
| 107 |
-
float(x2 / image_ele["width"]),
|
| 108 |
-
float(y2 / image_ele["height"]),
|
| 109 |
-
]
|
| 110 |
-
elif tgt_format == "molmo":
|
| 111 |
-
new_bbox = [
|
| 112 |
-
round(x1 / image_ele["width"] * 100, ndigits=1),
|
| 113 |
-
round(y1 / image_ele["height"] * 100, ndigits=1),
|
| 114 |
-
round(x2 / image_ele["width"] * 100, ndigits=1),
|
| 115 |
-
round(y2 / image_ele["height"] * 100, ndigits=1),
|
| 116 |
-
]
|
| 117 |
-
else:
|
| 118 |
-
assert False, f"Unknown tgt_format: {tgt_format}"
|
| 119 |
-
return new_bbox
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
def _convert_bbox_format_to_abs_origin(bbox, image_ele: dict, *, src_format: str):
|
| 123 |
-
x1, y1, x2, y2 = bbox
|
| 124 |
-
if src_format == "abs_origin":
|
| 125 |
-
new_bbox = [int(x1), int(y1), int(x2), int(y2)]
|
| 126 |
-
elif src_format == "abs_resized":
|
| 127 |
-
new_bbox = [
|
| 128 |
-
int(x1 / image_ele["resized_width"] * image_ele["width"]),
|
| 129 |
-
int(y1 / image_ele["resized_height"] * image_ele["height"]),
|
| 130 |
-
int(x2 / image_ele["resized_width"] * image_ele["width"]),
|
| 131 |
-
int(y2 / image_ele["resized_height"] * image_ele["height"]),
|
| 132 |
-
]
|
| 133 |
-
elif src_format == "qwen-vl":
|
| 134 |
-
new_bbox = [
|
| 135 |
-
int(x1 / 999 * image_ele["width"]),
|
| 136 |
-
int(y1 / 999 * image_ele["height"]),
|
| 137 |
-
int(x2 / 999 * image_ele["width"]),
|
| 138 |
-
int(y2 / 999 * image_ele["height"]),
|
| 139 |
-
]
|
| 140 |
-
elif src_format == "rel":
|
| 141 |
-
new_bbox = [
|
| 142 |
-
int(x1 * image_ele["width"]),
|
| 143 |
-
int(y1 * image_ele["height"]),
|
| 144 |
-
int(x2 * image_ele["width"]),
|
| 145 |
-
int(y2 * image_ele["height"]),
|
| 146 |
-
]
|
| 147 |
-
elif src_format == "molmo":
|
| 148 |
-
new_bbox = [
|
| 149 |
-
int(x1 / 100 * image_ele["width"]),
|
| 150 |
-
int(y1 / 100 * image_ele["height"]),
|
| 151 |
-
int(x2 / 100 * image_ele["width"]),
|
| 152 |
-
int(y2 / 100 * image_ele["height"]),
|
| 153 |
-
]
|
| 154 |
-
else:
|
| 155 |
-
assert False, f"Unknown src_format: {src_format}"
|
| 156 |
-
return new_bbox
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
def convert_bbox_format(bbox, image_ele: dict, *, src_format: str, tgt_format: str):
|
| 160 |
-
bbox_abs_origin = _convert_bbox_format_to_abs_origin(bbox, image_ele, src_format=src_format)
|
| 161 |
-
bbox_tgt_format = _convert_bbox_format_from_abs_origin(bbox_abs_origin, image_ele, tgt_format=tgt_format)
|
| 162 |
-
return bbox_tgt_format
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
def _convert_point_format_from_abs_origin(point, image_ele: dict, *, tgt_format: str):
|
| 166 |
-
x, y = point
|
| 167 |
-
if tgt_format == "abs_origin":
|
| 168 |
-
new_point = [int(x), int(y)]
|
| 169 |
-
elif tgt_format == "abs_resized":
|
| 170 |
-
new_point = [
|
| 171 |
-
int(x / image_ele["width"] * image_ele["resized_width"]),
|
| 172 |
-
int(y / image_ele["height"] * image_ele["resized_height"]),
|
| 173 |
-
]
|
| 174 |
-
elif tgt_format == "qwen-vl":
|
| 175 |
-
new_point = [
|
| 176 |
-
int(x / image_ele["width"] * 999),
|
| 177 |
-
int(y / image_ele["height"] * 999),
|
| 178 |
-
]
|
| 179 |
-
elif tgt_format == "rel":
|
| 180 |
-
new_point = [
|
| 181 |
-
float(x / image_ele["width"]),
|
| 182 |
-
float(y / image_ele["height"]),
|
| 183 |
-
]
|
| 184 |
-
elif tgt_format == "molmo":
|
| 185 |
-
new_point = [
|
| 186 |
-
round(x / image_ele["width"] * 100, ndigits=1),
|
| 187 |
-
round(y / image_ele["height"] * 100, ndigits=1),
|
| 188 |
-
]
|
| 189 |
-
else:
|
| 190 |
-
assert False, f"Unknown tgt_format: {tgt_format}"
|
| 191 |
-
return new_point
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
def _convert_point_format_to_abs_origin(point, image_ele: dict, *, src_format: str):
|
| 195 |
-
x, y = point
|
| 196 |
-
if src_format == "abs_origin":
|
| 197 |
-
new_point = [int(x), int(y)]
|
| 198 |
-
elif src_format == "abs_resized":
|
| 199 |
-
new_point = [
|
| 200 |
-
int(x / image_ele["resized_width"] * image_ele["width"]),
|
| 201 |
-
int(y / image_ele["resized_height"] * image_ele["height"]),
|
| 202 |
-
]
|
| 203 |
-
elif src_format == "qwen-vl":
|
| 204 |
-
new_point = [
|
| 205 |
-
int(x / 999 * image_ele["width"]),
|
| 206 |
-
int(y / 999 * image_ele["height"]),
|
| 207 |
-
]
|
| 208 |
-
elif src_format == "rel":
|
| 209 |
-
new_point = [
|
| 210 |
-
int(x * image_ele["width"]),
|
| 211 |
-
int(y * image_ele["height"]),
|
| 212 |
-
]
|
| 213 |
-
elif src_format == "molmo":
|
| 214 |
-
new_point = [
|
| 215 |
-
int(x / 100 * image_ele["width"]),
|
| 216 |
-
int(y / 100 * image_ele["height"]),
|
| 217 |
-
]
|
| 218 |
-
else:
|
| 219 |
-
assert False, f"Unknown src_format: {src_format}"
|
| 220 |
-
return new_point
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
def convert_point_format(point, image_ele: dict, *, src_format: str, tgt_format: str):
|
| 224 |
-
point_abs_origin = _convert_point_format_to_abs_origin(point, image_ele, src_format=src_format)
|
| 225 |
-
point_tgt_format = _convert_point_format_from_abs_origin(point_abs_origin, image_ele, tgt_format=tgt_format)
|
| 226 |
-
return point_tgt_format
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
__all__ = [
|
| 230 |
-
"update_image_size_",
|
| 231 |
-
"convert_bbox_format",
|
| 232 |
-
"convert_point_format",
|
| 233 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|