Predict class for new image
(self, img)
| 80 | } |
| 81 | |
| 82 | def predict(self, img): |
| 83 | """Predict class for new image""" |
| 84 | if self.model is None: |
| 85 | raise ValueError("Model not trained. Please train the model first.") |
| 86 | |
| 87 | processed_img = self.preprocess_image(img) |
| 88 | features = self.extract_features(processed_img) |
| 89 | |
| 90 | # Concatenate features similar to training |
| 91 | feature_vector = np.concatenate([ |
| 92 | features['hog'].flatten(), |
| 93 | features['color'].flatten(), |
| 94 | features['sift'].flatten() if features['sift'] is not None else np.zeros(128) |
| 95 | ]) |
| 96 | |
| 97 | prediction = self.model.predict([feature_vector])[0] |
| 98 | confidence = np.max(self.model.predict_proba([feature_vector])) |
| 99 | |
| 100 | return { |
| 101 | 'prediction': prediction, |
| 102 | 'confidence': confidence |
| 103 | } |
| 104 | |
| 105 | def save_model(self, path): |
| 106 | """Save trained model""" |
no test coverage detected