Spaces:
Sleeping
Sleeping
File size: 6,048 Bytes
c4f9a44 8a8c9e1 34a9c67 df3ee3f c4f9a44 f20e1e0 c9e0e70 c4f9a44 c9e0e70 a1b81e3 23f6964 67db196 3be6d9b a1b81e3 c4f9a44 a1b81e3 c4f9a44 a1b81e3 c9e0e70 c4f9a44 c9e0e70 c4f9a44 a1b81e3 c4f9a44 9197033 d30be54 c4f9a44 d30be54 c4f9a44 a1b81e3 34a9c67 7787ff3 9197033 a1b81e3 34a9c67 7787ff3 c4f9a44 f20e1e0 c4f9a44 f20e1e0 c4f9a44 3be6d9b a1b81e3 df3ee3f d97e3ee df3ee3f 67db196 d97e3ee 67db196 d97e3ee 67db196 d97e3ee 5793a9c 67db196 d97e3ee c4f9a44 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | import os
import sys
import gc
import base64
import tempfile
import traceback
import io
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
# Import the logic from the backend modules
from centerline_extraction import compute_centerlines_network, load_nrrd_as_vtk_image
from ostium_detection import extract_centerline_data
from endograft_generation import build_endograft_stl_payload
from maquette_generation import generate_fenestrated_maquette
from endograft_selection import start_endograft_pipeline
# Flask API server that wraps the VMTK centerline extraction, ostium detection, and endograft generation pipelines
app = Flask(__name__)
CORS(app) # Allow cross-origin requests from the Next.js frontend
app.config["MAX_CONTENT_LENGTH"] = 200 * 1024 * 1024 # Maximum file upload size (200 MB)
# Simple health check so the deployment platform knows the server is running
@app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "ok"})
# Runs the full VMTK centerline extraction pipeline on NRRD segmentation and returns the result as JSON
@app.route("/extract", methods=["POST"])
def extract():
# Check that the segmentation file sent to the centerline extraction endpoint exists and is an NRRD file
if "file" not in request.files:
return jsonify({"status": "error", "error": "No file provided"}), 400
uploaded = request.files["file"]
file_name = uploaded.filename.lower()
if not file_name.endswith(".nrrd"):
return jsonify({"status": "error", "error": "Invalid File Type. Allowed: .nrrd"}), 400
# Save to a temp file so VMTK can read it from disk
tmp = tempfile.NamedTemporaryFile(suffix=".nrrd", delete=False)
try:
uploaded.save(tmp.name)
tmp.close()
# Read the NRRD segmentation via SimpleITK (preserves full direction matrix)
image, origin, direction = load_nrrd_as_vtk_image(tmp.name)
# Voronoi centerline extraction
centerlines, branch_junctions = compute_centerlines_network(image, origin, direction)
# Extract labelled segments and artery bifurcation data
data = extract_centerline_data(centerlines, branch_junctions=branch_junctions)
# Free the large VTK pipeline objects immediately
del centerlines, branch_junctions
gc.collect()
# Include the NRRD image's physical extent so the frontend can normalize the centerline
dims = image.GetDimensions()
spacing = image.GetSpacing()
phys_size = [dims[i] * spacing[i] for i in range(3)]
phys_center = [origin[i] + phys_size[i] / 2.0 for i in range(3)]
data['volume_info'] = {'center': {'x': phys_center[0], 'y': phys_center[1], 'z': phys_center[2]}, 'size': {'x': phys_size[0], 'y': phys_size[1], 'z': phys_size[2]}}
del image
gc.collect()
return jsonify({"status": "success", "data": data})
except TimeoutError as e:
return jsonify({"status": "error", "error": str(e)}), 504
except Exception as e:
traceback.print_exc()
return jsonify({"status": "error", "error": str(e)}), 500
finally:
# Always clean up the temp file
try:
os.unlink(tmp.name)
except OSError:
pass
# Runs the endograft selection pipeline on computed bounds of aneurysm
@app.route("/select_graft", methods=["POST"])
def select_graft():
body = request.get_json(silent=True) or {}
proximal_diameter = body.get("proximal_diameter", 0)
distal_diameter = body.get("distal_diameter", 0)
proximal_start_point = body.get("proximal_start_point")
distal_end_point = body.get("distal_end_point")
length = body.get("length", 0)
fenestrations_exist = body.get("fenestrations_exist", False)
distance_to_last_fenestration = body.get("distance_to_last_fenestration", 0)
try:
selected_graft = start_endograft_pipeline(
proximal_diameter, distal_diameter, proximal_start_point, distal_end_point, length, fenestrations_exist, distance_to_last_fenestration
)
return jsonify({"status": "success", "data": selected_graft})
except Exception as e:
traceback.print_exc()
return jsonify({"status": "error", "error": str(e)}), 500
# Runs the endograft generation pipeline on VMTK centerline and returns the result as JSON
@app.route("/endograft", methods=["POST"])
def endograft():
body = request.get_json(silent=True) or {}
centerline = body.get("centerline")
params = body.get("params") or {}
if not isinstance(centerline, list) or len(centerline) < 2:
return jsonify({"status": "error", "error": "centerline must be a list of at least 2 points"}), 400
try:
payload = build_endograft_stl_payload(centerline, include_combined=False, **params)
result = {"endograft_stl_b64": base64.b64encode(payload["endograft"]).decode("utf-8"), "struts_stl_b64": base64.b64encode(payload["struts"]).decode("utf-8")}
return jsonify({"status": "success", "data": result})
except Exception as e:
traceback.print_exc()
return jsonify({"status": "error", "error": str(e)}), 500
# Generates endograft STL maquette
@app.route("/maquette", methods=["POST"])
def maquette():
body = request.get_json(silent=True) or {}
centerline = body.get("centerline")
params = body.get("params") or {}
fenestrations = body.get("fenestrations") or []
include_struts = body.get("include_struts", True)
if not isinstance(centerline, list) or len(centerline) < 2:
return jsonify({"status": "error", "error": "centerline must be a list of at least 2 points"}), 400
try:
stl_bytes = generate_fenestrated_maquette(centerline, params, fenestrations, include_struts)
result = {"maquette_stl_b64": base64.b64encode(stl_bytes).decode("utf-8")}
return jsonify({"status": "success", "data": result})
except Exception as e:
traceback.print_exc()
return jsonify({"status": "error", "error": str(e)}), 500
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5050))
app.run(host="0.0.0.0", port=port)
|