Spaces:
Sleeping
Sleeping
| """ | |
| Module for handling example data management and processing. | |
| Provides functionality to load and organize landmark examples. | |
| """ | |
| import json | |
| import os | |
| from collections import OrderedDict | |
| # Define the desired category order | |
| CATEGORY_ORDER = [ | |
| "Historical Sites & Monuments", | |
| "Modern Architecture & Urban Landmarks", | |
| "National Parks & Natural Wonders", | |
| "Bridges", | |
| "Other" | |
| ] | |
| def load_categories(): | |
| """Load landmark categories from JSON file in specified order.""" | |
| with open('landmark_categories.json') as f: | |
| categories = json.load(f) | |
| # Create an OrderedDict with categories in the specified order | |
| ordered_categories = OrderedDict() | |
| for category in CATEGORY_ORDER: | |
| if category in categories: | |
| ordered_categories[category] = categories[category] | |
| return ordered_categories | |
| def create_example_data(categories): | |
| """ | |
| Create organized example data for the interface. | |
| Args: | |
| categories (dict): Dictionary of landmark categories and their landmarks | |
| Returns: | |
| dict: Examples organized by category with format {category: [(image_path, label_info)]} | |
| Raises: | |
| ValueError: If categories is empty or not a dictionary | |
| """ | |
| if not isinstance(categories, dict) or not categories: | |
| raise ValueError("Categories must be a non-empty dictionary") | |
| category_examples = {} | |
| def generate_filename_variations(landmark_name): | |
| """ | |
| Generate possible filename variations for landmark images. | |
| Handles different naming conventions (spaces, underscores, etc.) | |
| """ | |
| base = landmark_name.lower().strip() | |
| return [ | |
| base.replace(' ', '_'), # underscore_format | |
| base.replace(' ', '-'), # dash-format | |
| base.replace(' ', ''), # nocapsnospacesformat | |
| '_'.join(base.split()) # underscore_all_words | |
| ] | |
| # Process each category and its landmarks | |
| for category, landmarks in categories.items(): | |
| category_examples[category] = [] | |
| # Find matching image files for each landmark | |
| for landmark in landmarks: | |
| if not isinstance(landmark, str): | |
| continue | |
| # Try different filename variations until a match is found | |
| for filename in generate_filename_variations(landmark): | |
| image_path = os.path.join("examples", f"{filename}.jpg") | |
| if os.path.exists(image_path): | |
| # Create label info dictionary for the example | |
| label_info = { | |
| "label": landmark, | |
| "confidences": [(landmark, 1.0)] | |
| } | |
| category_examples[category].append([image_path, label_info]) | |
| break | |
| return category_examples | |