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

Add sample images as alternative to upload

Browse files
app.py CHANGED
@@ -5,6 +5,10 @@ import numpy as np
5
  from PIL import Image
6
  import math
7
  from einops import rearrange
 
 
 
 
8
 
9
 
10
  def to_2tuple(x):
@@ -722,6 +726,14 @@ def upscale_image(image):
722
  return Image.fromarray(output_np)
723
 
724
 
 
 
 
 
 
 
 
 
725
  # Gradio interface using Blocks for better layout control
726
  def upscale_and_display(image):
727
  if image is None:
@@ -731,8 +743,23 @@ def upscale_and_display(image):
731
  upscaled = upscale_image(image)
732
  return upscaled
733
 
734
- # Custom CSS to make images fill containers while preserving aspect ratio
735
- css = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
736
  /* Target only the image display area, not the whole component */
737
  .image-container [data-testid="image"] {
738
  height: 500px !important;
@@ -746,42 +773,81 @@ css = """
746
  object-fit: contain !important;
747
  object-position: center !important;
748
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
749
  """
750
 
 
 
 
 
 
 
 
 
 
 
751
  with gr.Blocks(css=css, title="HAT Super-Resolution for Satellite Images") as iface:
752
  gr.Markdown("# HAT Super-Resolution for Satellite Images")
753
- gr.Markdown("Upload a satellite image to enhance its resolution by 4x using a fine-tuned HAT model. The output shows the full high-resolution enhanced image.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754
 
755
  with gr.Row():
756
- with gr.Column():
757
- input_image = gr.Image(
758
- type="pil",
759
- label="Input Satellite Image",
760
- elem_classes="image-container",
761
- sources=["upload"],
762
- height=500,
763
- width=500,
764
- show_download_button=False
765
- )
766
-
767
- with gr.Column():
768
- output_image = gr.Image(
769
- type="pil",
770
- label="Enhanced Output (4x Super-Resolution)",
771
- elem_classes="image-container",
772
- interactive=False,
773
- height=500,
774
- width=500,
775
- show_download_button=True
776
- )
777
 
778
  submit_btn = gr.Button("Enhance Image", variant="primary")
779
 
780
- submit_btn.click(
781
- fn=upscale_and_display,
782
- inputs=input_image,
783
- outputs=output_image
784
- )
 
785
 
786
  if __name__ == "__main__":
787
  iface.launch()
 
5
  from PIL import Image
6
  import math
7
  from einops import rearrange
8
+ import os
9
+ import glob
10
+ import base64
11
+ from io import BytesIO
12
 
13
 
14
  def to_2tuple(x):
 
726
  return Image.fromarray(output_np)
727
 
728
 
729
+ # Get sample images
730
+ def get_sample_images():
731
+ sample_dir = "sample_images"
732
+ if os.path.exists(sample_dir):
733
+ image_files = glob.glob(os.path.join(sample_dir, "*.png")) + glob.glob(os.path.join(sample_dir, "*.jpg"))
734
+ return sorted(image_files)
735
+ return []
736
+
737
  # Gradio interface using Blocks for better layout control
738
  def upscale_and_display(image):
739
  if image is None:
 
743
  upscaled = upscale_image(image)
744
  return upscaled
745
 
746
+ def select_sample_image(image_path):
747
+ if image_path:
748
+ return Image.open(image_path)
749
+ return None
750
+
751
+ def image_to_base64(image_path):
752
+ """Convert image to base64 data URL for CSS background"""
753
+ img = Image.open(image_path)
754
+ img.thumbnail((120, 120), Image.Resampling.LANCZOS)
755
+ buffer = BytesIO()
756
+ img.save(buffer, format='PNG')
757
+ img_str = base64.b64encode(buffer.getvalue()).decode()
758
+ return f"data:image/png;base64,{img_str}"
759
+
760
+ # Generate CSS with base64 images
761
+ def generate_css():
762
+ base_css = """
763
  /* Target only the image display area, not the whole component */
764
  .image-container [data-testid="image"] {
765
  height: 500px !important;
 
773
  object-fit: contain !important;
774
  object-position: center !important;
775
  }
776
+
777
+ /* Sample image buttons with background images */
778
+ .sample-image-btn {
779
+ height: 120px !important;
780
+ width: 120px !important;
781
+ background-size: cover !important;
782
+ background-position: center !important;
783
+ border: 2px solid #ddd !important;
784
+ border-radius: 8px !important;
785
+ cursor: pointer !important;
786
+ transition: border-color 0.2s !important;
787
+ margin: 5px !important;
788
+ }
789
+
790
+ .sample-image-btn:hover {
791
+ border-color: #007acc !important;
792
+ }
793
  """
794
 
795
+ # Add background images for each sample
796
+ sample_images = get_sample_images()
797
+ for i, img_path in enumerate(sample_images):
798
+ base64_img = image_to_base64(img_path)
799
+ base_css += f"#sample_btn_{i} {{ background-image: url('{base64_img}'); }}\n"
800
+
801
+ return base_css
802
+
803
+ css = generate_css()
804
+
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()
811
+ sample_buttons = []
812
+ if sample_images:
813
+ gr.Markdown("**Sample Images (click to select):**")
814
+ with gr.Row():
815
+ for i, img_path in enumerate(sample_images):
816
+ btn = gr.Button(
817
+ "",
818
+ elem_id=f"sample_btn_{i}",
819
+ elem_classes="sample-image-btn"
820
+ )
821
+ sample_buttons.append((btn, img_path))
822
 
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,
830
+ width=500
831
+ )
832
+
833
+ output_image = gr.Image(
834
+ type="pil",
835
+ label="Enhanced Output (4x)",
836
+ elem_classes="image-container",
837
+ interactive=False,
838
+ height=500,
839
+ width=500,
840
+ show_download_button=True
841
+ )
 
 
 
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()
sample_images/image_0007.png ADDED
sample_images/image_0019.png ADDED
sample_images/image_3510.png ADDED
sample_images/image_6862.png ADDED
sample_images/image_6899.png ADDED