Spaces:
Sleeping
Sleeping
Update app/services/predictor.py
Browse files- app/services/predictor.py +20 -9
app/services/predictor.py
CHANGED
|
@@ -1,34 +1,45 @@
|
|
| 1 |
import numpy as np
|
| 2 |
from typing import Tuple
|
|
|
|
| 3 |
from app.configs import get_classification_model, model_classes
|
| 4 |
from app.storage import save_image
|
| 5 |
from app.core.preprocessing import preprocess_image
|
| 6 |
from app.core.validation import is_valid_image
|
| 7 |
|
| 8 |
def predict_image(file_bytes: bytes, filename: str) -> Tuple[str, float, str]:
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
if not is_valid_image(file_bytes):
|
| 11 |
raise ValueError("Invalid image")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
model = get_classification_model()
|
| 15 |
|
| 16 |
if model is None:
|
| 17 |
raise ValueError("Classification model not loaded")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
image_path = save_image(file_bytes, image_filename)
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
confidence = float(np.max(predictions))
|
| 28 |
-
predicted_class = int(np.argmax(predictions))
|
| 29 |
|
| 30 |
prediction = model_classes.get(predicted_class, "unknown")
|
| 31 |
|
| 32 |
-
print(f"CLASSIFICATION: {prediction} ({confidence:.3f})")
|
| 33 |
|
| 34 |
return prediction, confidence, image_path
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from typing import Tuple
|
| 3 |
+
import tensorflow as tf
|
| 4 |
from app.configs import get_classification_model, model_classes
|
| 5 |
from app.storage import save_image
|
| 6 |
from app.core.preprocessing import preprocess_image
|
| 7 |
from app.core.validation import is_valid_image
|
| 8 |
|
| 9 |
def predict_image(file_bytes: bytes, filename: str) -> Tuple[str, float, str]:
|
| 10 |
+
"""Predict image using classification model"""
|
| 11 |
+
|
| 12 |
+
# Check if image is valid
|
| 13 |
if not is_valid_image(file_bytes):
|
| 14 |
raise ValueError("Invalid image")
|
| 15 |
|
| 16 |
+
# Get model
|
| 17 |
model = get_classification_model()
|
| 18 |
|
| 19 |
if model is None:
|
| 20 |
raise ValueError("Classification model not loaded")
|
| 21 |
|
| 22 |
+
# Save image first
|
| 23 |
+
import hashlib
|
| 24 |
+
image_filename = f"{hashlib.md5(filename.encode()).hexdigest()[:12]}.jpg"
|
| 25 |
image_path = save_image(file_bytes, image_filename)
|
| 26 |
|
| 27 |
+
# Preprocess
|
| 28 |
+
img_array = preprocess_image(file_bytes, target_size=(224, 224))
|
| 29 |
|
| 30 |
+
# 🔥 Use SavedModel signature
|
| 31 |
+
result = model(tf.constant(img_array))
|
| 32 |
+
|
| 33 |
+
if isinstance(result, dict):
|
| 34 |
+
predictions = list(result.values())[0].numpy()
|
| 35 |
+
else:
|
| 36 |
+
predictions = result.numpy()
|
| 37 |
|
| 38 |
+
confidence = float(np.max(predictions[0]))
|
| 39 |
+
predicted_class = int(np.argmax(predictions[0]))
|
| 40 |
|
| 41 |
prediction = model_classes.get(predicted_class, "unknown")
|
| 42 |
|
| 43 |
+
print(f"🔍 CLASSIFICATION: {prediction} ({confidence:.3f})")
|
| 44 |
|
| 45 |
return prediction, confidence, image_path
|