-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnuclickSegmentation.py
More file actions
149 lines (128 loc) · 5.43 KB
/
nuclickSegmentation.py
File metadata and controls
149 lines (128 loc) · 5.43 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
import os
import tempfile
import cv2
import numpy as np
import torch
from monai.apps.nuclick.transforms import AddClickSignalsd, PostFilterLabeld
from monai.data import PILReader
from monai.networks.nets import BasicUNet
from monai.transforms import (Activationsd, AsDiscreted, Compose,
EnsureChannelFirstd, LoadImage, LoadImaged,
ScaleIntensityRangeD, SqueezeDimd)
from skimage.measure import label, regionprops
def run_ai_model_inferencing(json_data, network):
print(json_data.keys())
image_data = json_data.get("image")
foreground_data = json_data.get("nuclei_location")
size_data = json_data.get("tilesize")
gx, gy, _, _, x, y = size_data
image = np.array(image_data)
temp_dir = tempfile.mkdtemp()
# infereing nuclei location #TODO
print('Tile reference', size_data)
print("\n")
print('nuclei locations', foreground_data)
print("\n")
print("image size", image.shape)
# adding tile reference to the input cordinates
for element in foreground_data:
element[0] = int(np.abs(element[0] - x))
element[1] = int(np.abs(element[1] - y))
print('updated foreground ', foreground_data)
##############################################
# Transforms
pre_transforms = Compose([
LoadImaged(
keys="image",
dtype=np.uint8,
image_only=True,
reader=PILReader(
converter=lambda im: im.convert("RGB"))),
EnsureChannelFirstd(keys="image"),
ScaleIntensityRangeD(
keys="image",
a_min=0.0,
a_max=255.0,
b_min=-1.0,
b_max=1.0),
AddClickSignalsd(
image="image",
foreground="foreground",
gaussian=False),
])
cv2.imwrite(os.path.join(temp_dir, 'image.png'), image)
data = {
"image": os.path.join(
temp_dir,
'image.png'),
"foreground": foreground_data}
data = pre_transforms(data)
# prediction
network.eval()
with torch.no_grad():
pred = network(data["image"])
# Transforms
post_transforms = Compose([
Activationsd(keys="pred", sigmoid=True),
AsDiscreted(keys="pred", threshold=0.5, dtype=np.uint8),
SqueezeDimd(keys="pred", dim=1),
PostFilterLabeld(keys="pred"),])
data["pred"] = pred
output_predictions = post_transforms(data)
nuclei_obj_props = regionprops(label(output_predictions["pred"]))
nuclei_annot_list = []
if len(nuclei_obj_props) >= 1:
for i in range(len(nuclei_obj_props)):
cx = nuclei_obj_props[i].centroid[1]
cy = nuclei_obj_props[i].centroid[0]
width = nuclei_obj_props[i].bbox[3] - \
nuclei_obj_props[i].bbox[1] + 1
height = nuclei_obj_props[i].bbox[2] - \
nuclei_obj_props[i].bbox[0] + 1
# generate contours
zero_image = np.zeros(output_predictions["pred"].shape)
zero_image[int(cy -
width /
2):int(cy +
width /
2), int(cx -
height /
2):int(cx +
height /
2)] = output_predictions["pred"][int(cy -
width /
2):int(cy +
width /
2), int(cx -
height /
2):int(cx +
height /
2)]
contours, _ = cv2.findContours(zero_image.astype(
'uint8'), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
output_list = [[[x[0][1] + gx, x[0][0] + gy, 0]
for x in arr.tolist()] for arr in list(contours)]
# create annotation json
for record in output_list:
cur_annot = {
'type': 'polyline',
'points': record,
'closed': True,
'fillColor': 'rgba(0,0,0,0)',
'lineColor': 'rgb(0,255,0)',
'group': 'Nuclick segmentation',
}
nuclei_annot_list.append(cur_annot)
return nuclei_annot_list
if __name__ == "__main__":
# Code for running the AI model locally
image_file = ""
foreground = [[190, 15], [218, 32], [296, 96]]
reader = PILReader(converter=lambda im: im.convert("RGB"))
image_np = LoadImage(
image_only=True,
dtype=np.uint8,
reader=reader)(image_file)
payload = {"image": image_np, "nuclei_location": str(foreground)}
output = run_ai_model_inferencing(payload)
print(output)