Spaces:
Sleeping
Sleeping
Added start button to reset progress based on previous state and fixed reload issue
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import numpy as np
|
| 4 |
import cv2 as cv
|
| 5 |
-
import csv
|
| 6 |
from google.cloud import storage
|
| 7 |
|
| 8 |
GCS_BUCKET_NAME = "veytel-cloud-store"
|
|
@@ -21,6 +20,7 @@ service_account_json = {
|
|
| 21 |
"universe_domain": "googleapis.com"
|
| 22 |
}
|
| 23 |
count = 0
|
|
|
|
| 24 |
|
| 25 |
def set_min_dense_1(max_dense_0):
|
| 26 |
global scaled_thresh1
|
|
@@ -77,22 +77,13 @@ def upload_csv_to_gcs(csv_content, filename):
|
|
| 77 |
blob.upload_from_string(csv_content)
|
| 78 |
|
| 79 |
def new__cxr(max_dense_0, max_dense_1, max_dense_2):
|
| 80 |
-
global textured_cxr, lung_noised, max_val, cxr, mask, scaled_thresh1, scaled_thresh2, scaled_thresh3, image_id, index, count
|
| 81 |
-
if count >= 0:
|
| 82 |
-
index += 1
|
| 83 |
-
if index > 3:
|
| 84 |
-
index = 1
|
| 85 |
-
image_id += 1
|
| 86 |
-
|
| 87 |
-
count += 1
|
| 88 |
-
|
| 89 |
csv_data = read_csv_from_gcs(user)
|
| 90 |
-
# write count, thresh1, thresh2, thresh3 to csv file
|
| 91 |
if count < 30:
|
| 92 |
csv_content = ""
|
| 93 |
for row in csv_data:
|
| 94 |
csv_content += ",".join(row) + "\n"
|
| 95 |
-
if count >0:
|
| 96 |
csv_content += f"{count},{max_dense_0},{max_dense_1},{max_dense_2}\n"
|
| 97 |
filename = f"density_{user}.csv"
|
| 98 |
upload_csv_to_gcs(csv_content, filename)
|
|
@@ -107,7 +98,7 @@ def new__cxr(max_dense_0, max_dense_1, max_dense_2):
|
|
| 107 |
upload_csv_to_gcs(csv_content, filename)
|
| 108 |
empty_image = np.zeros((256, 256), dtype=np.uint8)
|
| 109 |
label1.update(visible=False)
|
| 110 |
-
count =
|
| 111 |
image_id = 1
|
| 112 |
index = 1
|
| 113 |
fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
|
|
@@ -115,6 +106,16 @@ def new__cxr(max_dense_0, max_dense_1, max_dense_2):
|
|
| 115 |
filename = f"density_{user}.csv"
|
| 116 |
upload_csv_to_gcs(csv_content, filename)
|
| 117 |
return empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
cxr_file = "cxr" + str(image_id) + "_cxr.png"
|
| 119 |
mask_file = "cxr" + str(image_id) + "_mask.png"
|
| 120 |
textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
|
|
@@ -138,22 +139,33 @@ def new__cxr(max_dense_0, max_dense_1, max_dense_2):
|
|
| 138 |
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
|
| 139 |
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
|
| 140 |
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
|
| 141 |
-
return cxr, textured_cxr, lung_noised, lung_noised, dense_0, dense_1, dense_2, dense_3, count
|
| 142 |
|
| 143 |
|
| 144 |
def create_csv():
|
| 145 |
-
global count, fieldnames, image_id, index, csv_path
|
| 146 |
fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
|
| 147 |
csv_data = read_csv_from_gcs(user)
|
| 148 |
csv_content = ""
|
|
|
|
| 149 |
if not csv_data:
|
| 150 |
csv_content = ",".join(fieldnames) + "\n"
|
| 151 |
else:
|
| 152 |
-
for row in csv_data:
|
| 153 |
csv_content += ",".join(row) + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
#csv_content += f"{count+1},{thresh1},{thresh2},{thresh3}\n"
|
| 155 |
filename = f"density_{user}.csv"
|
| 156 |
upload_csv_to_gcs(csv_content, filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
def check_auth(username, password):
|
|
@@ -177,24 +189,69 @@ def check_auth(username, password):
|
|
| 177 |
elif (user == 'swathi' and password == 'veytel'):
|
| 178 |
create_csv()
|
| 179 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
def change_vis():
|
| 183 |
-
global count
|
|
|
|
|
|
|
| 184 |
if count >= 30:
|
| 185 |
-
return gr.Label.update(visible=True), gr.Button.update(visible=False)
|
| 186 |
else:
|
| 187 |
-
return gr.Label.update(visible=False), gr.Button.update(visible=True)
|
| 188 |
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
cxr_file = "cxr" + str(image_id) + "_cxr.png"
|
| 199 |
mask_file = "cxr" + str(image_id) + "_mask.png"
|
| 200 |
textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
|
|
@@ -203,7 +260,6 @@ with gr.Blocks() as demo:
|
|
| 203 |
mask_path = os.path.join(mask_dir, mask_file)
|
| 204 |
textured_cxr_path = os.path.join(textured_cxr_dir, textured_cxr_file)
|
| 205 |
lung_noised_path = os.path.join(lung_noised_dir, lung_noised_file)
|
| 206 |
-
# print("*"*50, cxr_path)
|
| 207 |
cxr = cv.imread(cxr_path, cv.IMREAD_GRAYSCALE)
|
| 208 |
mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
|
| 209 |
textured_cxr = cv.imread(textured_cxr_path, cv.IMREAD_GRAYSCALE)
|
|
@@ -219,6 +275,34 @@ with gr.Blocks() as demo:
|
|
| 219 |
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
|
| 220 |
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
|
| 221 |
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
# open csv in append mode
|
| 223 |
# add title
|
| 224 |
with gr.Row():
|
|
@@ -250,8 +334,9 @@ with gr.Blocks() as demo:
|
|
| 250 |
set_min_dense_1, inputs=max_dense_0, outputs=[min_dense_1, dense0, dense1])
|
| 251 |
|
| 252 |
with gr.Column():
|
| 253 |
-
progress_log = gr.Slider(1, 30, value=
|
| 254 |
-
button1 = gr.Button(value="Save & continue", visible=
|
|
|
|
| 255 |
|
| 256 |
with gr.Row():
|
| 257 |
with gr.Column():
|
|
@@ -272,10 +357,15 @@ with gr.Blocks() as demo:
|
|
| 272 |
|
| 273 |
with gr.Column(): # adding additional for better visualization
|
| 274 |
im3_1 = gr.Image(lung_noised, label="Synthetic CXR")
|
| 275 |
-
|
| 276 |
button1.click(new__cxr, inputs=[max_dense_0, max_dense_1, max_dense_2],
|
| 277 |
outputs=[im1, im2, im3, im3_1, dense0, dense1, dense2, dense3, progress_log])
|
| 278 |
-
button1.click(change_vis, outputs=[label1, button1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
if __name__ == "__main__":
|
| 281 |
demo.launch(auth=check_auth)
|
|
|
|
| 2 |
import os
|
| 3 |
import numpy as np
|
| 4 |
import cv2 as cv
|
|
|
|
| 5 |
from google.cloud import storage
|
| 6 |
|
| 7 |
GCS_BUCKET_NAME = "veytel-cloud-store"
|
|
|
|
| 20 |
"universe_domain": "googleapis.com"
|
| 21 |
}
|
| 22 |
count = 0
|
| 23 |
+
fresh_start = False
|
| 24 |
|
| 25 |
def set_min_dense_1(max_dense_0):
|
| 26 |
global scaled_thresh1
|
|
|
|
| 77 |
blob.upload_from_string(csv_content)
|
| 78 |
|
| 79 |
def new__cxr(max_dense_0, max_dense_1, max_dense_2):
|
| 80 |
+
global textured_cxr, lung_noised, max_val, cxr, mask, scaled_thresh1, scaled_thresh2, scaled_thresh3, image_id, index, count, label1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
csv_data = read_csv_from_gcs(user)
|
|
|
|
| 82 |
if count < 30:
|
| 83 |
csv_content = ""
|
| 84 |
for row in csv_data:
|
| 85 |
csv_content += ",".join(row) + "\n"
|
| 86 |
+
if count > 0:
|
| 87 |
csv_content += f"{count},{max_dense_0},{max_dense_1},{max_dense_2}\n"
|
| 88 |
filename = f"density_{user}.csv"
|
| 89 |
upload_csv_to_gcs(csv_content, filename)
|
|
|
|
| 98 |
upload_csv_to_gcs(csv_content, filename)
|
| 99 |
empty_image = np.zeros((256, 256), dtype=np.uint8)
|
| 100 |
label1.update(visible=False)
|
| 101 |
+
count = 0
|
| 102 |
image_id = 1
|
| 103 |
index = 1
|
| 104 |
fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
|
|
|
|
| 106 |
filename = f"density_{user}.csv"
|
| 107 |
upload_csv_to_gcs(csv_content, filename)
|
| 108 |
return empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, 0
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if count >= 0:
|
| 112 |
+
index += 1
|
| 113 |
+
if index > 3:
|
| 114 |
+
index = 1
|
| 115 |
+
image_id += 1
|
| 116 |
+
|
| 117 |
+
count += 1
|
| 118 |
+
# write count, thresh1, thresh2, thresh3 to csv file
|
| 119 |
cxr_file = "cxr" + str(image_id) + "_cxr.png"
|
| 120 |
mask_file = "cxr" + str(image_id) + "_mask.png"
|
| 121 |
textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
|
|
|
|
| 139 |
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
|
| 140 |
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
|
| 141 |
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
|
| 142 |
+
return cxr, textured_cxr, lung_noised, lung_noised, dense_0, dense_1, dense_2, dense_3, count
|
| 143 |
|
| 144 |
|
| 145 |
def create_csv():
|
| 146 |
+
global count, fieldnames, image_id, index, csv_path, fresh_start
|
| 147 |
fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
|
| 148 |
csv_data = read_csv_from_gcs(user)
|
| 149 |
csv_content = ""
|
| 150 |
+
last_row_count = 0
|
| 151 |
if not csv_data:
|
| 152 |
csv_content = ",".join(fieldnames) + "\n"
|
| 153 |
else:
|
| 154 |
+
for i, row in enumerate(csv_data):
|
| 155 |
csv_content += ",".join(row) + "\n"
|
| 156 |
+
if i != 0:
|
| 157 |
+
if isinstance(row[0], str):
|
| 158 |
+
last_row_count = 0
|
| 159 |
+
else:
|
| 160 |
+
last_row_count = int(row[0])
|
| 161 |
+
|
| 162 |
#csv_content += f"{count+1},{thresh1},{thresh2},{thresh3}\n"
|
| 163 |
filename = f"density_{user}.csv"
|
| 164 |
upload_csv_to_gcs(csv_content, filename)
|
| 165 |
+
count = last_row_count
|
| 166 |
+
if(count>0):
|
| 167 |
+
image_id, index = get_image_id_index(count)
|
| 168 |
+
fresh_start = True
|
| 169 |
|
| 170 |
|
| 171 |
def check_auth(username, password):
|
|
|
|
| 189 |
elif (user == 'swathi' and password == 'veytel'):
|
| 190 |
create_csv()
|
| 191 |
return True
|
| 192 |
+
elif (user == 'mike' and password == 'veytel'):
|
| 193 |
+
create_csv()
|
| 194 |
+
return True
|
| 195 |
+
elif (user == 'nischal' and password == 'veytel'):
|
| 196 |
+
create_csv()
|
| 197 |
+
return True
|
| 198 |
|
| 199 |
|
| 200 |
def change_vis():
|
| 201 |
+
global count, fresh_start, button1, button2
|
| 202 |
+
if fresh_start:
|
| 203 |
+
fresh_start = not fresh_start
|
| 204 |
if count >= 30:
|
| 205 |
+
return gr.Label.update(visible=True), gr.Button.update(visible=False), gr.Button.update(visible=False)
|
| 206 |
else:
|
| 207 |
+
return gr.Label.update(visible=False), gr.Button.update(visible=True), gr.Button.update(visible=False)
|
| 208 |
|
| 209 |
|
| 210 |
+
image_id = 1
|
| 211 |
+
index = 1
|
| 212 |
+
|
| 213 |
+
def get_image_id_index(count):
|
| 214 |
+
#global image_id, index
|
| 215 |
+
if count ==0:
|
| 216 |
+
return 1, 1
|
| 217 |
+
image_id = (count - 1) // 3 + 1
|
| 218 |
+
index = (count - 1) % 3 + 1
|
| 219 |
+
return image_id, index
|
| 220 |
+
|
| 221 |
+
def set_layout():
|
| 222 |
+
global textured_cxr, lung_noised, max_val, cxr, mask, scaled_thresh1, scaled_thresh2, scaled_thresh3, image_id, index, count
|
| 223 |
+
csv_data = read_csv_from_gcs(user)
|
| 224 |
+
csv_content = ""
|
| 225 |
+
last_row_count = 0
|
| 226 |
+
if csv_data:
|
| 227 |
+
for i, row in enumerate(csv_data):
|
| 228 |
+
if i != 0:
|
| 229 |
+
if str(row[0]) == 'count':
|
| 230 |
+
last_row_count = 0
|
| 231 |
+
else:
|
| 232 |
+
last_row_count = int(row[0])
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
count = last_row_count
|
| 236 |
+
if (count > 0):
|
| 237 |
+
image_id, index = get_image_id_index(count)
|
| 238 |
+
|
| 239 |
+
if count >= 30:
|
| 240 |
+
empty_image = np.zeros((256, 256), dtype=np.uint8)
|
| 241 |
+
return empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, 30
|
| 242 |
+
image_id, index = get_image_id_index(count)
|
| 243 |
+
if count == 0:
|
| 244 |
+
count = 1
|
| 245 |
+
image_id = 1
|
| 246 |
+
index = 1
|
| 247 |
+
else:
|
| 248 |
+
count +=1
|
| 249 |
+
index += 1
|
| 250 |
+
if index > 3:
|
| 251 |
+
index = 1
|
| 252 |
+
image_id += 1
|
| 253 |
+
|
| 254 |
+
#new__cxr(max_dense_0, max_dense_1, max_dense_2)
|
| 255 |
cxr_file = "cxr" + str(image_id) + "_cxr.png"
|
| 256 |
mask_file = "cxr" + str(image_id) + "_mask.png"
|
| 257 |
textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
|
|
|
|
| 260 |
mask_path = os.path.join(mask_dir, mask_file)
|
| 261 |
textured_cxr_path = os.path.join(textured_cxr_dir, textured_cxr_file)
|
| 262 |
lung_noised_path = os.path.join(lung_noised_dir, lung_noised_file)
|
|
|
|
| 263 |
cxr = cv.imread(cxr_path, cv.IMREAD_GRAYSCALE)
|
| 264 |
mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
|
| 265 |
textured_cxr = cv.imread(textured_cxr_path, cv.IMREAD_GRAYSCALE)
|
|
|
|
| 275 |
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
|
| 276 |
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
|
| 277 |
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
|
| 278 |
+
return cxr, textured_cxr, lung_noised, lung_noised, dense_0, dense_1, dense_2, dense_3, count
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
def update_layout(image_id, index):
|
| 283 |
+
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
|
| 284 |
+
executable_path = os.path.dirname(os.path.realpath(__file__))
|
| 285 |
+
cxr_dir = os.path.join(executable_path, "Images/cxr")
|
| 286 |
+
mask_dir = os.path.join(executable_path, "Images/mask")
|
| 287 |
+
textured_cxr_dir = os.path.join(executable_path, "Images/textured_cxr")
|
| 288 |
+
lung_noised_dir = os.path.join(executable_path, "Images/lung_noised")
|
| 289 |
+
|
| 290 |
+
empty_image = np.zeros((256, 256), dtype=np.uint8)
|
| 291 |
+
cxr = empty_image
|
| 292 |
+
mask = empty_image
|
| 293 |
+
textured_cxr = empty_image
|
| 294 |
+
lung_noised = empty_image
|
| 295 |
+
max_val = np.percentile(cxr, 97) # To optimize later
|
| 296 |
+
thresh1 = 50
|
| 297 |
+
thresh2 = 100
|
| 298 |
+
thresh3 = 150
|
| 299 |
+
scaled_thresh1 = thresh1 * max_val / 255
|
| 300 |
+
scaled_thresh2 = thresh2 * max_val / 255
|
| 301 |
+
scaled_thresh3 = thresh3 * max_val / 255
|
| 302 |
+
dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
|
| 303 |
+
dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
|
| 304 |
+
dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
|
| 305 |
+
dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
|
| 306 |
# open csv in append mode
|
| 307 |
# add title
|
| 308 |
with gr.Row():
|
|
|
|
| 334 |
set_min_dense_1, inputs=max_dense_0, outputs=[min_dense_1, dense0, dense1])
|
| 335 |
|
| 336 |
with gr.Column():
|
| 337 |
+
progress_log = gr.Slider(1, 30, value=0, step=1, label="progress")
|
| 338 |
+
button1 = gr.Button(value="Save & continue", visible=fresh_start)
|
| 339 |
+
button2 = gr.Button(value="Start", visible=not fresh_start)
|
| 340 |
|
| 341 |
with gr.Row():
|
| 342 |
with gr.Column():
|
|
|
|
| 357 |
|
| 358 |
with gr.Column(): # adding additional for better visualization
|
| 359 |
im3_1 = gr.Image(lung_noised, label="Synthetic CXR")
|
|
|
|
| 360 |
button1.click(new__cxr, inputs=[max_dense_0, max_dense_1, max_dense_2],
|
| 361 |
outputs=[im1, im2, im3, im3_1, dense0, dense1, dense2, dense3, progress_log])
|
| 362 |
+
button1.click(change_vis, outputs=[label1, button1, button2])
|
| 363 |
+
button2.click(set_layout,
|
| 364 |
+
outputs=[im1, im2, im3, im3_1, dense0, dense1, dense2, dense3, progress_log])
|
| 365 |
+
button2.click(change_vis, outputs=[label1, button1, button2])
|
| 366 |
+
|
| 367 |
+
with gr.Blocks() as demo:
|
| 368 |
+
update_layout(image_id, index)
|
| 369 |
|
| 370 |
if __name__ == "__main__":
|
| 371 |
demo.launch(auth=check_auth)
|