Spaces:
Running
Running
Upload 2 files
Browse files- .gitignore +12 -0
- gradio_demo.py +107 -0
.gitignore
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
weights/icon_caption_blip2
|
2 |
+
weights/icon_caption_florence
|
3 |
+
weights/icon_detect/
|
4 |
+
weights/icon_detect_v1_5/
|
5 |
+
weights/icon_detect_v1_5_2/
|
6 |
+
.gradio
|
7 |
+
__pycache__/
|
8 |
+
debug.ipynb
|
9 |
+
util/__pycache__/
|
10 |
+
index.html?linkid=2289031
|
11 |
+
wget-log
|
12 |
+
weights/icon_caption_florence_v2/
|
gradio_demo.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
import base64, os
|
9 |
+
from util.utils import check_ocr_box, get_yolo_model, get_caption_model_processor, get_som_labeled_img
|
10 |
+
import torch
|
11 |
+
from PIL import Image
|
12 |
+
import ast
|
13 |
+
|
14 |
+
# 定义模型路径,使用相对路径,并使用 os.path.join 确保跨平台兼容性
|
15 |
+
MODEL_DIR = 'weights'
|
16 |
+
YOLO_MODEL_PATH = os.path.join(MODEL_DIR, 'icon_detect', 'model.pt')
|
17 |
+
CAPTION_MODEL_PATH = os.path.join(MODEL_DIR, 'icon_caption')
|
18 |
+
# BLIP2_CAPTION_MODEL_PATH = os.path.join(MODEL_DIR, 'icon_caption_blip2') # 如果使用 BLIP2 模型
|
19 |
+
|
20 |
+
yolo_model = get_yolo_model(model_path='weights/icon_detect/model.pt')
|
21 |
+
caption_model_processor = get_caption_model_processor(model_name="ollama", model_name_or_path=CAPTION_MODEL_PATH)
|
22 |
+
# caption_model_processor = get_caption_model_processor(model_name="blip2", model_name_or_path="weights/icon_caption_blip2")
|
23 |
+
|
24 |
+
MARKDOWN = """
|
25 |
+
# OmniParser for Pure Vision Based General GUI Agent嘻嘻 🔥
|
26 |
+
<div>
|
27 |
+
<a href="https://arxiv.org/pdf/2408.00203">
|
28 |
+
<img src="https://img.shields.io/badge/arXiv-2408.00203-b31b1b.svg" alt="Arxiv" style="display:inline-block;">
|
29 |
+
</a>
|
30 |
+
</div>
|
31 |
+
|
32 |
+
OmniParser is a screen parsing tool to convert general GUI screen to structured elements.
|
33 |
+
"""
|
34 |
+
|
35 |
+
DEVICE = torch.device('cuda')
|
36 |
+
|
37 |
+
def process(
|
38 |
+
image_input,
|
39 |
+
box_threshold,
|
40 |
+
iou_threshold,
|
41 |
+
use_paddleocr,
|
42 |
+
imgsz
|
43 |
+
) -> Optional[Image.Image]:
|
44 |
+
|
45 |
+
image_save_path = 'imgs/saved_image_demo.png'
|
46 |
+
image_input.save(image_save_path)
|
47 |
+
image = Image.open(image_save_path)
|
48 |
+
box_overlay_ratio = image.size[0] / 3200
|
49 |
+
draw_bbox_config = {
|
50 |
+
'text_scale': 0.8 * box_overlay_ratio,
|
51 |
+
'text_thickness': max(int(2 * box_overlay_ratio), 1),
|
52 |
+
'text_padding': max(int(3 * box_overlay_ratio), 1),
|
53 |
+
'thickness': max(int(3 * box_overlay_ratio), 1),
|
54 |
+
}
|
55 |
+
|
56 |
+
ocr_bbox_rslt, is_goal_filtered = check_ocr_box(image_save_path, display_img = False, output_bb_format='xyxy', goal_filtering=None, easyocr_args={'paragraph': False, 'text_threshold':0.9}, use_paddleocr=use_paddleocr)
|
57 |
+
text, ocr_bbox_input = ocr_bbox_rslt
|
58 |
+
|
59 |
+
# Correctly handle ocr_bbox and ocr_text
|
60 |
+
if ocr_bbox_input is None or not ocr_bbox_input:
|
61 |
+
ocr_bbox = []
|
62 |
+
ocr_text = []
|
63 |
+
else:
|
64 |
+
ocr_bbox = []
|
65 |
+
for box_str in ocr_bbox_input:
|
66 |
+
try:
|
67 |
+
# 使用 eval(),但要非常小心!
|
68 |
+
box = eval(box_str) # 转换为元组
|
69 |
+
ocr_bbox.append(box)
|
70 |
+
except (SyntaxError, NameError, TypeError, ValueError):
|
71 |
+
print(f"警告:无法解析边界框字符串:{box_str}") # 打印警告信息,但继续处理其他框
|
72 |
+
continue # 跳过错误的框
|
73 |
+
ocr_text = text # 使用 check_ocr_box 返回的 text
|
74 |
+
|
75 |
+
dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(image_save_path, yolo_model, BOX_TRESHOLD=box_threshold, output_coord_in_ratio=True, ocr_bbox=ocr_bbox, draw_bbox_config=draw_bbox_config, caption_model_processor=caption_model_processor, ocr_text=ocr_text, iou_threshold=iou_threshold, imgsz=imgsz)
|
76 |
+
image = Image.open(io.BytesIO(base64.b64decode(dino_labled_img)))
|
77 |
+
print('finish processing')
|
78 |
+
parsed_content_list = '\n'.join([f'icon {i}: ' + str(v) for i,v in enumerate(parsed_content_list)])
|
79 |
+
return image, str(parsed_content_list)
|
80 |
+
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
gr.Markdown(MARKDOWN)
|
83 |
+
with gr.Row():
|
84 |
+
with gr.Column():
|
85 |
+
image_input_component = gr.Image(type='pil', label='Upload image')
|
86 |
+
box_threshold_component = gr.Slider(label='Box Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.05)
|
87 |
+
iou_threshold_component = gr.Slider(label='IOU Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.1)
|
88 |
+
use_paddleocr_component = gr.Checkbox(label='Use PaddleOCR', value=True)
|
89 |
+
imgsz_component = gr.Slider(label='Icon Detect Image Size', minimum=640, maximum=1920, step=32, value=640)
|
90 |
+
submit_button_component = gr.Button(value='Submit', variant='primary')
|
91 |
+
with gr.Column():
|
92 |
+
image_output_component = gr.Image(type='pil', label='Image Output')
|
93 |
+
text_output_component = gr.Textbox(label='Parsed screen elements', placeholder='Text Output')
|
94 |
+
|
95 |
+
submit_button_component.click(
|
96 |
+
fn=process,
|
97 |
+
inputs=[
|
98 |
+
image_input_component,
|
99 |
+
box_threshold_component,
|
100 |
+
iou_threshold_component,
|
101 |
+
use_paddleocr_component,
|
102 |
+
imgsz_component
|
103 |
+
],
|
104 |
+
outputs=[image_output_component, text_output_component]
|
105 |
+
)
|
106 |
+
|
107 |
+
demo.launch(share=True, server_port=7861, server_name='0.0.0.0')
|