1.引言
通過上篇文章介紹 OmniParser:下一代純視覺UI自動化測試先驅相信大家已經對OmniParser有初步了解,接下來詳細介紹下OmniParser使用了哪些技術模型實現了對UI純視覺的檢測和理解。
2.整體方案
通過閱讀OmniParser提供的運行Demo代碼知道,其實整個UI純視覺檢測主要分為2部分,涉及3個環節分別是:圖片OCR、圖片icon檢測、圖片元素理解,分別使用的模型為:
環節 | 模型 | 作用 |
---|---|---|
圖片OCR | paddle_ocr | 識別圖片文字區域和坐標 |
圖片icon檢測 | yolov8n | 獲取圖片目標區域和坐標 |
圖片元素理解 | Florence-2-base-ft | 對檢測到的元素理解 |
以下是從官網提供的demo程序中的截取:
def parse(self, image_base64: str):image_bytes = base64.b64decode(image_base64)image = Image.open(io.BytesIO(image_bytes))print('image size:', image.size)box_overlay_ratio = max(image.size) / 3200draw_bbox_config = {'text_scale': 0.8 * box_overlay_ratio,'text_thickness': max(int(2 * box_overlay_ratio), 1),'text_padding': max(int(3 * box_overlay_ratio), 1),'thickness': max(int(3 * box_overlay_ratio), 1),}(text, ocr_bbox), _ = check_ocr_box(image, display_img=False, output_bb_format='xyxy', easyocr_args={'text_threshold': 0.8}, use_paddleocr=False)dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(image, self.som_model, BOX_TRESHOLD = self.config['BOX_TRESHOLD'], output_coord_in_ratio=True, ocr_bbox=ocr_bbox,draw_bbox_config=draw_bbox_config, caption_model_processor=self.caption_model_processor, ocr_text=text,use_local_semantics=True, iou_threshold=0.7, scale_img=False, batch_size=128)return dino_labled_img, parsed_content_list, label_coordinates
3.模型的介紹
3.1 Paddle_ocr
官方針對圖片字符識別推薦使用的是paddle_ocr,猜測原因應該是效果不錯 + 開源,接下來介紹下paddle_ocr基本使用。
3.1.1安裝
pip install paddleocr
由于PaddleOCR支持多種語言,需要設置一些配置參數,以下為Demo設置的參數,識別的語言設置的英文,這里如果識別中文修改為lang=‘ch’:
paddle_ocr = PaddleOCR(lang='en', # other lang also availableuse_angle_cls=False,use_gpu=False, # using cuda will conflict with pytorch in the same processshow_log=False,max_batch_size=1024,use_dilation=True, # improves accuracydet_db_score_mode='slow', # improves accuracyrec_batch_num=1024)
result = paddle_ocr.ocr(image_np, cls=False)[0]
識別結果格式示例如下,分為有文本框,文字和識別置信度
[[[28.0, 37.0], [302.0, 39.0], [302.0, 72.0], [27.0, 70.0]], ('純臻營養護發素', 0.9658738374710083)]
......
3.2 YOLOv8n
3.2.1. 圖標檢測模型簡介
圖標檢測模型是OmniParser-v2的基礎組件之一,主要負責從屏幕截圖中識別并定位可交互的UI元素,如按鈕、輸入框等。該模型經過大規模數據集訓練,能夠檢測最小至8×8像素的元素,確保在各種分辨率和界面復雜度下都能準確識別。使用的檢測模型為YOLOv8,以下為官網的介紹:
YOLOv8 was released by Ultralytics on January 10th, 2023, offering cutting-edge performance in terms of accuracy and speed. Building upon the advancements of previous YOLO versions, YOLOv8 introduced new features and optimizations that make it an ideal choice for various object detection tasks in a wide range of applications.
翻譯:YOLOv8 由 Ultralytics 于 2023 年 1 月 10 日發布,在準確率和速度方面提供一流的性能。在之前 YOLO 版本的改進基礎上,YOLOv8 引入了新功能和優化,使其成為廣泛應用中各種對象檢測任務的理想選擇。
下面圖片是官網提供COCO數據上檢測結果對比,v8一共有5種變形模型,從官方的測試結果看yolov8n其實是效果最差的,不知OmniParser為啥選這個檢測模型。
# Display model information (optional)
model.info()# Train the model on the COCO8 example dataset for 100 epochs
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)# Run inference with the YOLOv8n model on the 'bus.jpg' image
results = model("path/to/bus.jpg")
這里推薦個YOLO網絡結構可視化的工具:netron具體用法可以搜下,后續再抽時間解讀網絡結構。
3.2.3 YOLOv8 主要特點
- 先進的主干和頸部架構:
YOLOv8 employs state-of-the-art backbone and neck architectures, resulting in improved feature extraction and object detection performance.
- 無錨分割 Ultralytics 頭:
YOLOv8 adopts an anchor-free split Ultralytics head, which contributes to better accuracy and a more efficient detection process compared to anchor-based approaches.
- 優化的準確度-速度權衡:
With a focus on maintaining an optimal balance between accuracy and speed, YOLOv8 is suitable for real-time object detection tasks in diverse application areas.
- 優化的準確度-速度權衡:
YOLOv8 offers a range of pre-trained models to cater to various tasks and performance requirements, making it easier to find the right model for your specific use case.
3. Florence-2
Florence-2 是微軟與2024年6月發布的多模態大模型,專為視覺理解任務設計,旨在處理多種視覺和視覺-語言任務。它在計算機視覺領域展現了強大的泛化能力,能夠處理多種視覺任務,如圖像理解、對象檢測、圖像字幕生成等。
Florence-2 主要特點:
- 通用視覺理解:采用模塊化設計,方便集成不同的LLM,滿足多樣化需求。
- 多模態學習:結合自然語言處理能力,實現視覺-語言任務,如圖像字幕生成和視覺問答。
- 大規模數據訓練:使用大規模、高質量的數據集進行訓練,提升模型的泛化能力和魯棒性。
- 高效推理:優化了計算效率,使其能夠在云端和本地設備上快速推理。
- 跨任務一致性:在不同的視覺任務上表現穩定,適用于多種應用場景,如自動駕駛、醫療影像分析和智能監控。
在官網Demo中可以看出使用的是Florence-2-base,Model size是0.23B,整體比較小,下面是運行模型的demo程序。
import requestsfrom PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)prompt = "<OD>"url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)generated_ids = model.generate(input_ids=inputs["input_ids"],pixel_values=inputs["pixel_values"],max_new_tokens=1024,do_sample=False,num_beams=3
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))print(parsed_answer)
4.結語
OmniParser-v2通過集成上述多種先進模型,實現了從屏幕截圖到結構化數據的高效轉換,為大型語言模型賦予了實際的計算機操作能力。但仔細分析使用的模型分支都為參數量最小的,實際測試下來效果的話也會大打折扣,猜測應該是因為開源替換了原先效果更優的模型。后續繼續挖掘下每個模型具體的實現以及如何進行模型的微調,幫助大家能在具體的業務場景實現定制化。
參考資料:
- paddleocr官方文檔
- ultralytics
- Florence-2-base-HF