not-lain commited on
Commit
7e39e4c
·
1 Parent(s): 0ef105d

fix linting

Browse files
Files changed (1) hide show
  1. app.py +63 -36
app.py CHANGED
@@ -1,16 +1,18 @@
1
  from typing import Optional
2
  import spaces
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
  from huggingface_hub import snapshot_download
16
 
@@ -24,8 +26,10 @@ snapshot_download(repo_id=repo_id, local_dir=local_dir)
24
  print(f"Repository downloaded to: {local_dir}")
25
 
26
 
27
- yolo_model = get_yolo_model(model_path='weights/icon_detect/model.pt')
28
- caption_model_processor = get_caption_model_processor(model_name="florence2", model_name_or_path="weights/icon_caption")
 
 
29
  # caption_model_processor = get_caption_model_processor(model_name="blip2", model_name_or_path="weights/icon_caption_blip2")
30
 
31
  MARKDOWN = """
@@ -39,61 +43,84 @@ MARKDOWN = """
39
  OmniParser is a screen parsing tool to convert general GUI screen to structured elements.
40
  """
41
 
42
- DEVICE = torch.device('cuda')
 
43
 
44
  @spaces.GPU
45
  @torch.inference_mode()
46
  # @torch.autocast(device_type="cuda", dtype=torch.bfloat16)
47
  def process(
48
- image_input,
49
- box_threshold,
50
- iou_threshold,
51
- use_paddleocr,
52
- imgsz
53
  ) -> Optional[Image.Image]:
54
-
55
  # image_save_path = 'imgs/saved_image_demo.png'
56
  # image_input.save(image_save_path)
57
  # image = Image.open(image_save_path)
58
  box_overlay_ratio = image_input.size[0] / 3200
59
  draw_bbox_config = {
60
- 'text_scale': 0.8 * box_overlay_ratio,
61
- 'text_thickness': max(int(2 * box_overlay_ratio), 1),
62
- 'text_padding': max(int(3 * box_overlay_ratio), 1),
63
- 'thickness': max(int(3 * box_overlay_ratio), 1),
64
  }
65
  # import pdb; pdb.set_trace()
66
 
67
- 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)
 
 
 
 
 
 
 
68
  text, ocr_bbox = ocr_bbox_rslt
69
- 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,)
 
 
 
 
 
 
 
 
 
 
 
70
  image = Image.open(io.BytesIO(base64.b64decode(dino_labled_img)))
71
- print('finish processing')
72
- parsed_content_list = '\n'.join([f'icon {i}: ' + str(v) for i,v in enumerate(parsed_content_list)])
 
 
73
  # parsed_content_list = str(parsed_content_list)
74
  return image, str(parsed_content_list)
75
 
 
76
  with gr.Blocks() as demo:
77
  gr.Markdown(MARKDOWN)
78
  with gr.Row():
79
  with gr.Column():
80
- image_input_component = gr.Image(
81
- type='pil', label='Upload image')
82
  # set the threshold for removing the bounding boxes with low confidence, default is 0.05
83
  box_threshold_component = gr.Slider(
84
- label='Box Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.05)
 
85
  # set the threshold for removing the bounding boxes with large overlap, default is 0.1
86
  iou_threshold_component = gr.Slider(
87
- label='IOU Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.1)
88
- use_paddleocr_component = gr.Checkbox(
89
- label='Use PaddleOCR', value=True)
90
  imgsz_component = gr.Slider(
91
- label='Icon Detect Image Size', minimum=640, maximum=1920, step=32, value=640)
92
- submit_button_component = gr.Button(
93
- value='Submit', variant='primary')
 
 
 
 
94
  with gr.Column():
95
- image_output_component = gr.Image(type='pil', label='Image Output')
96
- text_output_component = gr.Textbox(label='Parsed screen elements', placeholder='Text Output')
 
 
97
 
98
  submit_button_component.click(
99
  fn=process,
@@ -102,11 +129,11 @@ with gr.Blocks() as demo:
102
  box_threshold_component,
103
  iou_threshold_component,
104
  use_paddleocr_component,
105
- imgsz_component
106
  ],
107
- outputs=[image_output_component, text_output_component]
108
  )
109
 
110
  # demo.launch(debug=False, show_error=True, share=True)
111
  # demo.launch(share=True, server_port=7861, server_name='0.0.0.0')
112
- demo.queue().launch(share=False)
 
1
  from typing import Optional
2
  import spaces
3
  import gradio as gr
 
4
  import torch
5
  from PIL import Image
6
  import io
7
 
8
 
9
+ import base64
10
+ from util.utils import (
11
+ check_ocr_box,
12
+ get_yolo_model,
13
+ get_caption_model_processor,
14
+ get_som_labeled_img,
15
+ )
16
 
17
  from huggingface_hub import snapshot_download
18
 
 
26
  print(f"Repository downloaded to: {local_dir}")
27
 
28
 
29
+ yolo_model = get_yolo_model(model_path="weights/icon_detect/model.pt")
30
+ caption_model_processor = get_caption_model_processor(
31
+ model_name="florence2", model_name_or_path="weights/icon_caption"
32
+ )
33
  # caption_model_processor = get_caption_model_processor(model_name="blip2", model_name_or_path="weights/icon_caption_blip2")
34
 
35
  MARKDOWN = """
 
43
  OmniParser is a screen parsing tool to convert general GUI screen to structured elements.
44
  """
45
 
46
+ DEVICE = torch.device("cuda")
47
+
48
 
49
  @spaces.GPU
50
  @torch.inference_mode()
51
  # @torch.autocast(device_type="cuda", dtype=torch.bfloat16)
52
  def process(
53
+ image_input, box_threshold, iou_threshold, use_paddleocr, imgsz
 
 
 
 
54
  ) -> Optional[Image.Image]:
 
55
  # image_save_path = 'imgs/saved_image_demo.png'
56
  # image_input.save(image_save_path)
57
  # image = Image.open(image_save_path)
58
  box_overlay_ratio = image_input.size[0] / 3200
59
  draw_bbox_config = {
60
+ "text_scale": 0.8 * box_overlay_ratio,
61
+ "text_thickness": max(int(2 * box_overlay_ratio), 1),
62
+ "text_padding": max(int(3 * box_overlay_ratio), 1),
63
+ "thickness": max(int(3 * box_overlay_ratio), 1),
64
  }
65
  # import pdb; pdb.set_trace()
66
 
67
+ ocr_bbox_rslt, is_goal_filtered = check_ocr_box(
68
+ image_input,
69
+ display_img=False,
70
+ output_bb_format="xyxy",
71
+ goal_filtering=None,
72
+ easyocr_args={"paragraph": False, "text_threshold": 0.9},
73
+ use_paddleocr=use_paddleocr,
74
+ )
75
  text, ocr_bbox = ocr_bbox_rslt
76
+ dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(
77
+ image_input,
78
+ yolo_model,
79
+ BOX_TRESHOLD=box_threshold,
80
+ output_coord_in_ratio=True,
81
+ ocr_bbox=ocr_bbox,
82
+ draw_bbox_config=draw_bbox_config,
83
+ caption_model_processor=caption_model_processor,
84
+ ocr_text=text,
85
+ iou_threshold=iou_threshold,
86
+ imgsz=imgsz,
87
+ )
88
  image = Image.open(io.BytesIO(base64.b64decode(dino_labled_img)))
89
+ print("finish processing")
90
+ parsed_content_list = "\n".join(
91
+ [f"icon {i}: " + str(v) for i, v in enumerate(parsed_content_list)]
92
+ )
93
  # parsed_content_list = str(parsed_content_list)
94
  return image, str(parsed_content_list)
95
 
96
+
97
  with gr.Blocks() as demo:
98
  gr.Markdown(MARKDOWN)
99
  with gr.Row():
100
  with gr.Column():
101
+ image_input_component = gr.Image(type="pil", label="Upload image")
 
102
  # set the threshold for removing the bounding boxes with low confidence, default is 0.05
103
  box_threshold_component = gr.Slider(
104
+ label="Box Threshold", minimum=0.01, maximum=1.0, step=0.01, value=0.05
105
+ )
106
  # set the threshold for removing the bounding boxes with large overlap, default is 0.1
107
  iou_threshold_component = gr.Slider(
108
+ label="IOU Threshold", minimum=0.01, maximum=1.0, step=0.01, value=0.1
109
+ )
110
+ use_paddleocr_component = gr.Checkbox(label="Use PaddleOCR", value=True)
111
  imgsz_component = gr.Slider(
112
+ label="Icon Detect Image Size",
113
+ minimum=640,
114
+ maximum=1920,
115
+ step=32,
116
+ value=640,
117
+ )
118
+ submit_button_component = gr.Button(value="Submit", variant="primary")
119
  with gr.Column():
120
+ image_output_component = gr.Image(type="pil", label="Image Output")
121
+ text_output_component = gr.Textbox(
122
+ label="Parsed screen elements", placeholder="Text Output"
123
+ )
124
 
125
  submit_button_component.click(
126
  fn=process,
 
129
  box_threshold_component,
130
  iou_threshold_component,
131
  use_paddleocr_component,
132
+ imgsz_component,
133
  ],
134
+ outputs=[image_output_component, text_output_component],
135
  )
136
 
137
  # demo.launch(debug=False, show_error=True, share=True)
138
  # demo.launch(share=True, server_port=7861, server_name='0.0.0.0')
139
+ demo.queue().launch(share=False)