Spaces:
Sleeping
Sleeping
File size: 16,526 Bytes
8eddc2e f15e3f8 8eddc2e 0f25f3b f15e3f8 621d353 cfa95d1 621d353 cfa95d1 0f25f3b cfa95d1 0f25f3b cfa95d1 0f25f3b cfa95d1 0f25f3b cfa95d1 b65cfda cfa95d1 0f25f3b cfa95d1 b65cfda 483f9d5 cfa95d1 483f9d5 cfa95d1 211c91c cfa95d1 b65cfda cfa95d1 b65cfda cfa95d1 211c91c cfa95d1 0f25f3b cfa95d1 8eddc2e cfa95d1 b65cfda cfa95d1 211c91c 8eddc2e cfa95d1 | 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | import gradio as gr
import json
import os
import numpy as np
import cv2 as cv
from google.cloud import storage
GCS_BUCKET_NAME = "veytel-cloud-store"
GCS_FOLDER_PATH = "density_mapper"
service_account_json_str = os.getenv('serviceKey')
service_account_json = json.loads(service_account_json_str)
if service_account_json:
print("Secret Value Retrieved Successfully")
#print(service_account_json)
else:
print("Failed to Retrieve Secret Value")
count = 0
fresh_start = False
def set_min_dense_1(max_dense_0):
global scaled_thresh1
print("max_dense_0", max_dense_0)
scaled_thresh1 = int(max_dense_0) * max_val / 255
dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
return max_dense_0, dense_0, dense_1
def set_min_dense_2(max_dense_1):
global scaled_thresh2
print("max_dense_1", max_dense_1)
scaled_thresh2 = int(max_dense_1) * max_val / 255
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
return max_dense_1, dense_1, dense_2
def set_min_dense_3(max_dense_2):
global scaled_thresh3
print("max_dense_2", max_dense_2)
scaled_thresh3 = int(max_dense_2) * max_val / 255
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
return max_dense_2, dense_2, dense_3
def authenticate_gcs():
return storage.Client.from_service_account_info(service_account_json)
def download_csv_from_gcs(filename):
client = authenticate_gcs()
bucket = client.get_bucket(GCS_BUCKET_NAME)
blob = bucket.blob(os.path.join(GCS_FOLDER_PATH, filename))
csv_content = blob.download_as_text()
return csv_content
def read_csv_from_gcs(user):
filename = f"density_{user}.csv"
try:
csv_content = download_csv_from_gcs(filename)
rows = csv_content.strip().split("\n")
csv_data = [row.split(",") for row in rows]
return csv_data
except Exception as e:
print("Error reading CSV from GCS:", e)
return []
def upload_csv_to_gcs(csv_content, filename):
client = authenticate_gcs()
bucket = client.get_bucket(GCS_BUCKET_NAME)
blob = bucket.blob(os.path.join(GCS_FOLDER_PATH, filename))
blob.upload_from_string(csv_content)
def new__cxr(max_dense_0, max_dense_1, max_dense_2):
global textured_cxr, lung_noised, max_val, cxr, mask, scaled_thresh1, scaled_thresh2, scaled_thresh3, image_id, index, count, label1
csv_data = read_csv_from_gcs(user)
if count < 30:
csv_content = ""
for row in csv_data:
csv_content += ",".join(row) + "\n"
if count > 0:
csv_content += f"{count},{max_dense_0},{max_dense_1},{max_dense_2}\n"
filename = f"density_{user}.csv"
upload_csv_to_gcs(csv_content, filename)
if (count >= 30):
csv_content = ""
for row in csv_data:
csv_content += ",".join(row) + "\n"
if count > 0:
csv_content += f"{count},{max_dense_0},{max_dense_1},{max_dense_2}\n"
filename = f"density_{user}.csv"
upload_csv_to_gcs(csv_content, filename)
empty_image = np.zeros((256, 256), dtype=np.uint8)
label1.update(visible=False)
count = 0
image_id = 1
index = 1
fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
csv_content += ",".join(fieldnames) + "\n"
filename = f"density_{user}.csv"
upload_csv_to_gcs(csv_content, filename)
return empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, 0
if count >= 0:
index += 1
if index > 3:
index = 1
image_id += 1
count += 1
# write count, thresh1, thresh2, thresh3 to csv file
cxr_file = "cxr" + str(image_id) + "_cxr.png"
mask_file = "cxr" + str(image_id) + "_mask.png"
textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
lung_noised_file = "cxr" + str(image_id) + "_lung_noised_" + str(index) + ".png"
cxr_path = os.path.join(cxr_dir, cxr_file)
mask_path = os.path.join(mask_dir, mask_file)
textured_cxr_path = os.path.join(textured_cxr_dir, textured_cxr_file)
lung_noised_path = os.path.join(lung_noised_dir, lung_noised_file)
cxr = cv.imread(cxr_path, cv.IMREAD_GRAYSCALE)
mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
textured_cxr = cv.imread(textured_cxr_path, cv.IMREAD_GRAYSCALE)
lung_noised = cv.imread(lung_noised_path, cv.IMREAD_GRAYSCALE)
max_val = np.percentile(cxr, 97) # To optimize later
thresh1 = 50
thresh2 = 100
thresh3 = 150
scaled_thresh1 = thresh1 * max_val / 255
scaled_thresh2 = thresh2 * max_val / 255
scaled_thresh3 = thresh3 * max_val / 255
dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
return cxr, textured_cxr, lung_noised, lung_noised, dense_0, dense_1, dense_2, dense_3, count
def create_csv():
global count, fieldnames, image_id, index, csv_path, fresh_start
fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
csv_data = read_csv_from_gcs(user)
csv_content = ""
last_row_count = 0
if not csv_data:
csv_content = ",".join(fieldnames) + "\n"
else:
for i, row in enumerate(csv_data):
csv_content += ",".join(row) + "\n"
if i != 0:
if isinstance(row[0], str):
last_row_count = 0
else:
last_row_count = int(row[0])
#csv_content += f"{count+1},{thresh1},{thresh2},{thresh3}\n"
filename = f"density_{user}.csv"
upload_csv_to_gcs(csv_content, filename)
count = last_row_count
if(count>0):
image_id, index = get_image_id_index(count)
fresh_start = True
def check_auth(username, password):
global user, fresh_start
user = username
if (user == 'gk' and password == 'upmc2023'):
create_csv()
return True
elif (user == 'veytel' and password == 'pittsburgh'):
create_csv()
return True
elif (user == 'cathy' and password == 'veytel'):
create_csv()
return True
elif (user == 'ellen' and password == 'veytel'):
create_csv()
return True
elif (user == 'kevin' and password == 'veytel'):
create_csv()
return True
elif (user == 'swathi' and password == 'veytel'):
create_csv()
return True
elif (user == 'mike' and password == 'veytel'):
create_csv()
return True
elif (user == 'test' and password == 'test'):
create_csv()
return True
elif (user == 'nischal' and password == 'veytel'):
create_csv()
return True
elif (user == 'vijayakumar' and password == 'veytel'):
create_csv()
return True
elif (user == 'konstantine' and password == 'upmc2023'):
create_csv()
return True
elif (user == 'taaha' and password == 'upmc2023'):
create_csv()
return True
elif (user == 'nameer' and password == 'upmc2023'):
create_csv()
return True
elif (user == 'siddique' and password == 'upmc2023'):
create_csv()
return True
def change_vis():
global count, fresh_start, button1, button2
if fresh_start:
fresh_start = not fresh_start
if count >= 30:
return gr.Label(visible=True), gr.Button(visible=False), gr.Button(visible=False)
else:
return gr.Label(visible=False), gr.Button(visible=True), gr.Button(visible=False)
image_id = 1
index = 1
def get_image_id_index(count):
#global image_id, index
if count ==0:
return 1, 1
image_id = (count - 1) // 3 + 1
index = (count - 1) % 3 + 1
return image_id, index
def set_layout():
global textured_cxr, lung_noised, max_val, cxr, mask, scaled_thresh1, scaled_thresh2, scaled_thresh3, image_id, index, count
csv_data = read_csv_from_gcs(user)
csv_content = ""
last_row_count = 0
if csv_data:
for i, row in enumerate(csv_data):
if i != 0:
if str(row[0]) == 'count':
last_row_count = 0
else:
last_row_count = int(row[0])
count = last_row_count
if (count > 0):
image_id, index = get_image_id_index(count)
if count >= 30:
empty_image = np.zeros((256, 256), dtype=np.uint8)
return empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, 30
image_id, index = get_image_id_index(count)
if count == 0:
count = 1
image_id = 1
index = 1
else:
count +=1
index += 1
if index > 3:
index = 1
image_id += 1
#new__cxr(max_dense_0, max_dense_1, max_dense_2)
cxr_file = "cxr" + str(image_id) + "_cxr.png"
mask_file = "cxr" + str(image_id) + "_mask.png"
textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
lung_noised_file = "cxr" + str(image_id) + "_lung_noised_" + str(index) + ".png"
cxr_path = os.path.join(cxr_dir, cxr_file)
mask_path = os.path.join(mask_dir, mask_file)
textured_cxr_path = os.path.join(textured_cxr_dir, textured_cxr_file)
lung_noised_path = os.path.join(lung_noised_dir, lung_noised_file)
cxr = cv.imread(cxr_path, cv.IMREAD_GRAYSCALE)
mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
textured_cxr = cv.imread(textured_cxr_path, cv.IMREAD_GRAYSCALE)
lung_noised = cv.imread(lung_noised_path, cv.IMREAD_GRAYSCALE)
max_val = np.percentile(cxr, 97) # To optimize later
thresh1 = 50
thresh2 = 100
thresh3 = 150
scaled_thresh1 = thresh1 * max_val / 255
scaled_thresh2 = thresh2 * max_val / 255
scaled_thresh3 = thresh3 * max_val / 255
dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
return cxr, textured_cxr, lung_noised, lung_noised, dense_0, dense_1, dense_2, dense_3, count
def update_layout(image_id, index):
global cxr_dir,mask_dir, textured_cxr_dir,lung_noised_dir, cxr, mask, textured_cxr, lung_noised, max_val, thresh1, thresh2, thresh3, scaled_thresh1, scaled_thresh2, scaled_thresh3, dense_0, dense_1, dense_2, dense_3, label_title, min_dense_0, max_dense_3, button1, button2, label1
executable_path = os.path.dirname(os.path.realpath(__file__))
cxr_dir = os.path.join(executable_path, "Images/cxr")
mask_dir = os.path.join(executable_path, "Images/mask")
textured_cxr_dir = os.path.join(executable_path, "Images/textured_cxr")
lung_noised_dir = os.path.join(executable_path, "Images/lung_noised")
empty_image = np.zeros((256, 256), dtype=np.uint8)
cxr = empty_image
mask = empty_image
textured_cxr = empty_image
lung_noised = empty_image
max_val = np.percentile(cxr, 97) # To optimize later
thresh1 = 50
thresh2 = 100
thresh3 = 150
scaled_thresh1 = thresh1 * max_val / 255
scaled_thresh2 = thresh2 * max_val / 255
scaled_thresh3 = thresh3 * max_val / 255
dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
# open csv in append mode
# add title
with gr.Row():
# label_title = gr.Label("Density Mapper", visible=True)
gr.Markdown(
"""
<center>
<h1>Density Mapper</h1>
</center>
<h3>Instructions:</h3>
1. Set the brightness of your display to maximum<br>
2. Initiate the process by clicking the 'Start' button <br>
3. Synthetic density(middle image in top row) is added to "Original CXR" to obtain "Synthetic CXR"<br>
4. Adjust the brightness thresholds using the sliders provided \
to obtain the correct density maps for each level of RALE density<br>
5. If a density level has absence of pixels at the upper limit, please set the Max Value to 255<br>
6. Click "Save & continue" to proceed to the next image. The progress is shown in the progress bar<br>
7. You may close the window and resume the process later when you reopen the window
"""
)
with gr.Row():
with gr.Column():
im1 = gr.Image(cxr, label="Original CXR")
with gr.Column():
im2 = gr.Image(textured_cxr, label="Combined Synthetic Density 0-3")
with gr.Column():
im3 = gr.Image(lung_noised, label="Synthetic CXR")
with gr.Column():
label1 = gr.Label("Completed! Please close window", visible=False)
with gr.Row():
with gr.Column():
dense0 = gr.Image(dense_0, label="Pixels @ Density 0")
with gr.Row():
min_dense_0 = gr.Textbox(value='0', label="Min")
max_dense_0 = gr.Slider(0, 255, value=50, step=1, label="Max")
with gr.Column():
dense1 = gr.Image(dense_1, label="Pixels @ Density 1")
with gr.Row():
min_dense_1 = gr.Textbox(value='50', label="Min")
max_dense_1 = gr.Slider(0, 255, value=100, step=1, label="Max")
max_dense_0.change(set_min_dense_1, inputs=max_dense_0, outputs=[min_dense_1, dense0, dense1]).then(
set_min_dense_1, inputs=max_dense_0, outputs=[min_dense_1, dense0, dense1])
with gr.Column():
progress_log = gr.Slider(1, 30, value=0, step=1, label="progress")
button1 = gr.Button(value="Save & continue", visible=fresh_start)
button2 = gr.Button(value="Start", visible=not fresh_start)
with gr.Row():
with gr.Column():
dense2 = gr.Image(dense_2, label="Pixels @ Density 2")
with gr.Row():
min_dense_2 = gr.Textbox(value='100', label="Min")
max_dense_2 = gr.Slider(0, 255, value=150, step=1, label="Max")
max_dense_1.change(set_min_dense_2, inputs=max_dense_1, outputs=[min_dense_2, dense1, dense2]).then(
set_min_dense_2, inputs=max_dense_1, outputs=[min_dense_2, dense1, dense2])
with gr.Column():
dense3 = gr.Image(dense_3, label="Pixels @ Density 3")
with gr.Row():
min_dense_3 = gr.Textbox(value='150', label="Min")
max_dense_3 = gr.Textbox(value='255', label="Max")
max_dense_2.change(set_min_dense_3, inputs=max_dense_2, outputs=[min_dense_3, dense2, dense3]).then(
set_min_dense_3, inputs=max_dense_2, outputs=[min_dense_3, dense2, dense3])
with gr.Column(): # adding additional for better visualization
im3_1 = gr.Image(lung_noised, label="Synthetic CXR")
button1.click(new__cxr, inputs=[max_dense_0, max_dense_1, max_dense_2],
outputs=[im1, im2, im3, im3_1, dense0, dense1, dense2, dense3, progress_log])
button1.click(change_vis, outputs=[label1, button1, button2])
button2.click(set_layout,
outputs=[im1, im2, im3, im3_1, dense0, dense1, dense2, dense3, progress_log])
button2.click(change_vis, outputs=[label1, button1, button2])
with gr.Blocks() as demo:
update_layout(image_id, index)
if __name__ == "__main__":
demo.launch(auth=check_auth)
|