Swathi02 commited on
Commit
cfa95d1
·
verified ·
1 Parent(s): 483f9d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +352 -118
app.py CHANGED
@@ -5,7 +5,6 @@ import numpy as np
5
  import cv2 as cv
6
  from google.cloud import storage
7
 
8
- # Constants
9
  GCS_BUCKET_NAME = "veytel-cloud-store"
10
  GCS_FOLDER_PATH = "density_mapper"
11
  service_account_json_str = os.getenv('serviceKey')
@@ -13,19 +12,60 @@ service_account_json = json.loads(service_account_json_str)
13
 
14
  if service_account_json:
15
  print("Secret Value Retrieved Successfully")
 
16
  else:
17
  print("Failed to Retrieve Secret Value")
18
 
19
- # GCS Authentication
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def authenticate_gcs():
21
  return storage.Client.from_service_account_info(service_account_json)
22
 
23
- # GCS Operations
24
  def download_csv_from_gcs(filename):
25
  client = authenticate_gcs()
26
  bucket = client.get_bucket(GCS_BUCKET_NAME)
27
  blob = bucket.blob(os.path.join(GCS_FOLDER_PATH, filename))
28
- return blob.download_as_text()
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  def upload_csv_to_gcs(csv_content, filename):
31
  client = authenticate_gcs()
@@ -33,139 +73,333 @@ def upload_csv_to_gcs(csv_content, filename):
33
  blob = bucket.blob(os.path.join(GCS_FOLDER_PATH, filename))
34
  blob.upload_from_string(csv_content)
35
 
36
- # Load user state
37
- def read_user_state(user):
38
- filename = f"density_{user}.csv"
39
- try:
40
- csv_content = download_csv_from_gcs(filename)
41
- rows = csv_content.strip().split("\n")
42
- csv_data = [row.split(",") for row in rows]
 
 
 
 
43
 
44
- if len(csv_data) > 1:
45
- last_row = csv_data[-1]
46
- user_state = {
47
- "count": int(last_row[0]),
48
- "max_dense_0": int(last_row[1]),
49
- "max_dense_1": int(last_row[2]),
50
- "max_dense_2": int(last_row[3])
51
- }
52
- else:
53
- user_state = {"count": 0, "max_dense_0": 50, "max_dense_1": 100, "max_dense_2": 150}
54
- return user_state
55
- except Exception as e:
56
- print(f"Error reading CSV for {user}: {e}")
57
- return {"count": 0, "max_dense_0": 50, "max_dense_1": 100, "max_dense_2": 150}
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- # Save user state
60
- def save_user_state(user, count, max_dense_0, max_dense_1, max_dense_2):
61
- csv_content = f"{count},{max_dense_0},{max_dense_1},{max_dense_2}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  filename = f"density_{user}.csv"
63
  upload_csv_to_gcs(csv_content, filename)
 
 
 
 
64
 
65
- # Density calculation functions
66
- def set_min_dense_1(max_dense_0, textured_cxr, max_val):
67
- scaled_thresh1 = int(max_dense_0) * max_val / 255
68
- dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
69
- return max_dense_0, dense_0
70
 
71
- def set_min_dense_2(max_dense_1, textured_cxr, scaled_thresh1, max_val):
72
- scaled_thresh2 = int(max_dense_1) * max_val / 255
73
- dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
74
- return max_dense_1, dense_1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- def set_min_dense_3(max_dense_2, textured_cxr, scaled_thresh2, max_val):
77
- scaled_thresh3 = int(max_dense_2) * max_val / 255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
79
  dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
80
- return max_dense_2, dense_2, dense_3
 
81
 
82
- # Update layout with new images and densities
83
- def update_layout(user_state, max_dense_0, max_dense_1, max_dense_2):
84
- count = user_state['count']
85
-
86
- # Dummy data (replace with actual image loading code)
87
- cxr = np.zeros((256, 256), dtype=np.uint8)
88
- textured_cxr = np.zeros((256, 256), dtype=np.uint8)
89
- lung_noised = np.zeros((256, 256), dtype=np.uint8)
90
-
91
- # Calculate scaled thresholds and densities
92
- max_val = np.percentile(cxr, 97)
93
- scaled_thresh1 = max_dense_0 * max_val / 255
94
- scaled_thresh2 = max_dense_1 * max_val / 255
95
- scaled_thresh3 = max_dense_2 * max_val / 255
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
98
  dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
99
  dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
100
  dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- return cxr, textured_cxr, lung_noised, dense_0, dense_1, dense_2, dense_3, count
 
 
 
 
 
 
 
 
103
 
104
- # Handle new CXR processing
105
- def new_cxr(user, max_dense_0, max_dense_1, max_dense_2):
106
- user_state = read_user_state(user)
107
- count = user_state['count'] + 1
108
- save_user_state(user, count, max_dense_0, max_dense_1, max_dense_2)
109
- return update_layout(user_state, max_dense_0, max_dense_1, max_dense_2)
110
 
111
- # User Authentication
112
- def check_auth(username, password):
113
- valid_users = {
114
- 'gk': 'upmc2023', 'veytel': 'pittsburgh',
115
- 'cathy': 'veytel', 'ellen': 'veytel', 'kevin': 'veytel',
116
- 'swathi': 'veytel', 'mike': 'veytel', 'test': 'test',
117
- 'nischal': 'veytel', 'vijayakumar': 'veytel',
118
- 'konstantine': 'upmc2023', 'taaha': 'upmc2023',
119
- 'nameer': 'upmc2023', 'siddique': 'upmc2023'
120
- }
121
- return valid_users.get(username) == password
122
-
123
- # Main Gradio Layout
124
- def auth_and_start(username, password):
125
- if check_auth(username, password):
126
- user_state = read_user_state(username)
127
- return update_layout(user_state, 50, 100, 150) # Default threshold values
128
- else:
129
- return "Invalid login credentials", None, None, None, None, None, None, None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- # Gradio App Interface
132
  with gr.Blocks() as demo:
133
- gr.Markdown("# Density Mapper")
134
-
135
- # Authentication input fields
136
- user_input = gr.Textbox(label="User Name")
137
- password_input = gr.Password(label="Password")
138
- auth_button = gr.Button("Login")
139
-
140
- # Output display placeholders
141
- cxr_output = gr.Image(label="Original CXR")
142
- synthetic_cxr_output = gr.Image(label="Synthetic CXR")
143
- combined_output = gr.Image(label="Combined Synthetic Density 0-3")
144
- dense0_output = gr.Image(label="Pixels @ Density 0")
145
- dense1_output = gr.Image(label="Pixels @ Density 1")
146
- dense2_output = gr.Image(label="Pixels @ Density 2")
147
- dense3_output = gr.Image(label="Pixels @ Density 3")
148
- progress_output = gr.Slider(minimum=0, maximum=30, step=1, label="Progress")
149
-
150
- # Hook up authentication and UI updates
151
- auth_button.click(auth_and_start, inputs=[user_input, password_input],
152
- outputs=[cxr_output, combined_output, synthetic_cxr_output,
153
- dense0_output, dense1_output, dense2_output,
154
- dense3_output, progress_output])
155
-
156
- # Sliders for adjusting densities
157
- max_dense_0_slider = gr.Slider(0, 255, value=50, label="Max Dense 0")
158
- max_dense_1_slider = gr.Slider(0, 255, value=100, label="Max Dense 1")
159
- max_dense_2_slider = gr.Slider(0, 255, value=150, label="Max Dense 2")
160
- update_button = gr.Button("Update Densities")
161
-
162
- def update_density_thresholds(username, max_dense_0, max_dense_1, max_dense_2):
163
- user_state = read_user_state(username)
164
- save_user_state(username, user_state['count'], max_dense_0, max_dense_1, max_dense_2)
165
- return update_layout(user_state, max_dense_0, max_dense_1, max_dense_2)
166
-
167
- update_button.click(update_density_thresholds, inputs=[user_input, max_dense_0_slider, max_dense_1_slider, max_dense_2_slider],
168
- outputs=[cxr_output, synthetic_cxr_output, combined_output, dense0_output, dense1_output, dense2_output, dense3_output, progress_output])
169
 
170
  if __name__ == "__main__":
171
- demo.launch()
 
 
 
 
5
  import cv2 as cv
6
  from google.cloud import storage
7
 
 
8
  GCS_BUCKET_NAME = "veytel-cloud-store"
9
  GCS_FOLDER_PATH = "density_mapper"
10
  service_account_json_str = os.getenv('serviceKey')
 
12
 
13
  if service_account_json:
14
  print("Secret Value Retrieved Successfully")
15
+ #print(service_account_json)
16
  else:
17
  print("Failed to Retrieve Secret Value")
18
 
19
+ count = 0
20
+ fresh_start = False
21
+
22
+ def set_min_dense_1(max_dense_0):
23
+ global scaled_thresh1
24
+ print("max_dense_0", max_dense_0)
25
+ scaled_thresh1 = int(max_dense_0) * max_val / 255
26
+ dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
27
+ dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
28
+ return max_dense_0, dense_0, dense_1
29
+
30
+
31
+ def set_min_dense_2(max_dense_1):
32
+ global scaled_thresh2
33
+ print("max_dense_1", max_dense_1)
34
+ scaled_thresh2 = int(max_dense_1) * max_val / 255
35
+ dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
36
+ dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
37
+ return max_dense_1, dense_1, dense_2
38
+
39
+
40
+ def set_min_dense_3(max_dense_2):
41
+ global scaled_thresh3
42
+ print("max_dense_2", max_dense_2)
43
+ scaled_thresh3 = int(max_dense_2) * max_val / 255
44
+ dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
45
+ dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
46
+ return max_dense_2, dense_2, dense_3
47
+
48
  def authenticate_gcs():
49
  return storage.Client.from_service_account_info(service_account_json)
50
 
51
+
52
  def download_csv_from_gcs(filename):
53
  client = authenticate_gcs()
54
  bucket = client.get_bucket(GCS_BUCKET_NAME)
55
  blob = bucket.blob(os.path.join(GCS_FOLDER_PATH, filename))
56
+ csv_content = blob.download_as_text()
57
+ return csv_content
58
+
59
+ def read_csv_from_gcs(user):
60
+ filename = f"density_{user}.csv"
61
+ try:
62
+ csv_content = download_csv_from_gcs(filename)
63
+ rows = csv_content.strip().split("\n")
64
+ csv_data = [row.split(",") for row in rows]
65
+ return csv_data
66
+ except Exception as e:
67
+ print("Error reading CSV from GCS:", e)
68
+ return []
69
 
70
  def upload_csv_to_gcs(csv_content, filename):
71
  client = authenticate_gcs()
 
73
  blob = bucket.blob(os.path.join(GCS_FOLDER_PATH, filename))
74
  blob.upload_from_string(csv_content)
75
 
76
+ def new__cxr(max_dense_0, max_dense_1, max_dense_2):
77
+ global textured_cxr, lung_noised, max_val, cxr, mask, scaled_thresh1, scaled_thresh2, scaled_thresh3, image_id, index, count, label1
78
+ csv_data = read_csv_from_gcs(user)
79
+ if count < 30:
80
+ csv_content = ""
81
+ for row in csv_data:
82
+ csv_content += ",".join(row) + "\n"
83
+ if count > 0:
84
+ csv_content += f"{count},{max_dense_0},{max_dense_1},{max_dense_2}\n"
85
+ filename = f"density_{user}.csv"
86
+ upload_csv_to_gcs(csv_content, filename)
87
 
88
+ if (count >= 30):
89
+ csv_content = ""
90
+ for row in csv_data:
91
+ csv_content += ",".join(row) + "\n"
92
+ if count > 0:
93
+ csv_content += f"{count},{max_dense_0},{max_dense_1},{max_dense_2}\n"
94
+ filename = f"density_{user}.csv"
95
+ upload_csv_to_gcs(csv_content, filename)
96
+ empty_image = np.zeros((256, 256), dtype=np.uint8)
97
+ label1.update(visible=False)
98
+ count = 0
99
+ image_id = 1
100
+ index = 1
101
+ fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
102
+ csv_content += ",".join(fieldnames) + "\n"
103
+ filename = f"density_{user}.csv"
104
+ upload_csv_to_gcs(csv_content, filename)
105
+ return empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, 0
106
+
107
+
108
+ if count >= 0:
109
+ index += 1
110
+ if index > 3:
111
+ index = 1
112
+ image_id += 1
113
 
114
+ count += 1
115
+ # write count, thresh1, thresh2, thresh3 to csv file
116
+ cxr_file = "cxr" + str(image_id) + "_cxr.png"
117
+ mask_file = "cxr" + str(image_id) + "_mask.png"
118
+ textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
119
+ lung_noised_file = "cxr" + str(image_id) + "_lung_noised_" + str(index) + ".png"
120
+ cxr_path = os.path.join(cxr_dir, cxr_file)
121
+ mask_path = os.path.join(mask_dir, mask_file)
122
+ textured_cxr_path = os.path.join(textured_cxr_dir, textured_cxr_file)
123
+ lung_noised_path = os.path.join(lung_noised_dir, lung_noised_file)
124
+ cxr = cv.imread(cxr_path, cv.IMREAD_GRAYSCALE)
125
+ mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
126
+ textured_cxr = cv.imread(textured_cxr_path, cv.IMREAD_GRAYSCALE)
127
+ lung_noised = cv.imread(lung_noised_path, cv.IMREAD_GRAYSCALE)
128
+ max_val = np.percentile(cxr, 97) # To optimize later
129
+ thresh1 = 50
130
+ thresh2 = 100
131
+ thresh3 = 150
132
+ scaled_thresh1 = thresh1 * max_val / 255
133
+ scaled_thresh2 = thresh2 * max_val / 255
134
+ scaled_thresh3 = thresh3 * max_val / 255
135
+ dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
136
+ dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
137
+ dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
138
+ dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
139
+ return cxr, textured_cxr, lung_noised, lung_noised, dense_0, dense_1, dense_2, dense_3, count
140
+
141
+
142
+ def create_csv():
143
+ global count, fieldnames, image_id, index, csv_path, fresh_start
144
+ fieldnames = ['count', 'thresh_1', 'thresh_2', 'thresh_3']
145
+ csv_data = read_csv_from_gcs(user)
146
+ csv_content = ""
147
+ last_row_count = 0
148
+ if not csv_data:
149
+ csv_content = ",".join(fieldnames) + "\n"
150
+ else:
151
+ for i, row in enumerate(csv_data):
152
+ csv_content += ",".join(row) + "\n"
153
+ if i != 0:
154
+ if isinstance(row[0], str):
155
+ last_row_count = 0
156
+ else:
157
+ last_row_count = int(row[0])
158
+
159
+ #csv_content += f"{count+1},{thresh1},{thresh2},{thresh3}\n"
160
  filename = f"density_{user}.csv"
161
  upload_csv_to_gcs(csv_content, filename)
162
+ count = last_row_count
163
+ if(count>0):
164
+ image_id, index = get_image_id_index(count)
165
+ fresh_start = True
166
 
 
 
 
 
 
167
 
168
+ def check_auth(username, password):
169
+ global user, fresh_start
170
+ user = username
171
+ if (user == 'gk' and password == 'upmc2023'):
172
+ create_csv()
173
+ return True
174
+ elif (user == 'veytel' and password == 'pittsburgh'):
175
+ create_csv()
176
+ return True
177
+ elif (user == 'cathy' and password == 'veytel'):
178
+ create_csv()
179
+ return True
180
+ elif (user == 'ellen' and password == 'veytel'):
181
+ create_csv()
182
+ return True
183
+ elif (user == 'kevin' and password == 'veytel'):
184
+ create_csv()
185
+ return True
186
+ elif (user == 'swathi' and password == 'veytel'):
187
+ create_csv()
188
+ return True
189
+ elif (user == 'mike' and password == 'veytel'):
190
+ create_csv()
191
+ return True
192
+ elif (user == 'test' and password == 'test'):
193
+ create_csv()
194
+ return True
195
+ elif (user == 'nischal' and password == 'veytel'):
196
+ create_csv()
197
+ return True
198
+ elif (user == 'vijayakumar' and password == 'veytel'):
199
+ create_csv()
200
+ return True
201
+ elif (user == 'konstantine' and password == 'upmc2023'):
202
+ create_csv()
203
+ return True
204
+ elif (user == 'taaha' and password == 'upmc2023'):
205
+ create_csv()
206
+ return True
207
+ elif (user == 'nameer' and password == 'upmc2023'):
208
+ create_csv()
209
+ return True
210
+ elif (user == 'siddique' and password == 'upmc2023'):
211
+ create_csv()
212
+ return True
213
 
214
+
215
+ def change_vis():
216
+ global count, fresh_start, button1, button2
217
+ if fresh_start:
218
+ fresh_start = not fresh_start
219
+ if count >= 30:
220
+ return gr.Label(visible=True), gr.Button(visible=False), gr.Button(visible=False)
221
+ else:
222
+ return gr.Label(visible=False), gr.Button(visible=True), gr.Button(visible=False)
223
+
224
+
225
+ image_id = 1
226
+ index = 1
227
+
228
+ def get_image_id_index(count):
229
+ #global image_id, index
230
+ if count ==0:
231
+ return 1, 1
232
+ image_id = (count - 1) // 3 + 1
233
+ index = (count - 1) % 3 + 1
234
+ return image_id, index
235
+
236
+ def set_layout():
237
+ global textured_cxr, lung_noised, max_val, cxr, mask, scaled_thresh1, scaled_thresh2, scaled_thresh3, image_id, index, count
238
+ csv_data = read_csv_from_gcs(user)
239
+ csv_content = ""
240
+ last_row_count = 0
241
+ if csv_data:
242
+ for i, row in enumerate(csv_data):
243
+ if i != 0:
244
+ if str(row[0]) == 'count':
245
+ last_row_count = 0
246
+ else:
247
+ last_row_count = int(row[0])
248
+
249
+
250
+ count = last_row_count
251
+ if (count > 0):
252
+ image_id, index = get_image_id_index(count)
253
+
254
+ if count >= 30:
255
+ empty_image = np.zeros((256, 256), dtype=np.uint8)
256
+ return empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, empty_image, 30
257
+ image_id, index = get_image_id_index(count)
258
+ if count == 0:
259
+ count = 1
260
+ image_id = 1
261
+ index = 1
262
+ else:
263
+ count +=1
264
+ index += 1
265
+ if index > 3:
266
+ index = 1
267
+ image_id += 1
268
+
269
+ #new__cxr(max_dense_0, max_dense_1, max_dense_2)
270
+ cxr_file = "cxr" + str(image_id) + "_cxr.png"
271
+ mask_file = "cxr" + str(image_id) + "_mask.png"
272
+ textured_cxr_file = "cxr" + str(image_id) + "_textured_" + str(index) + ".png"
273
+ lung_noised_file = "cxr" + str(image_id) + "_lung_noised_" + str(index) + ".png"
274
+ cxr_path = os.path.join(cxr_dir, cxr_file)
275
+ mask_path = os.path.join(mask_dir, mask_file)
276
+ textured_cxr_path = os.path.join(textured_cxr_dir, textured_cxr_file)
277
+ lung_noised_path = os.path.join(lung_noised_dir, lung_noised_file)
278
+ cxr = cv.imread(cxr_path, cv.IMREAD_GRAYSCALE)
279
+ mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
280
+ textured_cxr = cv.imread(textured_cxr_path, cv.IMREAD_GRAYSCALE)
281
+ lung_noised = cv.imread(lung_noised_path, cv.IMREAD_GRAYSCALE)
282
+ max_val = np.percentile(cxr, 97) # To optimize later
283
+ thresh1 = 50
284
+ thresh2 = 100
285
+ thresh3 = 150
286
+ scaled_thresh1 = thresh1 * max_val / 255
287
+ scaled_thresh2 = thresh2 * max_val / 255
288
+ scaled_thresh3 = thresh3 * max_val / 255
289
+ dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
290
+ dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
291
  dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
292
  dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
293
+ return cxr, textured_cxr, lung_noised, lung_noised, dense_0, dense_1, dense_2, dense_3, count
294
+
295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
 
297
+ def update_layout(image_id, index):
298
+ 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
299
+ executable_path = os.path.dirname(os.path.realpath(__file__))
300
+ cxr_dir = os.path.join(executable_path, "Images/cxr")
301
+ mask_dir = os.path.join(executable_path, "Images/mask")
302
+ textured_cxr_dir = os.path.join(executable_path, "Images/textured_cxr")
303
+ lung_noised_dir = os.path.join(executable_path, "Images/lung_noised")
304
+
305
+ empty_image = np.zeros((256, 256), dtype=np.uint8)
306
+ cxr = empty_image
307
+ mask = empty_image
308
+ textured_cxr = empty_image
309
+ lung_noised = empty_image
310
+ max_val = np.percentile(cxr, 97) # To optimize later
311
+ thresh1 = 50
312
+ thresh2 = 100
313
+ thresh3 = 150
314
+ scaled_thresh1 = thresh1 * max_val / 255
315
+ scaled_thresh2 = thresh2 * max_val / 255
316
+ scaled_thresh3 = thresh3 * max_val / 255
317
  dense_0 = np.where(textured_cxr < scaled_thresh1, textured_cxr, 0)
318
  dense_1 = np.where(((textured_cxr < scaled_thresh2) & (textured_cxr >= scaled_thresh1)), textured_cxr, 0)
319
  dense_2 = np.where(((textured_cxr < scaled_thresh3) & (textured_cxr >= scaled_thresh2)), textured_cxr, 0)
320
  dense_3 = np.where(((textured_cxr < 255) & (textured_cxr >= scaled_thresh3)), textured_cxr, 0)
321
+ # open csv in append mode
322
+ # add title
323
+ with gr.Row():
324
+ # label_title = gr.Label("Density Mapper", visible=True)
325
+ gr.Markdown(
326
+ """
327
+ <center>
328
+ <h1>Density Mapper</h1>
329
+ </center>
330
+ <h3>Instructions:</h3>
331
+ 1. Set the brightness of your display to maximum<br>
332
+ 2. Initiate the process by clicking the 'Start' button <br>
333
+ 3. Synthetic density(middle image in top row) is added to "Original CXR" to obtain "Synthetic CXR"<br>
334
+ 4. Adjust the brightness thresholds using the sliders provided \
335
+ to obtain the correct density maps for each level of RALE density<br>
336
+ 5. If a density level has absence of pixels at the upper limit, please set the Max Value to 255<br>
337
+ 6. Click "Save & continue" to proceed to the next image. The progress is shown in the progress bar<br>
338
+ 7. You may close the window and resume the process later when you reopen the window
339
+ """
340
+ )
341
 
342
+ with gr.Row():
343
+ with gr.Column():
344
+ im1 = gr.Image(cxr, label="Original CXR")
345
+ with gr.Column():
346
+ im2 = gr.Image(textured_cxr, label="Combined Synthetic Density 0-3")
347
+ with gr.Column():
348
+ im3 = gr.Image(lung_noised, label="Synthetic CXR")
349
+ with gr.Column():
350
+ label1 = gr.Label("Completed! Please close window", visible=False)
351
 
352
+ with gr.Row():
353
+ with gr.Column():
354
+ dense0 = gr.Image(dense_0, label="Pixels @ Density 0")
355
+ with gr.Row():
356
+ min_dense_0 = gr.Textbox(value='0', label="Min")
357
+ max_dense_0 = gr.Slider(0, 255, value=50, step=1, label="Max")
358
 
359
+ with gr.Column():
360
+ dense1 = gr.Image(dense_1, label="Pixels @ Density 1")
361
+ with gr.Row():
362
+ min_dense_1 = gr.Textbox(value='50', label="Min")
363
+ max_dense_1 = gr.Slider(0, 255, value=100, step=1, label="Max")
364
+ max_dense_0.change(set_min_dense_1, inputs=max_dense_0, outputs=[min_dense_1, dense0, dense1]).then(
365
+ set_min_dense_1, inputs=max_dense_0, outputs=[min_dense_1, dense0, dense1])
366
+
367
+ with gr.Column():
368
+ progress_log = gr.Slider(1, 30, value=0, step=1, label="progress")
369
+ button1 = gr.Button(value="Save & continue", visible=fresh_start)
370
+ button2 = gr.Button(value="Start", visible=not fresh_start)
371
+
372
+ with gr.Row():
373
+ with gr.Column():
374
+ dense2 = gr.Image(dense_2, label="Pixels @ Density 2")
375
+ with gr.Row():
376
+ min_dense_2 = gr.Textbox(value='100', label="Min")
377
+ max_dense_2 = gr.Slider(0, 255, value=150, step=1, label="Max")
378
+ max_dense_1.change(set_min_dense_2, inputs=max_dense_1, outputs=[min_dense_2, dense1, dense2]).then(
379
+ set_min_dense_2, inputs=max_dense_1, outputs=[min_dense_2, dense1, dense2])
380
+
381
+ with gr.Column():
382
+ dense3 = gr.Image(dense_3, label="Pixels @ Density 3")
383
+ with gr.Row():
384
+ min_dense_3 = gr.Textbox(value='150', label="Min")
385
+ max_dense_3 = gr.Textbox(value='255', label="Max")
386
+ max_dense_2.change(set_min_dense_3, inputs=max_dense_2, outputs=[min_dense_3, dense2, dense3]).then(
387
+ set_min_dense_3, inputs=max_dense_2, outputs=[min_dense_3, dense2, dense3])
388
+
389
+ with gr.Column(): # adding additional for better visualization
390
+ im3_1 = gr.Image(lung_noised, label="Synthetic CXR")
391
+ button1.click(new__cxr, inputs=[max_dense_0, max_dense_1, max_dense_2],
392
+ outputs=[im1, im2, im3, im3_1, dense0, dense1, dense2, dense3, progress_log])
393
+ button1.click(change_vis, outputs=[label1, button1, button2])
394
+ button2.click(set_layout,
395
+ outputs=[im1, im2, im3, im3_1, dense0, dense1, dense2, dense3, progress_log])
396
+ button2.click(change_vis, outputs=[label1, button1, button2])
397
 
 
398
  with gr.Blocks() as demo:
399
+ update_layout(image_id, index)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
 
401
  if __name__ == "__main__":
402
+ demo.launch(auth=check_auth)
403
+
404
+
405
+