Pavan2k4 commited on
Commit
c928a9a
·
verified ·
1 Parent(s): b0577b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -15
app.py CHANGED
@@ -203,18 +203,18 @@ def upload_page():
203
  filepath = os.path.join(UPLOAD_DIR, filename)
204
  converted_filepath = os.path.join(UPLOAD_DIR, converted_filename)
205
 
206
- # Save original file using cv2
207
- nparr = np.frombuffer(bytes_data, np.uint8)
208
- img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
209
- cv2.imwrite(filepath, img)
210
 
211
  if file_extension in ['.tiff', '.tif']:
212
  st.info('Processing GeoTIFF image...')
213
  rgb_image = read_pansharpened_rgb(filepath)
214
  cv2.imwrite(converted_filepath, cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR))
215
  st.success(f'GeoTIFF converted to 8-bit image and saved as {converted_filename}')
 
216
  else:
217
- cv2.imwrite(converted_filepath, img)
 
218
 
219
  if os.path.exists(converted_filepath):
220
  st.success(f"Image saved successfully: {converted_filepath}")
@@ -223,15 +223,12 @@ def upload_page():
223
  else:
224
  st.error(f"Failed to save image: {converted_filepath}")
225
 
226
- # Display image using PIL (as Streamlit's st.image works better with PIL)
227
- pil_img = Image.open(converted_filepath)
228
- st.image(pil_img, caption='Uploaded Image', use_column_width=True)
229
  st.success(f'Image processed and saved as {converted_filename}')
230
 
231
  st.session_state.filename = converted_filename
232
 
233
- # Use cv2 for further processing
234
- img_array = cv2.imread(converted_filepath)
235
 
236
  if img_array.shape[0] > 650 or img_array.shape[1] > 650:
237
  st.info('Large image detected. Using patch-based processing.')
@@ -240,14 +237,11 @@ def upload_page():
240
  else:
241
  st.info('Small image detected. Processing whole image at once.')
242
  with st.spinner('Analyzing image...'):
243
- # Convert to RGB for the model
244
- img_rgb = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
245
- img_pil = Image.fromarray(img_rgb)
246
- img_transformed = transforms(img_pil)
247
  prediction = predict(img_transformed)
248
  full_mask = (prediction > 0.5).astype(np.uint8) * 255
249
 
250
- full_mask = refine_mask(full_mask)
251
  mask_filename = f"mask_{timestamp}.png"
252
  mask_filepath = os.path.join(MASK_DIR, mask_filename)
253
  cv2.imwrite(mask_filepath, full_mask)
 
203
  filepath = os.path.join(UPLOAD_DIR, filename)
204
  converted_filepath = os.path.join(UPLOAD_DIR, converted_filename)
205
 
206
+ with open(filepath, "wb") as f:
207
+ f.write(bytes_data)
 
 
208
 
209
  if file_extension in ['.tiff', '.tif']:
210
  st.info('Processing GeoTIFF image...')
211
  rgb_image = read_pansharpened_rgb(filepath)
212
  cv2.imwrite(converted_filepath, cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR))
213
  st.success(f'GeoTIFF converted to 8-bit image and saved as {converted_filename}')
214
+ img = Image.open(converted_filepath)
215
  else:
216
+ img = Image.open(filepath)
217
+ img.save(converted_filepath)
218
 
219
  if os.path.exists(converted_filepath):
220
  st.success(f"Image saved successfully: {converted_filepath}")
 
223
  else:
224
  st.error(f"Failed to save image: {converted_filepath}")
225
 
226
+ st.image(img, caption='Uploaded Image', use_column_width=True)
 
 
227
  st.success(f'Image processed and saved as {converted_filename}')
228
 
229
  st.session_state.filename = converted_filename
230
 
231
+ img_array = np.array(img)
 
232
 
233
  if img_array.shape[0] > 650 or img_array.shape[1] > 650:
234
  st.info('Large image detected. Using patch-based processing.')
 
237
  else:
238
  st.info('Small image detected. Processing whole image at once.')
239
  with st.spinner('Analyzing image...'):
240
+ img_transformed = transforms(img)
 
 
 
241
  prediction = predict(img_transformed)
242
  full_mask = (prediction > 0.5).astype(np.uint8) * 255
243
 
244
+ full_mask = refine_mask(full_mask)#-----------------------------------------------------------------------
245
  mask_filename = f"mask_{timestamp}.png"
246
  mask_filepath = os.path.join(MASK_DIR, mask_filename)
247
  cv2.imwrite(mask_filepath, full_mask)