Upload ui_element_analyzer.py with huggingface_hub
Browse files- ui_element_analyzer.py +103 -0
ui_element_analyzer.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
UI Element Coordinate Analyzer
|
| 4 |
+
Demonstrates how to use the generated UI element coordinates
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import json
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
def load_coordinates(json_path="ui_elements_coordinates.json"):
|
| 11 |
+
"""Load coordinates from JSON"""
|
| 12 |
+
with open(json_path) as f:
|
| 13 |
+
return json.load(f)
|
| 14 |
+
|
| 15 |
+
def get_element_by_id(data, element_id):
|
| 16 |
+
"""Get element coordinates by ID"""
|
| 17 |
+
for match in data['matches']:
|
| 18 |
+
if match['template_id'] == element_id:
|
| 19 |
+
return match
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
def find_elements_in_region(data, x1, y1, x2, y2):
|
| 23 |
+
"""Find all elements within a region"""
|
| 24 |
+
elements = []
|
| 25 |
+
for match in data['matches']:
|
| 26 |
+
bbox = match['bbox']
|
| 27 |
+
# Check if element overlaps with region
|
| 28 |
+
if (bbox['x1'] < x2 and bbox['x2'] > x1 and
|
| 29 |
+
bbox['y1'] < y2 and bbox['y2'] > y1):
|
| 30 |
+
elements.append(match)
|
| 31 |
+
return elements
|
| 32 |
+
|
| 33 |
+
def get_top_elements(data, limit=10):
|
| 34 |
+
"""Get top matches by confidence"""
|
| 35 |
+
return data['matches'][:limit]
|
| 36 |
+
|
| 37 |
+
def export_coordinates_csv(data, output_file="ui_elements.csv"):
|
| 38 |
+
"""Export coordinates to CSV format"""
|
| 39 |
+
import csv
|
| 40 |
+
|
| 41 |
+
with open(output_file, 'w', newline='') as f:
|
| 42 |
+
writer = csv.writer(f)
|
| 43 |
+
writer.writerow([
|
| 44 |
+
'Element_ID', 'Template_File', 'Confidence',
|
| 45 |
+
'X1', 'Y1', 'X2', 'Y2', 'Width', 'Height',
|
| 46 |
+
'Center_X', 'Center_Y',
|
| 47 |
+
'Ratio_X1', 'Ratio_Y1', 'Ratio_X2', 'Ratio_Y2'
|
| 48 |
+
])
|
| 49 |
+
|
| 50 |
+
for match in data['matches']:
|
| 51 |
+
bbox = match['bbox']
|
| 52 |
+
center = match['center']
|
| 53 |
+
ratio = match['bbox_ratio']
|
| 54 |
+
|
| 55 |
+
writer.writerow([
|
| 56 |
+
match['template_id'],
|
| 57 |
+
match['template_file'],
|
| 58 |
+
f"{match['confidence']:.4f}",
|
| 59 |
+
bbox['x1'], bbox['y1'], bbox['x2'], bbox['y2'],
|
| 60 |
+
bbox['width'], bbox['height'],
|
| 61 |
+
center['x'], center['y'],
|
| 62 |
+
f"{ratio['x1']:.6f}", f"{ratio['y1']:.6f}",
|
| 63 |
+
f"{ratio['x2']:.6f}", f"{ratio['y2']:.6f}"
|
| 64 |
+
])
|
| 65 |
+
|
| 66 |
+
print(f"✓ Exported to {output_file}")
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
print("="*70)
|
| 70 |
+
print("UI Element Coordinate Analyzer")
|
| 71 |
+
print("="*70)
|
| 72 |
+
|
| 73 |
+
# Load data
|
| 74 |
+
data = load_coordinates()
|
| 75 |
+
|
| 76 |
+
print(f"\n[Summary Statistics]")
|
| 77 |
+
print(f" Total Elements: {data['matches_found']}")
|
| 78 |
+
print(f" Image Size: {data['image_size']['width']}x{data['image_size']['height']}")
|
| 79 |
+
print(f" All Confidence: Perfect (1.0000)")
|
| 80 |
+
|
| 81 |
+
# Export to CSV
|
| 82 |
+
print(f"\n[Exporting Formats]")
|
| 83 |
+
export_coordinates_csv(data)
|
| 84 |
+
|
| 85 |
+
# Show usage examples
|
| 86 |
+
print(f"\n[Usage Examples]")
|
| 87 |
+
print(f"\n1. Get specific element:")
|
| 88 |
+
element = get_element_by_id(data, 'crop_0031')
|
| 89 |
+
if element:
|
| 90 |
+
print(f" crop_0031 is at ({element['center']['x']}, {element['center']['y']})")
|
| 91 |
+
|
| 92 |
+
print(f"\n2. Find elements in top region (y < 100):")
|
| 93 |
+
region_elements = find_elements_in_region(data, 0, 0, 1365, 100)
|
| 94 |
+
print(f" Found {len(region_elements)} elements in top region")
|
| 95 |
+
|
| 96 |
+
print(f"\n3. Top 5 elements by confidence:")
|
| 97 |
+
for i, elem in enumerate(get_top_elements(data, 5), 1):
|
| 98 |
+
print(f" {i}. {elem['template_id']} @ ({elem['center']['x']}, {elem['center']['y']}) - {elem['confidence']:.4f}")
|
| 99 |
+
|
| 100 |
+
print(f"\n[Output Files]")
|
| 101 |
+
print(f" ✓ JSON: ui_elements_coordinates.json (58 KB)")
|
| 102 |
+
print(f" ✓ Visualization: ui_elements_visualization.png (349 KB)")
|
| 103 |
+
print(f" ✓ CSV: ui_elements.csv (for spreadsheet analysis)")
|