forked from Project-MONAI/MONAILabel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfer.py
More file actions
204 lines (171 loc) · 7.84 KB
/
Copy pathinfer.py
File metadata and controls
204 lines (171 loc) · 7.84 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import os
import pathlib
import shutil
import tempfile
from enum import Enum
from typing import Optional
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
from fastapi.background import BackgroundTasks
from fastapi.responses import FileResponse, Response
from requests_toolbelt import MultipartEncoder
from monailabel.config import RBAC_USER, settings
from monailabel.datastore.dicom import DICOMWebDatastore
from monailabel.datastore.utils.convert import binary_to_image, nifti_to_dicom_seg
from monailabel.endpoints.user.auth import RBAC, User
from monailabel.interfaces.app import MONAILabelApp
from monailabel.interfaces.utils.app import app_instance
from monailabel.utils.others.generic import get_mime_type, remove_file
logger = logging.getLogger(__name__)
router = APIRouter(
prefix="/infer",
tags=["Infer"],
responses={
404: {"description": "Not found"},
200: {
"description": "OK",
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"points": {
"type": "string",
"description": "Reserved for future; Currently it will be empty",
},
"file": {
"type": "string",
"format": "binary",
"description": "The result NIFTI image which will have segmentation mask",
},
},
},
"encoding": {
"points": {"contentType": "text/plain"},
"file": {"contentType": "application/octet-stream"},
},
},
"application/json": {"schema": {"type": "string", "example": "{}"}},
"application/octet-stream": {"schema": {"type": "string", "format": "binary"}},
"application/dicom": {"schema": {"type": "string", "format": "binary"}},
},
},
},
)
class ResultType(str, Enum):
image = "image"
json = "json"
all = "all"
dicom_seg = "dicom_seg"
def send_response(datastore, result, output, background_tasks):
res_img = result.get("file") if result.get("file") else result.get("label")
res_tag = result.get("tag")
res_json = result.get("params")
if res_img:
if not os.path.exists(res_img):
res_img = datastore.get_label_uri(res_img, res_tag)
else:
background_tasks.add_task(remove_file, res_img)
if output == "json":
return res_json
if output == "image":
return FileResponse(res_img, media_type=get_mime_type(res_img), filename=os.path.basename(res_img))
if output == "dicom_seg":
res_dicom_seg = result.get("dicom_seg")
if res_dicom_seg is None:
raise HTTPException(status_code=500, detail="Error processing inference")
else:
return FileResponse(res_dicom_seg, media_type="application/dicom", filename=os.path.basename(res_dicom_seg))
res_fields = dict()
res_fields["params"] = (None, json.dumps(res_json), "application/json")
if res_img and os.path.exists(res_img):
res_fields["image"] = (os.path.basename(res_img), open(res_img, "rb"), get_mime_type(res_img))
else:
logger.info(f"Return only Result Json as Result Image is not available: {res_img}")
return res_json
return_message = MultipartEncoder(fields=res_fields)
return Response(content=return_message.to_string(), media_type=return_message.content_type)
def run_inference(
background_tasks: BackgroundTasks,
model: str,
image: str = "",
session_id: str = "",
params: str = Form("{}"),
file: UploadFile = File(None),
label: UploadFile = File(None),
output: Optional[ResultType] = None,
):
request = {"model": model, "image": image}
if not file and not image and not session_id:
raise HTTPException(status_code=500, detail="Neither Image nor File not Session ID input is provided")
instance: MONAILabelApp = app_instance()
if file:
file_ext = "".join(pathlib.Path(file.filename).suffixes) if file.filename else ".nii.gz"
image_file = tempfile.NamedTemporaryFile(suffix=file_ext).name
with open(image_file, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
request["image"] = image_file
background_tasks.add_task(remove_file, image_file)
if label:
file_ext = "".join(pathlib.Path(label.filename).suffixes) if label.filename else ".nii.gz"
label_file = tempfile.NamedTemporaryFile(suffix=file_ext).name
with open(label_file, "wb") as buffer:
shutil.copyfileobj(label.file, buffer)
background_tasks.add_task(remove_file, label_file)
# if binary file received, e.g. scribbles from OHIF - then convert using reference image
if file_ext == ".bin":
image_uri = instance.datastore().get_image_uri(image)
label_file = binary_to_image(image_uri, label_file)
request["label"] = label_file
config = instance.info().get("config", {}).get("infer", {})
request.update(config)
p = json.loads(params) if params else {}
request.update(p)
if session_id:
session = instance.sessions().get_session(session_id)
if session:
request["image"] = session.image
request["session"] = session.to_json()
logger.info(f"Infer Request: {request}")
result = instance.infer(request)
if result is None:
raise HTTPException(status_code=500, detail="Failed to execute infer")
# Dicom Seg Integration
if output == "dicom_seg":
dicom_seg_file = None
if not isinstance(instance.datastore(), DICOMWebDatastore):
raise HTTPException(status_code=500, detail="DICOM SEG format is not supported in a non-DICOM datastore")
elif p.get("label_info") is None:
raise HTTPException(status_code=404, detail="Parameters for DICOM SEG inference cannot be empty!")
# Transform image uri to id (similar to _to_id in local datastore)
image_uri = instance.datastore().get_image_uri(image)
suffixes = [".nii", ".nii.gz", ".nrrd"]
image_path = [image_uri.replace(suffix, "") for suffix in suffixes if image_uri.endswith(suffix)][0]
res_img = result.get("file") if result.get("file") else result.get("label")
dicom_seg_file = nifti_to_dicom_seg(image_path, res_img, p.get("label_info"))
result["dicom_seg"] = dicom_seg_file
return send_response(instance.datastore(), result, output, background_tasks)
@router.post("/{model}", summary=f"{RBAC_USER}Run Inference for supported model")
async def api_run_inference(
background_tasks: BackgroundTasks,
model: str,
image: str = "",
session_id: str = "",
params: str = Form("{}"),
file: UploadFile = File(None),
label: UploadFile = File(None),
output: Optional[ResultType] = None,
user: User = Depends(RBAC(settings.MONAI_LABEL_AUTH_ROLE_USER)),
):
return run_inference(background_tasks, model, image, session_id, params, file, label, output)