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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Example: How to use OmniParser WITH image captioning enabled
|
| 4 |
+
===========================================================
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
# EXAMPLE 1: Start server WITH Florence captions
|
| 8 |
+
# ================================================
|
| 9 |
+
|
| 10 |
+
# In the original omniparserserver.py (before my changes):
|
| 11 |
+
# omniparser = Omniparser(config)
|
| 12 |
+
#
|
| 13 |
+
# This initializes Florence model for captioning:
|
| 14 |
+
# class Omniparser:
|
| 15 |
+
# def __init__(self, config):
|
| 16 |
+
# self.caption_model_processor = get_caption_model_processor(
|
| 17 |
+
# model_name='florence2',
|
| 18 |
+
# model_name_or_path='weights/icon_caption_florence',
|
| 19 |
+
# device='cuda' # or 'cpu'
|
| 20 |
+
# )
|
| 21 |
+
#
|
| 22 |
+
# Then parsing goes through:
|
| 23 |
+
# parse() β get_som_labeled_img() β get_parsed_content_icon()
|
| 24 |
+
# β Florence model generates captions for each UI element
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# EXAMPLE 2: How Florence Captioning Works (Pseudocode)
|
| 28 |
+
# ========================================================
|
| 29 |
+
|
| 30 |
+
import torch
|
| 31 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 32 |
+
from PIL import Image
|
| 33 |
+
import cv2
|
| 34 |
+
|
| 35 |
+
def florence_caption_example():
|
| 36 |
+
"""Demonstration of how Florence-2 captions UI elements"""
|
| 37 |
+
|
| 38 |
+
# 1. Initialize model
|
| 39 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 40 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 41 |
+
"microsoft/Florence-2-large",
|
| 42 |
+
trust_remote_code=True
|
| 43 |
+
).to(device)
|
| 44 |
+
processor = AutoProcessor.from_pretrained(
|
| 45 |
+
"microsoft/Florence-2-large",
|
| 46 |
+
trust_remote_code=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# 2. Simulate detected UI elements (boxes from YOLO)
|
| 50 |
+
detected_boxes = [
|
| 51 |
+
(0.43, 0.51, 0.56, 0.58), # Select File button
|
| 52 |
+
(0.22, 0.34, 0.32, 0.36), # JPG Converter text
|
| 53 |
+
(0.15, 0.61, 0.45, 0.68), # Some icon/image element
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
# 3. Load screenshot
|
| 57 |
+
screenshot = Image.open("/workspaces/omoi/Screenshot.png")
|
| 58 |
+
width, height = screenshot.size
|
| 59 |
+
|
| 60 |
+
# 4. Process each element
|
| 61 |
+
captions = []
|
| 62 |
+
for box in detected_boxes:
|
| 63 |
+
# Crop the box region
|
| 64 |
+
x1_norm, y1_norm, x2_norm, y2_norm = box
|
| 65 |
+
x1 = int(x1_norm * width)
|
| 66 |
+
y1 = int(y1_norm * height)
|
| 67 |
+
x2 = int(x2_norm * width)
|
| 68 |
+
y2 = int(y2_norm * height)
|
| 69 |
+
|
| 70 |
+
cropped = screenshot.crop((x1, y1, x2, y2))
|
| 71 |
+
cropped = cropped.resize((64, 64)) # Normalize size
|
| 72 |
+
|
| 73 |
+
# Pass to Florence
|
| 74 |
+
prompt = "<CAPTION>" # Special Florence prompt
|
| 75 |
+
inputs = processor(
|
| 76 |
+
text=[prompt],
|
| 77 |
+
images=[cropped],
|
| 78 |
+
return_tensors="pt"
|
| 79 |
+
).to(device)
|
| 80 |
+
|
| 81 |
+
# Generate caption
|
| 82 |
+
with torch.no_grad():
|
| 83 |
+
generated_ids = model.generate(
|
| 84 |
+
input_ids=inputs["input_ids"],
|
| 85 |
+
pixel_values=inputs["pixel_values"],
|
| 86 |
+
max_new_tokens=20,
|
| 87 |
+
num_beams=1,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Decode result
|
| 91 |
+
caption = processor.batch_decode(
|
| 92 |
+
generated_ids,
|
| 93 |
+
skip_special_tokens=True
|
| 94 |
+
)[0]
|
| 95 |
+
|
| 96 |
+
captions.append(caption)
|
| 97 |
+
print(f"Box {box} -> Caption: '{caption}'")
|
| 98 |
+
|
| 99 |
+
return captions
|
| 100 |
+
|
| 101 |
+
# Expected output:
|
| 102 |
+
# Box (0.43, 0.51, 0.56, 0.58) -> Caption: 'Select File button'
|
| 103 |
+
# Box (0.22, 0.34, 0.32, 0.36) -> Caption: 'JPG Converter text'
|
| 104 |
+
# Box (0.15, 0.61, 0.45, 0.68) -> Caption: 'Image or icon element'
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
# EXAMPLE 3: How my OCR-only approach works (faster alternative)
|
| 108 |
+
# ================================================================
|
| 109 |
+
|
| 110 |
+
def ocr_text_fallback_example():
|
| 111 |
+
"""What I implemented instead - using OCR text"""
|
| 112 |
+
|
| 113 |
+
# Already have from PaddleOCR phase:
|
| 114 |
+
ocr_text = ["Select File", "JPG Converter", "Download link"]
|
| 115 |
+
ocr_bbox = [
|
| 116 |
+
(0.43, 0.51, 0.56, 0.58), # Matches first box!
|
| 117 |
+
(0.22, 0.34, 0.32, 0.36), # Matches second box!
|
| 118 |
+
(0.10, 0.60, 0.40, 0.67),
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
# Detected UI elements
|
| 122 |
+
detected_boxes = [
|
| 123 |
+
(0.43, 0.51, 0.56, 0.58), # Select File button
|
| 124 |
+
(0.22, 0.34, 0.32, 0.36), # JPG Converter text
|
| 125 |
+
(0.15, 0.61, 0.45, 0.68), # Some icon/image element
|
| 126 |
+
]
|
| 127 |
+
|
| 128 |
+
# Simple bbox intersection
|
| 129 |
+
labels = []
|
| 130 |
+
for ui_box in detected_boxes:
|
| 131 |
+
label = "Icon" # default
|
| 132 |
+
|
| 133 |
+
# Check if any OCR text overlaps with this UI element
|
| 134 |
+
for ocr_t, ocr_b in zip(ocr_text, ocr_bbox):
|
| 135 |
+
ui_x1, ui_y1, ui_x2, ui_y2 = ui_box
|
| 136 |
+
ocr_x1, ocr_y1, ocr_x2, ocr_y2 = ocr_b
|
| 137 |
+
|
| 138 |
+
# Check intersection
|
| 139 |
+
if (ui_x1 < ocr_x2 and ui_x2 > ocr_x1 and
|
| 140 |
+
ui_y1 < ocr_y2 and ui_y2 > ocr_y1):
|
| 141 |
+
label = ocr_t
|
| 142 |
+
break
|
| 143 |
+
|
| 144 |
+
labels.append(label)
|
| 145 |
+
print(f"Box {ui_box} -> Label: '{label}'")
|
| 146 |
+
|
| 147 |
+
return labels
|
| 148 |
+
|
| 149 |
+
# Output:
|
| 150 |
+
# Box (0.43, 0.51, 0.56, 0.58) -> Label: 'Select File'
|
| 151 |
+
# Box (0.22, 0.34, 0.32, 0.36) -> Label: 'JPG Converter text'
|
| 152 |
+
# Box (0.15, 0.61, 0.45, 0.68) -> Label: 'Icon' # Fallback, no OCR match
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# EXAMPLE 4: Comparison
|
| 156 |
+
# =====================
|
| 157 |
+
|
| 158 |
+
comparison = """
|
| 159 |
+
βββββββββββββββββββββββ¬βββββββββββββββββββββββ¬ββββββββββββββββββββββββ
|
| 160 |
+
β Method β OCR-only (Fast) β Florence (Semantic) β
|
| 161 |
+
βββββββββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββ€
|
| 162 |
+
β Speed β Instant (0.1s) β Slow (30s per batch) β
|
| 163 |
+
β Quality β Text-only labels β Semantic descriptions β
|
| 164 |
+
β Works on CPU? β YES β β NO (too slow) β β
|
| 165 |
+
β Icon without text β "Icon N" (fallback) β "Download button" β β
|
| 166 |
+
β Requires GPU? β NO β YES (recommended) β
|
| 167 |
+
β Model size β 0 (OCR built-in) β 14GB β
|
| 168 |
+
βββββββββββββββββββββββ΄βββββββββββββββββββββββ΄ββββββββββββββββββββββββ
|
| 169 |
+
|
| 170 |
+
For this demo:
|
| 171 |
+
β’ Screenshot size: 1365x767
|
| 172 |
+
β’ Detected elements: 120
|
| 173 |
+
β’ OCR approach: Complete in ~20 seconds total
|
| 174 |
+
β’ Florence approach: Would take ~15 minutes on CPU
|
| 175 |
+
"""
|
| 176 |
+
|
| 177 |
+
print(comparison)
|