BorisEm commited on
Commit
40c7ec0
·
1 Parent(s): 0fa2ece

Add validation to user input

Browse files
Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -735,13 +735,32 @@ def get_sample_images():
735
  return []
736
 
737
  # Gradio interface using Blocks for better layout control
 
 
 
 
 
 
 
 
 
 
 
738
  def upscale_and_display(image):
739
  if image is None:
740
- return None
 
 
 
 
 
741
 
742
- # Get the super-resolution output
743
- upscaled = upscale_image(image)
744
- return upscaled
 
 
 
745
 
746
  def select_sample_image(image_path):
747
  if image_path:
@@ -805,6 +824,7 @@ css = generate_css()
805
  with gr.Blocks(css=css, title="HAT Super-Resolution for Satellite Images") as iface:
806
  gr.Markdown("# HAT Super-Resolution for Satellite Images")
807
  gr.Markdown("Upload a satellite image or select a sample to enhance its resolution by 4x.")
 
808
 
809
  # Sample images
810
  sample_images = get_sample_images()
@@ -823,7 +843,7 @@ with gr.Blocks(css=css, title="HAT Super-Resolution for Satellite Images") as if
823
  with gr.Row():
824
  input_image = gr.Image(
825
  type="pil",
826
- label="Input Image",
827
  elem_classes="image-container",
828
  sources=["upload"],
829
  height=500,
@@ -842,12 +862,19 @@ with gr.Blocks(css=css, title="HAT Super-Resolution for Satellite Images") as if
842
 
843
  submit_btn = gr.Button("Enhance Image", variant="primary")
844
 
 
 
 
 
 
 
 
845
  # Event handlers
846
  if sample_images:
847
  for btn, img_path in sample_buttons:
848
  btn.click(fn=lambda path=img_path: select_sample_image(path), outputs=input_image)
849
 
850
- submit_btn.click(fn=upscale_and_display, inputs=input_image, outputs=output_image)
851
 
852
  if __name__ == "__main__":
853
  iface.launch()
 
735
  return []
736
 
737
  # Gradio interface using Blocks for better layout control
738
+ def validate_image_size(image):
739
+ """Validate that the image is exactly 130x130 pixels"""
740
+ if image is None:
741
+ return False, "No image provided"
742
+
743
+ width, height = image.size
744
+ if width != 130 or height != 130:
745
+ return False, f"Image must be exactly 130x130 pixels. Your image is {width}x{height} pixels."
746
+
747
+ return True, "Valid image size"
748
+
749
  def upscale_and_display(image):
750
  if image is None:
751
+ return None, "Please upload an image or select a sample image."
752
+
753
+ # Validate image size
754
+ is_valid, message = validate_image_size(image)
755
+ if not is_valid:
756
+ return None, f"❌ Error: {message}"
757
 
758
+ try:
759
+ # Get the super-resolution output
760
+ upscaled = upscale_image(image)
761
+ return upscaled, "✅ Image successfully enhanced!"
762
+ except Exception as e:
763
+ return None, f"❌ Error processing image: {str(e)}"
764
 
765
  def select_sample_image(image_path):
766
  if image_path:
 
824
  with gr.Blocks(css=css, title="HAT Super-Resolution for Satellite Images") as iface:
825
  gr.Markdown("# HAT Super-Resolution for Satellite Images")
826
  gr.Markdown("Upload a satellite image or select a sample to enhance its resolution by 4x.")
827
+ gr.Markdown("⚠️ **Important**: Images must be exactly **130x130 pixels** for the model to work properly.")
828
 
829
  # Sample images
830
  sample_images = get_sample_images()
 
843
  with gr.Row():
844
  input_image = gr.Image(
845
  type="pil",
846
+ label="Input Image (must be 130x130 pixels)",
847
  elem_classes="image-container",
848
  sources=["upload"],
849
  height=500,
 
862
 
863
  submit_btn = gr.Button("Enhance Image", variant="primary")
864
 
865
+ # Status message
866
+ status_message = gr.Textbox(
867
+ label="Status",
868
+ interactive=False,
869
+ show_label=True
870
+ )
871
+
872
  # Event handlers
873
  if sample_images:
874
  for btn, img_path in sample_buttons:
875
  btn.click(fn=lambda path=img_path: select_sample_image(path), outputs=input_image)
876
 
877
+ submit_btn.click(fn=upscale_and_display, inputs=input_image, outputs=[output_image, status_message])
878
 
879
  if __name__ == "__main__":
880
  iface.launch()