Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| UI Element Coordinate Analyzer | |
| Demonstrates how to use the generated UI element coordinates | |
| """ | |
| import json | |
| import sys | |
| def load_coordinates(json_path="ui_elements_coordinates.json"): | |
| """Load coordinates from JSON""" | |
| with open(json_path) as f: | |
| return json.load(f) | |
| def get_element_by_id(data, element_id): | |
| """Get element coordinates by ID""" | |
| for match in data['matches']: | |
| if match['template_id'] == element_id: | |
| return match | |
| return None | |
| def find_elements_in_region(data, x1, y1, x2, y2): | |
| """Find all elements within a region""" | |
| elements = [] | |
| for match in data['matches']: | |
| bbox = match['bbox'] | |
| # Check if element overlaps with region | |
| if (bbox['x1'] < x2 and bbox['x2'] > x1 and | |
| bbox['y1'] < y2 and bbox['y2'] > y1): | |
| elements.append(match) | |
| return elements | |
| def get_top_elements(data, limit=10): | |
| """Get top matches by confidence""" | |
| return data['matches'][:limit] | |
| def export_coordinates_csv(data, output_file="ui_elements.csv"): | |
| """Export coordinates to CSV format""" | |
| import csv | |
| with open(output_file, 'w', newline='') as f: | |
| writer = csv.writer(f) | |
| writer.writerow([ | |
| 'Element_ID', 'Template_File', 'Confidence', | |
| 'X1', 'Y1', 'X2', 'Y2', 'Width', 'Height', | |
| 'Center_X', 'Center_Y', | |
| 'Ratio_X1', 'Ratio_Y1', 'Ratio_X2', 'Ratio_Y2' | |
| ]) | |
| for match in data['matches']: | |
| bbox = match['bbox'] | |
| center = match['center'] | |
| ratio = match['bbox_ratio'] | |
| writer.writerow([ | |
| match['template_id'], | |
| match['template_file'], | |
| f"{match['confidence']:.4f}", | |
| bbox['x1'], bbox['y1'], bbox['x2'], bbox['y2'], | |
| bbox['width'], bbox['height'], | |
| center['x'], center['y'], | |
| f"{ratio['x1']:.6f}", f"{ratio['y1']:.6f}", | |
| f"{ratio['x2']:.6f}", f"{ratio['y2']:.6f}" | |
| ]) | |
| print(f"β Exported to {output_file}") | |
| if __name__ == "__main__": | |
| print("="*70) | |
| print("UI Element Coordinate Analyzer") | |
| print("="*70) | |
| # Load data | |
| data = load_coordinates() | |
| print(f"\n[Summary Statistics]") | |
| print(f" Total Elements: {data['matches_found']}") | |
| print(f" Image Size: {data['image_size']['width']}x{data['image_size']['height']}") | |
| print(f" All Confidence: Perfect (1.0000)") | |
| # Export to CSV | |
| print(f"\n[Exporting Formats]") | |
| export_coordinates_csv(data) | |
| # Show usage examples | |
| print(f"\n[Usage Examples]") | |
| print(f"\n1. Get specific element:") | |
| element = get_element_by_id(data, 'crop_0031') | |
| if element: | |
| print(f" crop_0031 is at ({element['center']['x']}, {element['center']['y']})") | |
| print(f"\n2. Find elements in top region (y < 100):") | |
| region_elements = find_elements_in_region(data, 0, 0, 1365, 100) | |
| print(f" Found {len(region_elements)} elements in top region") | |
| print(f"\n3. Top 5 elements by confidence:") | |
| for i, elem in enumerate(get_top_elements(data, 5), 1): | |
| print(f" {i}. {elem['template_id']} @ ({elem['center']['x']}, {elem['center']['y']}) - {elem['confidence']:.4f}") | |
| print(f"\n[Output Files]") | |
| print(f" β JSON: ui_elements_coordinates.json (58 KB)") | |
| print(f" β Visualization: ui_elements_visualization.png (349 KB)") | |
| print(f" β CSV: ui_elements.csv (for spreadsheet analysis)") | |