speed up DICOM slice previews
This commit is contained in:
@@ -12,7 +12,7 @@ from email.policy import default
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from urllib.parse import parse_qs, unquote, urlparse
|
||||
from urllib.parse import parse_qs, quote, unquote, urlparse
|
||||
|
||||
os.environ.setdefault("MPLCONFIGDIR", "/tmp/head_ct_morph_matplotlib")
|
||||
|
||||
@@ -37,9 +37,11 @@ HOST = "0.0.0.0"
|
||||
PORT = 8787
|
||||
JOBS = {}
|
||||
JOBS_LOCK = threading.Lock()
|
||||
DICOM_FILE_CACHE = {}
|
||||
LIBRARY_DIR = APP_DIR / "web_library"
|
||||
LIBRARY_META = LIBRARY_DIR / "library.json"
|
||||
RESULT_DIR = APP_DIR / "web_results"
|
||||
PREVIEW_CACHE_DIR = LIBRARY_DIR / "_preview_cache"
|
||||
|
||||
|
||||
def json_default(value):
|
||||
@@ -124,7 +126,23 @@ def sort_key_for_dicom(path):
|
||||
|
||||
|
||||
def sorted_dicom_files(dicom_dir):
|
||||
return sorted(Path(dicom_dir).glob("*.dcm"), key=sort_key_for_dicom)
|
||||
dicom_dir = Path(dicom_dir).resolve()
|
||||
files = list(dicom_dir.glob("*.dcm"))
|
||||
signature = (
|
||||
str(dicom_dir),
|
||||
len(files),
|
||||
max((file_path.stat().st_mtime for file_path in files), default=0),
|
||||
)
|
||||
cached = DICOM_FILE_CACHE.get(str(dicom_dir))
|
||||
if cached and cached["signature"] == signature:
|
||||
return cached["files"]
|
||||
|
||||
sorted_files = sorted(files, key=sort_key_for_dicom)
|
||||
DICOM_FILE_CACHE[str(dicom_dir)] = {
|
||||
"signature": signature,
|
||||
"files": sorted_files,
|
||||
}
|
||||
return sorted_files
|
||||
|
||||
|
||||
def find_library_item(item_id):
|
||||
@@ -142,22 +160,30 @@ def make_library_slice_preview(item_id, index):
|
||||
|
||||
count = len(dicom_files)
|
||||
index = max(0, min(int(index), count - 1))
|
||||
ds = pydicom.dcmread(str(dicom_files[index]), force=True)
|
||||
image = ds.pixel_array.astype("float32")
|
||||
image = image * float(getattr(ds, "RescaleSlope", 1))
|
||||
image = image + float(getattr(ds, "RescaleIntercept", 0))
|
||||
cache_dir = PREVIEW_CACHE_DIR / item_id
|
||||
safe_mkdir(cache_dir)
|
||||
preview_path = cache_dir / f"slice_{index:04d}.png"
|
||||
if not preview_path.exists():
|
||||
ds = pydicom.dcmread(str(dicom_files[index]), force=True)
|
||||
image = ds.pixel_array.astype("float32")
|
||||
image = image * float(getattr(ds, "RescaleSlope", 1))
|
||||
image = image + float(getattr(ds, "RescaleIntercept", 0))
|
||||
|
||||
preview = Image.fromarray(ct_window(image)).convert("RGB")
|
||||
preview = fit_image(preview, 720, 520)
|
||||
canvas = BytesIO()
|
||||
preview.save(canvas, format="PNG")
|
||||
encoded = base64.b64encode(canvas.getvalue()).decode("ascii")
|
||||
preview = Image.fromarray(ct_window(image)).convert("RGB")
|
||||
preview = fit_image(preview, 720, 520)
|
||||
preview.save(preview_path, format="PNG")
|
||||
|
||||
neighbors = [value for value in [index - 1, index + 1] if 0 <= value < count]
|
||||
return {
|
||||
"image": f"data:image/png;base64,{encoded}",
|
||||
"imageUrl": f"/api/file?path={quote(str(preview_path.resolve()), safe='')}",
|
||||
"index": index,
|
||||
"count": count,
|
||||
"file": dicom_files[index].name,
|
||||
"patientId": item["patientId"],
|
||||
"neighbors": [
|
||||
f"/api/library/preview?id={item_id}&index={neighbor}"
|
||||
for neighbor in neighbors
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@@ -522,7 +548,11 @@ class Handler(BaseHTTPRequestHandler):
|
||||
write_library_meta(remaining)
|
||||
upload_root = Path(target["dicomPath"]).resolve().parent
|
||||
if upload_root.exists() and upload_root.is_relative_to(LIBRARY_DIR.resolve()):
|
||||
DICOM_FILE_CACHE.pop(str(Path(target["dicomPath"]).resolve()), None)
|
||||
shutil.rmtree(upload_root)
|
||||
preview_cache = PREVIEW_CACHE_DIR / item_id
|
||||
if preview_cache.exists():
|
||||
shutil.rmtree(preview_cache)
|
||||
self.send_json({"ok": True, "items": remaining})
|
||||
|
||||
def read_bytes(self):
|
||||
|
||||
Reference in New Issue
Block a user