makeitfr commited on
Commit
d9962b3
·
verified ·
1 Parent(s): 5a61e42

Upload OmniParser/gradio_demo.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. OmniParser/gradio_demo.py +97 -0
OmniParser/gradio_demo.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+
10
+ import base64, os
11
+ from util.utils import check_ocr_box, get_yolo_model, get_caption_model_processor, get_som_labeled_img
12
+ import torch
13
+ from PIL import Image
14
+
15
+ yolo_model = get_yolo_model(model_path='weights/icon_detect/model.pt')
16
+ # Florence-2 model removed - using OCR-only mode for pure vision-based GUI parsing
17
+ caption_model_processor = None
18
+ # caption_model_processor = get_caption_model_processor(model_name="blip2", model_name_or_path="weights/icon_caption_blip2")
19
+
20
+ MARKDOWN = """
21
+ # OmniParser for Pure Vision Based General GUI Agent 🔥
22
+ <div>
23
+ <a href="https://arxiv.org/pdf/2408.00203">
24
+ <img src="https://img.shields.io/badge/arXiv-2408.00203-b31b1b.svg" alt="Arxiv" style="display:inline-block;">
25
+ </a>
26
+ </div>
27
+
28
+ OmniParser is a screen parsing tool to convert general GUI screen to structured elements.
29
+ """
30
+
31
+ DEVICE = torch.device('cuda')
32
+
33
+ # @spaces.GPU
34
+ # @torch.inference_mode()
35
+ # @torch.autocast(device_type="cuda", dtype=torch.bfloat16)
36
+ def process(
37
+ image_input,
38
+ box_threshold,
39
+ iou_threshold,
40
+ use_paddleocr,
41
+ imgsz
42
+ ) -> Optional[Image.Image]:
43
+
44
+ box_overlay_ratio = image_input.size[0] / 3200
45
+ draw_bbox_config = {
46
+ 'text_scale': 0.8 * box_overlay_ratio,
47
+ 'text_thickness': max(int(2 * box_overlay_ratio), 1),
48
+ 'text_padding': max(int(3 * box_overlay_ratio), 1),
49
+ 'thickness': max(int(3 * box_overlay_ratio), 1),
50
+ }
51
+ # import pdb; pdb.set_trace()
52
+
53
+ ocr_bbox_rslt, is_goal_filtered = check_ocr_box(image_input, display_img = False, output_bb_format='xyxy', goal_filtering=None, easyocr_args={'paragraph': False, 'text_threshold':0.9}, use_paddleocr=use_paddleocr)
54
+ text, ocr_bbox = ocr_bbox_rslt
55
+ dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(image_input, 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=text,iou_threshold=iou_threshold, imgsz=imgsz,)
56
+ image = Image.open(io.BytesIO(base64.b64decode(dino_labled_img)))
57
+ print('finish processing')
58
+ parsed_content_list = '\n'.join([f'icon {i}: ' + str(v) for i,v in enumerate(parsed_content_list)])
59
+ # parsed_content_list = str(parsed_content_list)
60
+ return image, str(parsed_content_list)
61
+
62
+ with gr.Blocks() as demo:
63
+ gr.Markdown(MARKDOWN)
64
+ with gr.Row():
65
+ with gr.Column():
66
+ image_input_component = gr.Image(
67
+ type='pil', label='Upload image')
68
+ # set the threshold for removing the bounding boxes with low confidence, default is 0.05
69
+ box_threshold_component = gr.Slider(
70
+ label='Box Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.05)
71
+ # set the threshold for removing the bounding boxes with large overlap, default is 0.1
72
+ iou_threshold_component = gr.Slider(
73
+ label='IOU Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.1)
74
+ use_paddleocr_component = gr.Checkbox(
75
+ label='Use PaddleOCR', value=True)
76
+ imgsz_component = gr.Slider(
77
+ label='Icon Detect Image Size', minimum=640, maximum=1920, step=32, value=640)
78
+ submit_button_component = gr.Button(
79
+ value='Submit', variant='primary')
80
+ with gr.Column():
81
+ image_output_component = gr.Image(type='pil', label='Image Output')
82
+ text_output_component = gr.Textbox(label='Parsed screen elements', placeholder='Text Output')
83
+
84
+ submit_button_component.click(
85
+ fn=process,
86
+ inputs=[
87
+ image_input_component,
88
+ box_threshold_component,
89
+ iou_threshold_component,
90
+ use_paddleocr_component,
91
+ imgsz_component
92
+ ],
93
+ outputs=[image_output_component, text_output_component]
94
+ )
95
+
96
+ # demo.launch(debug=False, show_error=True, share=True)
97
+ demo.launch(share=True, server_port=7861, server_name='127.0.0.1')