Spaces:
Paused
Paused
Upload config.py with huggingface_hub
Browse files
config.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Path Configuration Module
|
| 3 |
+
Handles all path resolution for the OmniParser system.
|
| 4 |
+
Works on any machine regardless of installation location.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
# Get the project root dynamically
|
| 12 |
+
def get_project_root():
|
| 13 |
+
"""
|
| 14 |
+
Get the project root directory.
|
| 15 |
+
Works by finding the parent of OmniParser directory.
|
| 16 |
+
"""
|
| 17 |
+
# If this config is in project root, use that
|
| 18 |
+
config_file = Path(__file__).resolve()
|
| 19 |
+
|
| 20 |
+
# Look for OmniParser directory to determine project root
|
| 21 |
+
current_dir = config_file.parent
|
| 22 |
+
|
| 23 |
+
# Search up the directory tree for OmniParser or omoi-v2
|
| 24 |
+
for _ in range(5): # Search up to 5 levels
|
| 25 |
+
if (current_dir / 'OmniParser').exists():
|
| 26 |
+
return current_dir
|
| 27 |
+
if (current_dir / '.git').exists() and 'omoi' in str(current_dir).lower():
|
| 28 |
+
return current_dir
|
| 29 |
+
current_dir = current_dir.parent
|
| 30 |
+
|
| 31 |
+
# Fallback: assume omoi-v2 is in the parent of OmniParser
|
| 32 |
+
omniparser_dir = Path(__file__).parent.parent
|
| 33 |
+
if omniparser_dir.name == 'OmniParser':
|
| 34 |
+
return omniparser_dir.parent
|
| 35 |
+
|
| 36 |
+
# Last resort: return current working directory
|
| 37 |
+
return Path.cwd()
|
| 38 |
+
|
| 39 |
+
# Get all paths
|
| 40 |
+
PROJECT_ROOT = get_project_root()
|
| 41 |
+
OMNIPARSER_DIR = PROJECT_ROOT / 'OmniParser'
|
| 42 |
+
WEIGHTS_DIR = OMNIPARSER_DIR / 'weights'
|
| 43 |
+
ICON_DETECT_DIR = WEIGHTS_DIR / 'icon_detect'
|
| 44 |
+
ICON_CAPTION_DIR = WEIGHTS_DIR / 'icon_caption_florence'
|
| 45 |
+
|
| 46 |
+
# Data directories
|
| 47 |
+
DATA_DIR = PROJECT_ROOT / 'data'
|
| 48 |
+
CROPPED_IMAGES_DIR = PROJECT_ROOT / 'cropped_images'
|
| 49 |
+
TEMP_CROP_DIR = Path('/tmp') / 'omoi_cropped_images'
|
| 50 |
+
|
| 51 |
+
# Output directories
|
| 52 |
+
OUTPUT_DIR = PROJECT_ROOT / 'output'
|
| 53 |
+
|
| 54 |
+
# Create directories if they don't exist
|
| 55 |
+
for directory in [DATA_DIR, CROPPED_IMAGES_DIR, OUTPUT_DIR, TEMP_CROP_DIR]:
|
| 56 |
+
directory.mkdir(parents=True, exist_ok=True)
|
| 57 |
+
|
| 58 |
+
# ============ Model Paths ============
|
| 59 |
+
|
| 60 |
+
def get_icon_detect_model_path():
|
| 61 |
+
"""Get YOLOv8 icon detection model path"""
|
| 62 |
+
model_path = ICON_DETECT_DIR / 'model.pt'
|
| 63 |
+
if not model_path.exists():
|
| 64 |
+
raise FileNotFoundError(f"Icon detect model not found at: {model_path}")
|
| 65 |
+
return str(model_path)
|
| 66 |
+
|
| 67 |
+
def get_icon_caption_model_path():
|
| 68 |
+
"""Get Florence-2 caption model path (if it exists)"""
|
| 69 |
+
model_path = ICON_CAPTION_DIR / 'model.safetensors'
|
| 70 |
+
if model_path.exists():
|
| 71 |
+
return str(ICON_CAPTION_DIR)
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
# ============ Configuration ============
|
| 75 |
+
|
| 76 |
+
def get_omniparser_config():
|
| 77 |
+
"""Get default OmniParser configuration"""
|
| 78 |
+
return {
|
| 79 |
+
'som_model_path': get_icon_detect_model_path(),
|
| 80 |
+
'caption_model_name': None, # Florence removed
|
| 81 |
+
'caption_model_path': get_icon_caption_model_path(),
|
| 82 |
+
'device': 'cpu',
|
| 83 |
+
'BOX_TRESHOLD': 0.05,
|
| 84 |
+
'save_cropped_images': True,
|
| 85 |
+
'cropped_images_dir': str(TEMP_CROP_DIR)
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
# ============ Path Resolution Functions ============
|
| 89 |
+
|
| 90 |
+
def resolve_path(path_str, base_dir=None):
|
| 91 |
+
"""
|
| 92 |
+
Resolve a path that could be relative or absolute.
|
| 93 |
+
|
| 94 |
+
Args:
|
| 95 |
+
path_str: Path string (relative or absolute)
|
| 96 |
+
base_dir: Base directory for relative paths (defaults to PROJECT_ROOT)
|
| 97 |
+
|
| 98 |
+
Returns:
|
| 99 |
+
Absolute path as string
|
| 100 |
+
"""
|
| 101 |
+
if base_dir is None:
|
| 102 |
+
base_dir = PROJECT_ROOT
|
| 103 |
+
|
| 104 |
+
path = Path(path_str)
|
| 105 |
+
|
| 106 |
+
# If already absolute, return as is
|
| 107 |
+
if path.is_absolute():
|
| 108 |
+
return str(path)
|
| 109 |
+
|
| 110 |
+
# Relative path: resolve from base directory
|
| 111 |
+
resolved = (Path(base_dir) / path).resolve()
|
| 112 |
+
return str(resolved)
|
| 113 |
+
|
| 114 |
+
def get_screenshot_path(filename='Screenshot2.png'):
|
| 115 |
+
"""Get path to a screenshot file"""
|
| 116 |
+
return str(DATA_DIR / filename)
|
| 117 |
+
|
| 118 |
+
def get_output_path(filename):
|
| 119 |
+
"""Get path to an output file"""
|
| 120 |
+
return str(OUTPUT_DIR / filename)
|
| 121 |
+
|
| 122 |
+
# ============ Print Configuration for Debugging ============
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
print("\n" + "="*70)
|
| 126 |
+
print("OmniParser Configuration")
|
| 127 |
+
print("="*70)
|
| 128 |
+
print(f"\nProject Root: {PROJECT_ROOT}")
|
| 129 |
+
print(f"OmniParser Dir: {OMNIPARSER_DIR}")
|
| 130 |
+
print(f"Weights Dir: {WEIGHTS_DIR}")
|
| 131 |
+
print(f"\n[Model Paths]")
|
| 132 |
+
print(f" Icon Detect: {get_icon_detect_model_path()}")
|
| 133 |
+
print(f" Caption Model: {get_icon_caption_model_path()}")
|
| 134 |
+
print(f"\n[Data Directories]")
|
| 135 |
+
print(f" Cropped Images: {CROPPED_IMAGES_DIR}")
|
| 136 |
+
print(f" Temp Crop: {TEMP_CROP_DIR}")
|
| 137 |
+
print(f" Output: {OUTPUT_DIR}")
|
| 138 |
+
print(f"\n[Configuration]")
|
| 139 |
+
config = get_omniparser_config()
|
| 140 |
+
for key, value in config.items():
|
| 141 |
+
print(f" {key}: {value}")
|
| 142 |
+
print("\n" + "="*70 + "\n")
|