Spaces:
Sleeping
Sleeping
File size: 2,886 Bytes
82c9f6d |
1 2 3 4 5 6 7 8 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
"""
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
|