Spaces:
Sleeping
Sleeping
Delete main.py
Browse files
main.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 1 |
-
import io
|
| 2 |
-
import base64
|
| 3 |
-
import os
|
| 4 |
-
import tempfile
|
| 5 |
-
import numpy as np
|
| 6 |
-
import cv2
|
| 7 |
-
import nibabel as nib
|
| 8 |
-
from PIL import Image
|
| 9 |
-
from fastapi import FastAPI, UploadFile, File
|
| 10 |
-
from contextlib import asynccontextmanager
|
| 11 |
-
import tensorflow as tf
|
| 12 |
-
|
| 13 |
-
model = None
|
| 14 |
-
|
| 15 |
-
@asynccontextmanager
|
| 16 |
-
async def lifespan(app: FastAPI):
|
| 17 |
-
global model
|
| 18 |
-
model = tf.keras.models.load_model('final_champion_model.keras', compile=False)
|
| 19 |
-
yield
|
| 20 |
-
model = None
|
| 21 |
-
|
| 22 |
-
app = FastAPI(lifespan=lifespan)
|
| 23 |
-
@app.get("/")
|
| 24 |
-
def read_root():
|
| 25 |
-
return {"message": "MRI Segmentation API is running and ready for predictions!"}
|
| 26 |
-
|
| 27 |
-
def slice_nii(file_path):
|
| 28 |
-
img = nib.load(file_path)
|
| 29 |
-
data = img.get_fdata()
|
| 30 |
-
mid_z = data.shape[2] // 2
|
| 31 |
-
slice_2d = data[:, :, mid_z]
|
| 32 |
-
if slice_2d.max() > 0:
|
| 33 |
-
slice_2d = (slice_2d - slice_2d.min()) / (slice_2d.max() - slice_2d.min()) * 255.0
|
| 34 |
-
slice_2d = slice_2d.astype(np.uint8)
|
| 35 |
-
return np.rot90(slice_2d)
|
| 36 |
-
|
| 37 |
-
@app.post("/predict")
|
| 38 |
-
async def predict(file: UploadFile = File(...), gt: UploadFile = File(None)):
|
| 39 |
-
# Save uploaded main file to temp
|
| 40 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.nii') as f:
|
| 41 |
-
f.write(await file.read())
|
| 42 |
-
temp_nii_path = f.name
|
| 43 |
-
|
| 44 |
-
slice_2d = slice_nii(temp_nii_path)
|
| 45 |
-
os.remove(temp_nii_path)
|
| 46 |
-
|
| 47 |
-
img_np = slice_2d
|
| 48 |
-
img_resized = cv2.resize(img_np, (128, 128))
|
| 49 |
-
img_normalized = img_resized / 255.0
|
| 50 |
-
input_tensor = np.expand_dims(img_normalized, axis=(0, -1))
|
| 51 |
-
|
| 52 |
-
predictions = model.predict(input_tensor)
|
| 53 |
-
mask = np.argmax(predictions, axis=-1)[0]
|
| 54 |
-
|
| 55 |
-
mask_visual = (mask * 85).astype(np.uint8)
|
| 56 |
-
mask_pil = Image.fromarray(mask_visual, mode='L')
|
| 57 |
-
|
| 58 |
-
buffered = io.BytesIO()
|
| 59 |
-
mask_pil.save(buffered, format="PNG")
|
| 60 |
-
mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 61 |
-
|
| 62 |
-
# Original image base64
|
| 63 |
-
orig_pil = Image.fromarray(img_np, mode='L')
|
| 64 |
-
buf_orig = io.BytesIO()
|
| 65 |
-
orig_pil.save(buf_orig, format="PNG")
|
| 66 |
-
orig_base64 = base64.b64encode(buf_orig.getvalue()).decode("utf-8")
|
| 67 |
-
|
| 68 |
-
# Process GT if provided
|
| 69 |
-
gt_base64 = None
|
| 70 |
-
if gt is not None:
|
| 71 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.nii') as fg:
|
| 72 |
-
endGT = await gt.read()
|
| 73 |
-
fg.write(endGT)
|
| 74 |
-
gt_path = fg.name
|
| 75 |
-
gt_slice = slice_nii(gt_path)
|
| 76 |
-
os.remove(gt_path)
|
| 77 |
-
gt_visual = (gt_slice * 85).astype(np.uint8) # Or whatever mapping
|
| 78 |
-
gt_pil = Image.fromarray(gt_visual, mode='L')
|
| 79 |
-
buf_gt = io.BytesIO()
|
| 80 |
-
gt_pil.save(buf_gt, format="PNG")
|
| 81 |
-
gt_base64 = base64.b64encode(buf_gt.getvalue()).decode("utf-8")
|
| 82 |
-
|
| 83 |
-
return {
|
| 84 |
-
"status": "success",
|
| 85 |
-
"mask_base64": mask_base64,
|
| 86 |
-
"orig_base64": orig_base64,
|
| 87 |
-
"gt_base64": gt_base64
|
| 88 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|