| 9 | import joblib |
| 10 | |
| 11 | class AutoVision: |
| 12 | def __init__(self, model_path=None): |
| 13 | """Initialize AutoVision with optional pre-trained model""" |
| 14 | self.model = None |
| 15 | self.feature_extractors = {} |
| 16 | self.classes = [] |
| 17 | |
| 18 | # Get absolute path for model files |
| 19 | if model_path: |
| 20 | self.model_path = self._get_resource_path(model_path) |
| 21 | self.load_model(self.model_path) |
| 22 | |
| 23 | def _get_resource_path(self, relative_path): |
| 24 | """Get absolute path to resource, works for dev and PyInstaller""" |
| 25 | base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) |
| 26 | return os.path.join(base_path, relative_path) |
| 27 | |
| 28 | def load_image(self, img_path): |
| 29 | """Load and validate image""" |
| 30 | img = cv2.imread(img_path) |
| 31 | if img is None: |
| 32 | raise ValueError(f"Failed to load image from {img_path}") |
| 33 | return img |
| 34 | |
| 35 | def preprocess_image(self, img, target_size=(224, 224)): |
| 36 | """Enhanced preprocessing pipeline""" |
| 37 | from .image_preprocessing import preprocess_pipeline |
| 38 | return preprocess_pipeline(img, target_size) |
| 39 | |
| 40 | def detect_objects(self, img): |
| 41 | """Enhanced object detection with confidence scores""" |
| 42 | from .object_detection import detect_objects |
| 43 | return detect_objects(img) |
| 44 | |
| 45 | def extract_features(self, img): |
| 46 | """Extract comprehensive feature set""" |
| 47 | from .feature_extraction import extract_features |
| 48 | return extract_features(img) |
| 49 | |
| 50 | def train(self, images, labels, test_size=0.2): |
| 51 | """Train the model with extracted features""" |
| 52 | features = [] |
| 53 | for img in images: |
| 54 | processed_img = self.preprocess_image(img) |
| 55 | img_features = self.extract_features(processed_img) |
| 56 | # Concatenate all feature types |
| 57 | feature_vector = np.concatenate([ |
| 58 | img_features['hog'].flatten(), |
| 59 | img_features['color'].flatten(), |
| 60 | img_features['sift'].flatten() if img_features['sift'] is not None else np.zeros(128) |
| 61 | ]) |
| 62 | features.append(feature_vector) |
| 63 | |
| 64 | X_train, X_test, y_train, y_test = train_test_split( |
| 65 | features, labels, test_size=test_size, random_state=42 |
| 66 | ) |
| 67 | |
| 68 | self.model = RandomForestClassifier(n_estimators=100, random_state=42) |