Pavan2k4 commited on
Commit
031194a
·
verified ·
1 Parent(s): 2a71a66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -72,14 +72,11 @@ MASK_DIR = os.path.join(base,"Masks")
72
 
73
  CSV_LOG_PATH = "image_log.csv"
74
 
75
- # Create directories
76
- def set_read_write_permissions(path):
77
- os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
78
 
79
  # Create directories with read and write permissions
80
  for directory in [UPLOAD_DIR, MASK_DIR]:
81
  os.makedirs(directory, exist_ok=True)
82
- set_read_write_permissions(directory)
83
 
84
 
85
 
@@ -208,18 +205,18 @@ def upload_page():
208
  filepath = os.path.join(UPLOAD_DIR, filename)
209
  converted_filepath = os.path.join(UPLOAD_DIR, converted_filename)
210
 
211
- with open(filepath, "wb") as f:
212
- f.write(bytes_data)
 
 
213
 
214
  if file_extension in ['.tiff', '.tif']:
215
  st.info('Processing GeoTIFF image...')
216
  rgb_image = read_pansharpened_rgb(filepath)
217
  cv2.imwrite(converted_filepath, cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR))
218
  st.success(f'GeoTIFF converted to 8-bit image and saved as {converted_filename}')
219
- img = Image.open(converted_filepath)
220
  else:
221
- img = Image.open(filepath)
222
- img.save(converted_filepath)
223
 
224
  if os.path.exists(converted_filepath):
225
  st.success(f"Image saved successfully: {converted_filepath}")
@@ -228,12 +225,15 @@ def upload_page():
228
  else:
229
  st.error(f"Failed to save image: {converted_filepath}")
230
 
231
- st.image(img, caption='Uploaded Image', use_column_width=True)
 
 
232
  st.success(f'Image processed and saved as {converted_filename}')
233
 
234
  st.session_state.filename = converted_filename
235
 
236
- img_array = np.array(img)
 
237
 
238
  if img_array.shape[0] > 650 or img_array.shape[1] > 650:
239
  st.info('Large image detected. Using patch-based processing.')
@@ -242,7 +242,10 @@ def upload_page():
242
  else:
243
  st.info('Small image detected. Processing whole image at once.')
244
  with st.spinner('Analyzing image...'):
245
- img_transformed = transforms(img)
 
 
 
246
  prediction = predict(img_transformed)
247
  full_mask = (prediction > 0.5).astype(np.uint8) * 255
248
 
@@ -269,7 +272,6 @@ def upload_page():
269
  st.success('Image analyzed')
270
  st.session_state.page = 'result'
271
  st.rerun()
272
-
273
  def result_page():
274
  st.title('Analysis Result')
275
 
 
72
 
73
  CSV_LOG_PATH = "image_log.csv"
74
 
 
 
 
75
 
76
  # Create directories with read and write permissions
77
  for directory in [UPLOAD_DIR, MASK_DIR]:
78
  os.makedirs(directory, exist_ok=True)
79
+
80
 
81
 
82
 
 
205
  filepath = os.path.join(UPLOAD_DIR, filename)
206
  converted_filepath = os.path.join(UPLOAD_DIR, converted_filename)
207
 
208
+ # Save original file using cv2
209
+ nparr = np.frombuffer(bytes_data, np.uint8)
210
+ img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
211
+ cv2.imwrite(filepath, img)
212
 
213
  if file_extension in ['.tiff', '.tif']:
214
  st.info('Processing GeoTIFF image...')
215
  rgb_image = read_pansharpened_rgb(filepath)
216
  cv2.imwrite(converted_filepath, cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR))
217
  st.success(f'GeoTIFF converted to 8-bit image and saved as {converted_filename}')
 
218
  else:
219
+ cv2.imwrite(converted_filepath, img)
 
220
 
221
  if os.path.exists(converted_filepath):
222
  st.success(f"Image saved successfully: {converted_filepath}")
 
225
  else:
226
  st.error(f"Failed to save image: {converted_filepath}")
227
 
228
+ # Display image using PIL (as Streamlit's st.image works better with PIL)
229
+ pil_img = Image.open(converted_filepath)
230
+ st.image(pil_img, caption='Uploaded Image', use_column_width=True)
231
  st.success(f'Image processed and saved as {converted_filename}')
232
 
233
  st.session_state.filename = converted_filename
234
 
235
+ # Use cv2 for further processing
236
+ img_array = cv2.imread(converted_filepath)
237
 
238
  if img_array.shape[0] > 650 or img_array.shape[1] > 650:
239
  st.info('Large image detected. Using patch-based processing.')
 
242
  else:
243
  st.info('Small image detected. Processing whole image at once.')
244
  with st.spinner('Analyzing image...'):
245
+ # Convert to RGB for the model
246
+ img_rgb = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
247
+ img_pil = Image.fromarray(img_rgb)
248
+ img_transformed = transforms(img_pil)
249
  prediction = predict(img_transformed)
250
  full_mask = (prediction > 0.5).astype(np.uint8) * 255
251
 
 
272
  st.success('Image analyzed')
273
  st.session_state.page = 'result'
274
  st.rerun()
 
275
  def result_page():
276
  st.title('Analysis Result')
277