| """ |
| Path Configuration Module |
| Handles all path resolution for the OmniParser system. |
| Works on any machine regardless of installation location. |
| """ |
|
|
| import os |
| from pathlib import Path |
| import sys |
|
|
| |
| def get_project_root(): |
| """ |
| Get the project root directory. |
| Works by finding the parent of OmniParser directory. |
| """ |
| |
| config_file = Path(__file__).resolve() |
| |
| |
| current_dir = config_file.parent |
| |
| |
| for _ in range(5): |
| if (current_dir / 'OmniParser').exists(): |
| return current_dir |
| if (current_dir / '.git').exists() and 'omoi' in str(current_dir).lower(): |
| return current_dir |
| current_dir = current_dir.parent |
| |
| |
| omniparser_dir = Path(__file__).parent.parent |
| if omniparser_dir.name == 'OmniParser': |
| return omniparser_dir.parent |
| |
| |
| return Path.cwd() |
|
|
| |
| PROJECT_ROOT = get_project_root() |
| OMNIPARSER_DIR = PROJECT_ROOT / 'OmniParser' |
| WEIGHTS_DIR = OMNIPARSER_DIR / 'weights' |
| ICON_DETECT_DIR = WEIGHTS_DIR / 'icon_detect' |
| ICON_CAPTION_DIR = WEIGHTS_DIR / 'icon_caption_florence' |
|
|
| |
| DATA_DIR = PROJECT_ROOT / 'data' |
| CROPPED_IMAGES_DIR = PROJECT_ROOT / 'cropped_images' |
| TEMP_CROP_DIR = Path('/tmp') / 'omoi_cropped_images' |
|
|
| |
| OUTPUT_DIR = PROJECT_ROOT / 'output' |
|
|
| |
| for directory in [DATA_DIR, CROPPED_IMAGES_DIR, OUTPUT_DIR, TEMP_CROP_DIR]: |
| directory.mkdir(parents=True, exist_ok=True) |
|
|
| |
|
|
| def get_icon_detect_model_path(): |
| """Get YOLOv8 icon detection model path""" |
| model_path = ICON_DETECT_DIR / 'model.pt' |
| if not model_path.exists(): |
| raise FileNotFoundError(f"Icon detect model not found at: {model_path}") |
| return str(model_path) |
|
|
| def get_icon_caption_model_path(): |
| """Get Florence-2 caption model path (if it exists)""" |
| model_path = ICON_CAPTION_DIR / 'model.safetensors' |
| if model_path.exists(): |
| return str(ICON_CAPTION_DIR) |
| return None |
|
|
| |
|
|
| def get_omniparser_config(): |
| """Get default OmniParser configuration""" |
| return { |
| 'som_model_path': get_icon_detect_model_path(), |
| 'caption_model_name': None, |
| 'caption_model_path': get_icon_caption_model_path(), |
| 'device': 'cpu', |
| 'BOX_TRESHOLD': 0.05, |
| 'save_cropped_images': True, |
| 'cropped_images_dir': str(TEMP_CROP_DIR) |
| } |
|
|
| |
|
|
| def resolve_path(path_str, base_dir=None): |
| """ |
| Resolve a path that could be relative or absolute. |
| |
| Args: |
| path_str: Path string (relative or absolute) |
| base_dir: Base directory for relative paths (defaults to PROJECT_ROOT) |
| |
| Returns: |
| Absolute path as string |
| """ |
| if base_dir is None: |
| base_dir = PROJECT_ROOT |
| |
| path = Path(path_str) |
| |
| |
| if path.is_absolute(): |
| return str(path) |
| |
| |
| resolved = (Path(base_dir) / path).resolve() |
| return str(resolved) |
|
|
| def get_screenshot_path(filename='Screenshot2.png'): |
| """Get path to a screenshot file""" |
| return str(DATA_DIR / filename) |
|
|
| def get_output_path(filename): |
| """Get path to an output file""" |
| return str(OUTPUT_DIR / filename) |
|
|
| |
|
|
| if __name__ == "__main__": |
| print("\n" + "="*70) |
| print("OmniParser Configuration") |
| print("="*70) |
| print(f"\nProject Root: {PROJECT_ROOT}") |
| print(f"OmniParser Dir: {OMNIPARSER_DIR}") |
| print(f"Weights Dir: {WEIGHTS_DIR}") |
| print(f"\n[Model Paths]") |
| print(f" Icon Detect: {get_icon_detect_model_path()}") |
| print(f" Caption Model: {get_icon_caption_model_path()}") |
| print(f"\n[Data Directories]") |
| print(f" Cropped Images: {CROPPED_IMAGES_DIR}") |
| print(f" Temp Crop: {TEMP_CROP_DIR}") |
| print(f" Output: {OUTPUT_DIR}") |
| print(f"\n[Configuration]") |
| config = get_omniparser_config() |
| for key, value in config.items(): |
| print(f" {key}: {value}") |
| print("\n" + "="*70 + "\n") |
|
|