truthdotphd commited on
Commit
5fe6028
·
verified ·
1 Parent(s): 57b4b4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -25
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import numpy as np
3
  import os
@@ -264,26 +265,28 @@ def process_satellite_images(red_file, green_file, blue_file, nir_file, batch_si
264
 
265
  return rgb_display, visualization, stats
266
 
 
 
267
 
268
- # Create Gradio interface with default examples
269
- demo = gr.Interface(
270
- fn=process_satellite_images,
271
- inputs=[
272
- gr.File(label="Red Channel (JP2)"),
273
- gr.File(label="Green Channel (JP2)"),
274
- gr.File(label="Blue Channel (JP2)"),
275
- gr.File(label="NIR Channel (JP2)"),
276
- gr.Slider(minimum=1, maximum=32, value=1, step=1, label="Batch Size", info="Higher values use more memory but process faster"),
277
- gr.Slider(minimum=500, maximum=2000, value=1000, step=100, label="Patch Size", info="Size of image patches for processing"),
278
- gr.Slider(minimum=100, maximum=500, value=300, step=50, label="Patch Overlap", info="Overlap between patches to avoid edge artifacts")
279
- ],
280
- outputs=[
281
- gr.Image(label="Original RGB Image"),
282
- gr.Image(label="Cloud Detection Visualization"),
283
- gr.Textbox(label="Statistics")
284
- ],
285
- title="Satellite Cloud Detection",
286
- description="""
287
  Upload separate JP2 files for Red, Green, Blue, and NIR channels to detect clouds in satellite imagery.
288
 
289
  This application uses the OmniCloudMask model to classify each pixel as:
@@ -293,11 +296,60 @@ demo = gr.Interface(
293
  - Cloud Shadow (3)
294
 
295
  The model works best with imagery at 10-50m resolution. For higher resolution imagery, downsampling is recommended.
296
- """,
297
- examples=[
298
- ["jp2s/B04.jp2", "jp2s/B03.jp2", "jp2s/B02.jp2", "jp2s/B8A.jp2", 1, 1000, 300]
299
- ]
300
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
 
302
  # Launch the app
303
- demo.launch(debug=True)
 
1
+ import psutil
2
  import gradio as gr
3
  import numpy as np
4
  import os
 
265
 
266
  return rgb_display, visualization, stats
267
 
268
+ def update_cpu():
269
+ return f"CPU Usage: {psutil.cpu_percent()}%"
270
 
271
+ with gr.Blocks() as demo:
272
+ cpu_text = gr.Textbox(label="CPU Usage")
273
+ check_cpu_btn = gr.Button("Check CPU")
274
+
275
+ # Attach the event handler using the click method
276
+ check_cpu_btn.click(fn=update_cpu, inputs=None, outputs=cpu_text)
277
+
278
+
279
+ # Define the CPU check function
280
+ def check_cpu_usage():
281
+ """Check and return the current CPU usage."""
282
+ return f"CPU Usage: {psutil.cpu_percent()}%"
283
+
284
+ # Create the Gradio application with Blocks
285
+ with gr.Blocks(title="Satellite Cloud Detection") as demo:
286
+ # Add the description
287
+ gr.Markdown("""
288
+ # Satellite Cloud Detection
289
+
290
  Upload separate JP2 files for Red, Green, Blue, and NIR channels to detect clouds in satellite imagery.
291
 
292
  This application uses the OmniCloudMask model to classify each pixel as:
 
296
  - Cloud Shadow (3)
297
 
298
  The model works best with imagery at 10-50m resolution. For higher resolution imagery, downsampling is recommended.
299
+ """)
300
+
301
+ # Main cloud detection interface
302
+ with gr.Row():
303
+ with gr.Column():
304
+ # Input components
305
+ red_input = gr.Image(type="filepath", label="Red Channel (JP2)")
306
+ green_input = gr.Image(type="filepath", label="Green Channel (JP2)")
307
+ blue_input = gr.Image(type="filepath", label="Blue Channel (JP2)")
308
+ nir_input = gr.Image(type="filepath", label="NIR Channel (JP2)")
309
+
310
+ batch_size = gr.Slider(minimum=1, maximum=32, value=1, step=1,
311
+ label="Batch Size",
312
+ info="Higher values use more memory but process faster")
313
+ patch_size = gr.Slider(minimum=500, maximum=2000, value=1000, step=100,
314
+ label="Patch Size",
315
+ info="Size of image patches for processing")
316
+ patch_overlap = gr.Slider(minimum=100, maximum=500, value=300, step=50,
317
+ label="Patch Overlap",
318
+ info="Overlap between patches to avoid edge artifacts")
319
+
320
+ process_btn = gr.Button("Process Cloud Detection")
321
+
322
+ with gr.Column():
323
+ # Output components
324
+ rgb_output = gr.Image(label="Original RGB Image")
325
+ cloud_output = gr.Image(label="Cloud Detection Visualization")
326
+ stats_output = gr.Textbox(label="Statistics")
327
+
328
+ # CPU usage monitoring section
329
+ with gr.Row():
330
+ with gr.Column():
331
+ gr.Markdown("## System Monitoring")
332
+ cpu_button = gr.Button("Check CPU Usage")
333
+ cpu_output = gr.Textbox(label="CPU Usage")
334
+
335
+ # Set up event handlers
336
+ process_btn.click(
337
+ fn=process_satellite_images,
338
+ inputs=[red_input, green_input, blue_input, nir_input, batch_size, patch_size, patch_overlap],
339
+ outputs=[rgb_output, cloud_output, stats_output]
340
+ )
341
+
342
+ cpu_button.click(
343
+ fn=check_cpu_usage,
344
+ inputs=None,
345
+ outputs=cpu_output
346
+ )
347
+
348
+ # Add examples
349
+ gr.Examples(
350
+ examples=[["jp2s/B04.jp2", "jp2s/B03.jp2", "jp2s/B02.jp2", "jp2s/B8A.jp2", 1, 1000, 300]],
351
+ inputs=[red_input, green_input, blue_input, nir_input, batch_size, patch_size, patch_overlap]
352
+ )
353
 
354
  # Launch the app
355
+ demo.launch()