Spaces:
Running
Running
Commit
·
d42f049
1
Parent(s):
c4cfc0a
update
Browse files- packages.txt +15 -1
- screencoder/image_box_detection.py +172 -61
packages.txt
CHANGED
|
@@ -1,2 +1,16 @@
|
|
| 1 |
libvips-dev
|
| 2 |
-
libpangocairo-1.0-0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
libvips-dev
|
| 2 |
+
libpangocairo-1.0-0
|
| 3 |
+
libnss3
|
| 4 |
+
libatk-bridge2.0-0
|
| 5 |
+
libdrm2
|
| 6 |
+
libxkbcommon0
|
| 7 |
+
libxcomposite1
|
| 8 |
+
libxdamage1
|
| 9 |
+
libxrandr2
|
| 10 |
+
libgbm1
|
| 11 |
+
libasound2
|
| 12 |
+
libpango-1.0-0
|
| 13 |
+
libcairo2
|
| 14 |
+
libatspi2.0-0
|
| 15 |
+
libgtk-3-0
|
| 16 |
+
libgdk-pixbuf2.0-0
|
screencoder/image_box_detection.py
CHANGED
|
@@ -2,71 +2,174 @@ import argparse, asyncio, cv2, json, os, sys
|
|
| 2 |
from pathlib import Path
|
| 3 |
import numpy as np
|
| 4 |
from playwright.async_api import async_playwright
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# ---------- Main logic ----------
|
| 7 |
async def extract_bboxes_from_html(html_path: Path):
|
| 8 |
async with async_playwright() as p:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
}
|
| 46 |
-
|
| 47 |
-
if (containing_region_id) {
|
| 48 |
-
placeholder_bboxes.push({
|
| 49 |
-
id: 'ph' + ph_id_counter++,
|
| 50 |
-
x: el_rect.x,
|
| 51 |
-
y: el_rect.y,
|
| 52 |
-
w: el_rect.width,
|
| 53 |
-
h: el_rect.height,
|
| 54 |
-
region_id: containing_region_id
|
| 55 |
-
});
|
| 56 |
-
}
|
| 57 |
-
}
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
def draw_bboxes_on_image(img, region_bboxes, placeholder_bboxes):
|
|
@@ -142,9 +245,17 @@ def main():
|
|
| 142 |
H, W = img.shape[:2]
|
| 143 |
|
| 144 |
# Parse HTML → Get bboxes
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
if not placeholder_bboxes:
|
| 149 |
# This is not necessarily an error; some UIs might not have placeholders.
|
| 150 |
print("Info: No gray placeholder blocks found.")
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
import numpy as np
|
| 4 |
from playwright.async_api import async_playwright
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
|
| 7 |
+
# ---------- Fallback HTML parsing method ----------
|
| 8 |
+
def extract_bboxes_from_html_fallback(html_path: Path):
|
| 9 |
+
"""
|
| 10 |
+
Fallback method to extract bboxes from HTML without using Playwright.
|
| 11 |
+
This is a simplified version that may not be as accurate but will allow the pipeline to continue.
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
with open(html_path, 'r', encoding='utf-8') as f:
|
| 15 |
+
html_content = f.read()
|
| 16 |
+
|
| 17 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 18 |
+
|
| 19 |
+
# Extract region bboxes from CSS styles
|
| 20 |
+
region_bboxes = []
|
| 21 |
+
region_containers = soup.find_all('div', class_='box')
|
| 22 |
+
for i, container in enumerate(region_containers):
|
| 23 |
+
container_id = container.get('id', f'region_{i}')
|
| 24 |
+
style = container.get('style', '')
|
| 25 |
+
|
| 26 |
+
# Parse CSS style to extract position and size
|
| 27 |
+
# This is a simplified parser - in real scenarios, you might need a more robust CSS parser
|
| 28 |
+
left = 0
|
| 29 |
+
top = 0
|
| 30 |
+
width = 100
|
| 31 |
+
height = 100
|
| 32 |
+
|
| 33 |
+
if 'left:' in style:
|
| 34 |
+
left_str = style.split('left:')[1].split('%')[0].strip()
|
| 35 |
+
left = float(left_str)
|
| 36 |
+
if 'top:' in style:
|
| 37 |
+
top_str = style.split('top:')[1].split('%')[0].strip()
|
| 38 |
+
top = float(top_str)
|
| 39 |
+
if 'width:' in style:
|
| 40 |
+
width_str = style.split('width:')[1].split('%')[0].strip()
|
| 41 |
+
width = float(width_str)
|
| 42 |
+
if 'height:' in style:
|
| 43 |
+
height_str = style.split('height:')[1].split('%')[0].strip()
|
| 44 |
+
height = float(height_str)
|
| 45 |
+
|
| 46 |
+
# Convert percentage to pixels (assuming 1280x720 viewport)
|
| 47 |
+
x = int(left * 12.8) # 1280 / 100
|
| 48 |
+
y = int(top * 7.2) # 720 / 100
|
| 49 |
+
w = int(width * 12.8)
|
| 50 |
+
h = int(height * 7.2)
|
| 51 |
+
|
| 52 |
+
region_bboxes.append({
|
| 53 |
+
'id': container_id,
|
| 54 |
+
'x': x,
|
| 55 |
+
'y': y,
|
| 56 |
+
'w': w,
|
| 57 |
+
'h': h
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
# Extract placeholder bboxes
|
| 61 |
+
placeholder_bboxes = []
|
| 62 |
+
placeholder_images = soup.find_all('img', src='placeholder.png')
|
| 63 |
+
for i, img in enumerate(placeholder_images):
|
| 64 |
+
# For fallback, we'll use a simple approach
|
| 65 |
+
# In a real scenario, you'd need to parse the actual layout
|
| 66 |
+
placeholder_bboxes.append({
|
| 67 |
+
'id': f'ph{i}',
|
| 68 |
+
'x': 100 + i * 50, # Simple positioning
|
| 69 |
+
'y': 100 + i * 50,
|
| 70 |
+
'w': 100,
|
| 71 |
+
'h': 100,
|
| 72 |
+
'region_id': region_bboxes[0]['id'] if region_bboxes else '1'
|
| 73 |
+
})
|
| 74 |
+
|
| 75 |
+
return region_bboxes, placeholder_bboxes, 1280, 720
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"Error in fallback HTML parsing: {e}")
|
| 79 |
+
return [], [], 1280, 720
|
| 80 |
|
| 81 |
# ---------- Main logic ----------
|
| 82 |
async def extract_bboxes_from_html(html_path: Path):
|
| 83 |
async with async_playwright() as p:
|
| 84 |
+
try:
|
| 85 |
+
# Try to launch browser with headless mode for HF Spaces compatibility
|
| 86 |
+
browser = await p.chromium.launch(headless=True)
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"Error launching browser: {e}")
|
| 89 |
+
print("Attempting to install browser dependencies...")
|
| 90 |
+
try:
|
| 91 |
+
# Try to install browser dependencies
|
| 92 |
+
import subprocess
|
| 93 |
+
result = subprocess.run(["playwright", "install", "chromium"],
|
| 94 |
+
capture_output=True, text=True, timeout=300)
|
| 95 |
+
if result.returncode == 0:
|
| 96 |
+
print("Browser dependencies installed successfully, retrying...")
|
| 97 |
+
browser = await p.chromium.launch(headless=True)
|
| 98 |
+
else:
|
| 99 |
+
print(f"Failed to install browser dependencies: {result.stderr}")
|
| 100 |
+
# Return empty results to continue the pipeline
|
| 101 |
+
return [], [], 1280, 720
|
| 102 |
+
except Exception as install_error:
|
| 103 |
+
print(f"Failed to install browser dependencies: {install_error}")
|
| 104 |
+
# Return empty results to continue the pipeline
|
| 105 |
+
return [], [], 1280, 720
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
ctx = await browser.new_context(
|
| 109 |
+
viewport={"width": 1280, "height": 720},
|
| 110 |
+
)
|
| 111 |
+
page = await ctx.new_page()
|
| 112 |
+
await page.goto(html_path.resolve().as_uri())
|
| 113 |
+
|
| 114 |
+
metrics = await page.evaluate("""
|
| 115 |
+
() => {
|
| 116 |
+
const region_containers = Array.from(document.querySelectorAll('.box[id]'));
|
| 117 |
+
const region_bboxes = region_containers.map(el => {
|
| 118 |
+
const rect = el.getBoundingClientRect();
|
| 119 |
+
return { id: el.id, x: rect.x, y: rect.y, w: rect.width, h: rect.height };
|
| 120 |
+
});
|
| 121 |
+
|
| 122 |
+
const placeholder_bboxes = [];
|
| 123 |
+
let ph_id_counter = 0;
|
| 124 |
+
const all_potential_placeholders = document.querySelectorAll('img[src="placeholder.png"]');
|
| 125 |
+
|
| 126 |
+
for (const el of all_potential_placeholders) {
|
| 127 |
+
// Apply the same filters as before
|
| 128 |
+
if (el.tagName === 'SVG') continue;
|
| 129 |
+
if (el.innerText && el.innerText.trim() !== '') continue;
|
| 130 |
+
|
| 131 |
+
const el_rect = el.getBoundingClientRect();
|
| 132 |
+
const el_center = { x: el_rect.left + el_rect.width / 2, y: el_rect.top + el_rect.height / 2 };
|
| 133 |
+
|
| 134 |
+
// Find which region this placeholder is inside
|
| 135 |
+
let containing_region_id = null;
|
| 136 |
+
for (const region_el of region_containers) {
|
| 137 |
+
const region_rect = region_el.getBoundingClientRect();
|
| 138 |
+
if (el_center.x >= region_rect.left && el_center.x <= region_rect.right &&
|
| 139 |
+
el_center.y >= region_rect.top && el_center.y <= region_rect.bottom) {
|
| 140 |
+
containing_region_id = region_el.id;
|
| 141 |
+
break; // Assume non-overlapping regions
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
if (containing_region_id) {
|
| 146 |
+
placeholder_bboxes.push({
|
| 147 |
+
id: 'ph' + ph_id_counter++,
|
| 148 |
+
x: el_rect.x,
|
| 149 |
+
y: el_rect.y,
|
| 150 |
+
w: el_rect.width,
|
| 151 |
+
h: el_rect.height,
|
| 152 |
+
region_id: containing_region_id
|
| 153 |
+
});
|
| 154 |
}
|
| 155 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
const layout_rect = document.documentElement.getBoundingClientRect();
|
| 158 |
+
return {
|
| 159 |
+
region_bboxes,
|
| 160 |
+
placeholder_bboxes,
|
| 161 |
+
layout_width: layout_rect.width,
|
| 162 |
+
layout_height: layout_rect.height
|
| 163 |
+
};
|
| 164 |
+
}
|
| 165 |
+
""")
|
| 166 |
+
await browser.close()
|
| 167 |
+
return metrics['region_bboxes'], metrics['placeholder_bboxes'], metrics['layout_width'], metrics['layout_height']
|
| 168 |
+
except Exception as e:
|
| 169 |
+
print(f"Error during browser operation: {e}")
|
| 170 |
+
await browser.close()
|
| 171 |
+
# Return empty results to continue the pipeline
|
| 172 |
+
return [], [], 1280, 720
|
| 173 |
|
| 174 |
|
| 175 |
def draw_bboxes_on_image(img, region_bboxes, placeholder_bboxes):
|
|
|
|
| 245 |
H, W = img.shape[:2]
|
| 246 |
|
| 247 |
# Parse HTML → Get bboxes
|
| 248 |
+
try:
|
| 249 |
+
region_bboxes, placeholder_bboxes, layout_width, layout_height = asyncio.run(
|
| 250 |
+
extract_bboxes_from_html(html_path)
|
| 251 |
+
)
|
| 252 |
+
print("Successfully extracted bboxes using Playwright")
|
| 253 |
+
except Exception as e:
|
| 254 |
+
print(f"Playwright failed: {e}")
|
| 255 |
+
print("Falling back to HTML parsing method...")
|
| 256 |
+
region_bboxes, placeholder_bboxes, layout_width, layout_height = extract_bboxes_from_html_fallback(html_path)
|
| 257 |
+
print("Successfully extracted bboxes using fallback method")
|
| 258 |
+
|
| 259 |
if not placeholder_bboxes:
|
| 260 |
# This is not necessarily an error; some UIs might not have placeholders.
|
| 261 |
print("Info: No gray placeholder blocks found.")
|