Spaces:
Running
on
Zero
Running
on
Zero
init
Browse files- app.py +174 -4
- img_utils.py +233 -0
app.py
CHANGED
@@ -1,7 +1,177 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
|
3 |
+
from transformers.image_utils import load_image
|
4 |
+
import torch
|
5 |
+
import spaces
|
6 |
+
import re
|
7 |
+
import base64
|
8 |
+
from PIL import Image, ImageDraw
|
9 |
+
from io import BytesIO
|
10 |
+
from img_utils import smart_resize
|
11 |
|
12 |
+
SYS_PROMPT = """You are a helpful assistant.
|
|
|
13 |
|
14 |
+
# Tools
|
15 |
+
|
16 |
+
You may call one or more functions to assist with the user query.
|
17 |
+
|
18 |
+
You are provided with function signatures within <tools></tools> XML tags:
|
19 |
+
<tools>
|
20 |
+
{{"type": "function", "function": {{"name": "computer_use", "description": "Use a mouse and keyboard to interact with a computer, and take screenshots.\n* This is an interface to a desktop GUI. You do not have access to a terminal or applications menu. You must click on desktop icons to start applications.\n* Some applications may take time to start or process actions, so you may need to wait and take successive screenshots to see the results of your actions. E.g. if you click on Firefox and a window doesn't open, try wait and taking another screenshot.\n* The screen's resolution is {width}x{height}.\n* Whenever you intend to move the cursor to click on an element like an icon, you should consult a screenshot to determine the coordinates of the element before moving the cursor.\n* If you tried clicking on a program or link but it failed to load, even after waiting, try adjusting your cursor position so that the tip of the cursor visually falls on the element that you want to click.\n* Make sure to click any buttons, links, icons, etc with the cursor tip in the center of the element. Don't click boxes on their edges unless asked.", "parameters": {{"properties": {{"action": {{"description": "The action to perform. The available actions are:\n* `key`: Performs key down presses on the arguments passed in order, then performs key releases in reverse order.\n* `type`: Type a string of text on the keyboard.\n* `mouse_move`: Move the cursor to a specified (x, y) pixel coordinate on the screen.\n* `left_click`: Click the left mouse button.\n* `left_click_drag`: Click and drag the cursor to a specified (x, y) pixel coordinate on the screen.\n* `right_click`: Click the right mouse button.\n* `middle_click`: Click the middle mouse button.\n* `double_click`: Double-click the left mouse button.\n* `scroll`: Performs a scroll of the mouse scroll wheel.\n* `wait`: Wait specified seconds for the change to happen.\n* `terminate`: Terminate the current task and report its completion status.", "enum": ["key", "type", "mouse_move", "left_click", "left_click_drag", "right_click", "middle_click", "double_click", "scroll", "wait", "terminate"], "type": "string"}}, "keys": {{"description": "Required only by `action=key`.", "type": "array"}}, "text": {{"description": "Required only by `action=type`.", "type": "string"}}, "coordinate": {{"description": "(x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to move the mouse to. Required only by `action=mouse_move`, `action=left_click_drag`, `action=left_click`, `action=right_click`, `action=double_click`.", "type": "array"}}, "pixels": {{"description": "The amount of scrolling to perform. Positive values scroll up, negative values scroll down. Required only by `action=scroll`.", "type": "number"}}, "time": {{"description": "The seconds to wait. Required only by `action=wait`.", "type": "number"}}, "status": {{"description": "The status of the task. Required only by `action=terminate`.", "type": "string", "enum": ["success", "failure"]}}}}, "required": ["action"], "type": "object"}}}}}}
|
21 |
+
</tools>
|
22 |
+
|
23 |
+
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
|
24 |
+
<tool_call>
|
25 |
+
{{"name": <function-name>, "arguments": <args-json-object>}}
|
26 |
+
</tool_call>
|
27 |
+
"""
|
28 |
+
|
29 |
+
MODEL_ID = "xlangai/Jedi-7B-1080p"
|
30 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
31 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
32 |
+
MODEL_ID,
|
33 |
+
trust_remote_code=True,
|
34 |
+
torch_dtype=torch.bfloat16
|
35 |
+
).to("cuda").eval()
|
36 |
+
|
37 |
+
def image_to_base64(image):
|
38 |
+
buffered = BytesIO()
|
39 |
+
image.save(buffered, format="PNG")
|
40 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
41 |
+
return img_str
|
42 |
+
|
43 |
+
def draw_bounding_boxes(image, bounding_boxes, outline_color="red", line_width=2):
|
44 |
+
draw = ImageDraw.Draw(image)
|
45 |
+
for box in bounding_boxes:
|
46 |
+
xmin, ymin, xmax, ymax = box
|
47 |
+
draw.rectangle([xmin, ymin, xmax, ymax], outline=outline_color, width=line_width)
|
48 |
+
return image
|
49 |
+
|
50 |
+
def rescale_bounding_boxes(bounding_boxes, original_width, original_height, scaled_width=1000, scaled_height=1000):
|
51 |
+
x_scale = original_width / scaled_width
|
52 |
+
y_scale = original_height / scaled_height
|
53 |
+
rescaled_boxes = []
|
54 |
+
for box in bounding_boxes:
|
55 |
+
xmin, ymin, xmax, ymax = box
|
56 |
+
rescaled_box = [
|
57 |
+
xmin * x_scale,
|
58 |
+
ymin * y_scale,
|
59 |
+
xmax * x_scale,
|
60 |
+
ymax * y_scale
|
61 |
+
]
|
62 |
+
rescaled_boxes.append(rescaled_box)
|
63 |
+
return rescaled_boxes
|
64 |
+
|
65 |
+
def parse_coordinates(response):
|
66 |
+
try:
|
67 |
+
action = json.loads(
|
68 |
+
response.split("<tool_call>\n")[1].split("\n</tool_call>")[0]
|
69 |
+
)
|
70 |
+
action_name = action["name"]
|
71 |
+
action_type = action["arguments"]["action"]
|
72 |
+
|
73 |
+
print(f"action_name: {action_name}, action_type: {action_type}")
|
74 |
+
|
75 |
+
if action_type == "wait":
|
76 |
+
return [-1, -1, -1, -1]
|
77 |
+
|
78 |
+
action_args = action["arguments"]["coordinate"]
|
79 |
+
|
80 |
+
# Return as [x1, y1, x2, y2] format for bounding box
|
81 |
+
return [action_args[0], action_args[1], action_args[0], action_args[1]]
|
82 |
+
except Exception as e:
|
83 |
+
print(f"Error parsing coordinates: {e}\nResponse: {response}")
|
84 |
+
return None
|
85 |
+
|
86 |
+
@spaces.GPU
|
87 |
+
def run_inference(image, text_input):
|
88 |
+
if image is None:
|
89 |
+
return "Please upload an image", "", None
|
90 |
+
|
91 |
+
if not text_input:
|
92 |
+
text_input = "Describe this image in detail"
|
93 |
+
|
94 |
+
width, height = smart_resize(image.height, image.width)
|
95 |
+
|
96 |
+
messages = [
|
97 |
+
{
|
98 |
+
"role": "system",
|
99 |
+
"content": SYS_PROMPT.format(width=width, height=height)
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"role": "user",
|
103 |
+
"content": [
|
104 |
+
{"type": "image", "image": image},
|
105 |
+
{"type": "text", "text": text_input},
|
106 |
+
],
|
107 |
+
}
|
108 |
+
]
|
109 |
+
|
110 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
111 |
+
inputs = processor(
|
112 |
+
text=[text],
|
113 |
+
images=[image],
|
114 |
+
return_tensors="pt",
|
115 |
+
padding=True,
|
116 |
+
).to("cuda")
|
117 |
+
|
118 |
+
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
119 |
+
generated_ids_trimmed = [
|
120 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
121 |
+
]
|
122 |
+
output_text = processor.batch_decode(
|
123 |
+
generated_ids_trimmed, skip_special_tokens=False, clean_up_tokenization_spaces=False
|
124 |
+
)[0]
|
125 |
+
|
126 |
+
try:
|
127 |
+
# Parse coordinates using the function from the reference code
|
128 |
+
coordinates = parse_coordinates(full_response)
|
129 |
+
|
130 |
+
if coordinates is None:
|
131 |
+
return f"Failed to parse coordinates from response: {output_text}", "", image
|
132 |
+
|
133 |
+
if coordinates == [-1, -1, -1, -1]:
|
134 |
+
return "Task deemed infeasible by the model (wait action returned)", "", image
|
135 |
+
|
136 |
+
# Create a bounding box with a small area around the click point
|
137 |
+
click_x, click_y = coordinates[0], coordinates[1]
|
138 |
+
box_size = 20 # 20px box around the click point
|
139 |
+
box = [
|
140 |
+
click_x - box_size/2,
|
141 |
+
click_y - box_size/2,
|
142 |
+
click_x + box_size/2,
|
143 |
+
click_y + box_size/2
|
144 |
+
]
|
145 |
+
|
146 |
+
# Draw the bounding box on the image
|
147 |
+
annotated_image = draw_bounding_boxes(image.copy(), [box])
|
148 |
+
|
149 |
+
return f"Click coordinates: ({click_x}, {click_y})", str(coordinates), annotated_image
|
150 |
+
|
151 |
+
css = """
|
152 |
+
#output {
|
153 |
+
height: 500px;
|
154 |
+
overflow: auto;
|
155 |
+
border: 1px solid #ccc;
|
156 |
+
}
|
157 |
+
"""
|
158 |
+
|
159 |
+
with gr.Blocks(css=css) as demo:
|
160 |
+
gr.Markdown(
|
161 |
+
"""
|
162 |
+
# Qwen2.5-VL Image Element Locator
|
163 |
+
Upload an image and provide a description of an element to locate.
|
164 |
+
""")
|
165 |
+
with gr.Row():
|
166 |
+
with gr.Column():
|
167 |
+
input_img = gr.Image(label="Input Image", type="pil")
|
168 |
+
text_input = gr.Textbox(label="User Prompt (e.g., 'select search textfield')")
|
169 |
+
submit_btn = gr.Button(value="Submit")
|
170 |
+
with gr.Column():
|
171 |
+
model_output_text = gr.Textbox(label="Model Output Text", lines=5)
|
172 |
+
model_output_box = gr.Textbox(label="Coordinates", lines=2)
|
173 |
+
annotated_image = gr.Image(label="Annotated Image")
|
174 |
+
|
175 |
+
submit_btn.click(run_inference, [input_img, text_input], [model_output_text, model_output_box, annotated_image])
|
176 |
+
|
177 |
+
demo.launch(debug=True)
|
img_utils.py
ADDED
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
]
|