Spaces:
Sleeping
Sleeping
Update app/services/segmenter.py
Browse files- app/services/segmenter.py +46 -80
app/services/segmenter.py
CHANGED
|
@@ -1,88 +1,54 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
-
from datetime import datetime
|
| 3 |
-
from typing import Dict, Any
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io
|
| 6 |
import tensorflow as tf
|
|
|
|
| 7 |
|
| 8 |
-
from app.configs import get_segmentation_model
|
| 9 |
-
from app.storage import update_history
|
| 10 |
from app.core.preprocessing import preprocess_image
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"timestamp": datetime.now().isoformat(),
|
| 21 |
-
"detections": []
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
# Get model
|
| 25 |
model = get_segmentation_model()
|
| 26 |
-
|
| 27 |
if model is None:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Save to segments directory
|
| 65 |
-
seg_filename = f"{request_id}_mask.png"
|
| 66 |
-
seg_path = str(SEGMENTS_DIR / seg_filename)
|
| 67 |
-
mask_image.save(seg_path, format='PNG')
|
| 68 |
-
|
| 69 |
-
seg_result.update({
|
| 70 |
-
"model_used": True,
|
| 71 |
-
"masks_shape": list(masks.shape),
|
| 72 |
-
"max_confidence": float(np.max(masks)),
|
| 73 |
-
"mask_path": seg_path
|
| 74 |
-
})
|
| 75 |
-
|
| 76 |
-
print(f"β
SEGMENTATION DONE {request_id}: {seg_path}")
|
| 77 |
-
|
| 78 |
-
except Exception as e:
|
| 79 |
-
seg_result["error"] = str(e)
|
| 80 |
-
seg_path = None
|
| 81 |
-
print(f"β SEGMENTATION FAILED {request_id}: {e}")
|
| 82 |
-
|
| 83 |
-
# Update history
|
| 84 |
-
update_history(
|
| 85 |
-
request_id,
|
| 86 |
-
segmentation_path=seg_path,
|
| 87 |
-
status="segmented" if seg_path else "segmentation_failed"
|
| 88 |
-
)
|
|
|
|
| 1 |
+
# app/services/segmenter.py
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
|
| 6 |
+
from app.configs import get_segmentation_model
|
|
|
|
| 7 |
from app.core.preprocessing import preprocess_image
|
| 8 |
|
| 9 |
|
| 10 |
+
def segment_image(image_bytes: bytes) -> Dict[str, Any]:
|
| 11 |
+
"""
|
| 12 |
+
Run segmentation on image bytes.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
Dict with keys: status, masks_shape, max_confidence, error (optional)
|
| 16 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
model = get_segmentation_model()
|
| 18 |
+
|
| 19 |
if model is None:
|
| 20 |
+
return {
|
| 21 |
+
"status": "failed",
|
| 22 |
+
"error": "Segmentation model not loaded",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Preprocess for segmentation (256x256)
|
| 27 |
+
img_array = preprocess_image(image_bytes, target_size=(256, 256))
|
| 28 |
+
|
| 29 |
+
# π₯ Call SavedModel signature
|
| 30 |
+
result = model(tf.constant(img_array))
|
| 31 |
+
|
| 32 |
+
if isinstance(result, dict):
|
| 33 |
+
masks = list(result.values())[0].numpy()
|
| 34 |
+
else:
|
| 35 |
+
masks = result.numpy()
|
| 36 |
+
|
| 37 |
+
# Extract stats
|
| 38 |
+
max_confidence = float(np.max(masks))
|
| 39 |
+
masks_shape = list(masks.shape)
|
| 40 |
+
|
| 41 |
+
print(f"π¨ SEGMENTATION: shape={masks_shape}, max_conf={max_confidence:.3f}")
|
| 42 |
+
|
| 43 |
+
return {
|
| 44 |
+
"status": "completed",
|
| 45 |
+
"masks_shape": masks_shape,
|
| 46 |
+
"max_confidence": max_confidence,
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"β SEGMENTATION FAILED: {e}")
|
| 51 |
+
return {
|
| 52 |
+
"status": "failed",
|
| 53 |
+
"error": str(e),
|
| 54 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|