RS-Tile-Creator / run.py
JunchuanYu's picture
Update run.py
ec190aa
import os
filepath='./lib/geoai_GDAL-3.4.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl'
os.system('pip install {}'.format(filepath))
import os,glob,h5py,time,sys
import gradio as gr
from PIL import Image
import numpy as np
from osgeo import gdal
from matplotlib import pyplot as plt
from tqdm import tqdm
from huggingface_hub import hf_hub_download
token = os.environ['HUB_TOKEN']
loc =hf_hub_download(repo_id="JunchuanYu/file_for_rs-tile-creator", filename="utils.py",repo_type="dataset",local_dir='.',token=token)
sys.path.append(loc)
from utils import *
title=(""" # <p align="center"> Remote Sensing Tile Dataset Creator 🛰️ <b>
### <p align="center"> yujunchuan (yujunchuna@mail.cgs.gov.cn)<b>""")
with gr.Blocks(theme=gr.themes.Soft(),css="footer {visibility: hidden}") as demo:
# dtype=gr.State(value=[])
fullarray=gr.State(value=None)
cropdata=gr.State(value=None)
boriginal_img=gr.State(value=None)
boriginal_lab=gr.State(value=None)
gr.Markdown(title)
with gr.Tab("Basic Mode"):
with gr.Row():
with gr.Column(scale=50):
# input_img=gr.Textbox(label='Input image path' )
# input_lab=gr.Textbox(label='Input mask path')
input_img=gr.File(label='Input image path',height=100,scale=2)
outpath=gr.Textbox(label='Output path',lines=2)
with gr.Column(scale=50):
with gr.Row():
datatype=gr.Radio(['Tiff','Hdf5'],interactive=True, type="value",value='Tiff',label='Data type')
with gr.Row():
# with gr.Column():
check_button = gr.Button("Check file")
with gr.Row():
# with gr.Column():
result=gr.Textbox(label='Logging info',lines=2,show_label=True)
with gr.Row(equal_height=True):
with gr.Column():
with gr.Row(equal_height=True):
with gr.Column(scale=50):
with gr.Row():
with gr.Column(scale=50):
clipmode=gr.Radio(['Sequential','Random'],interactive=True, type="value",value='Sequential',label='Clip mode')
with gr.Column(scale=50):
npatch=gr.Number(label="N patches",value=50,precision=0,interactive=True)
with gr.Row():
thresh = gr.Slider(minimum=0, maximum=1, value=0, step=0.1, interactive=True, label="threshhold")
with gr.Column(scale=50):
with gr.Row(equal_height=True):
with gr.Column(scale=50):
cropsize = gr.Number(label="Crop size",value=256,precision=0,interactive=True)
run_button = gr.Button("Start",size='lg',scale=2,variant="primary")
with gr.Column(scale=50):
stride = gr.Number(label="Stride",value=256,precision=0,interactive=True)
# with gr.Row(equal_height=True):
save_button = gr.Button("Save",size='lg',scale=2,variant="primary")
with gr.Row(equal_height=True):
with gr.Column(scale=50):
original_img=gr.Image(label='Input image')
with gr.Column(scale=50):
original_lab=gr.Image(label='Input mask')
with gr.Column(scale=100):
show_pathes=gr.Image(label='Image tile')
with gr.Row(equal_height=True):
with gr.Column(scale=1):
start=gr.Number(label='Start number',value=0,precision=0,interactive=True)
with gr.Column(scale=1):
dstride=gr.Number(label='Show N files',value=10,precision=0,interactive=True)
with gr.Column(scale=2):
showtile=gr.Button('Show tile',size='lg',variant="primary")
tilecheck=gr.Button('Next tile',size='lg',variant="secondary")
with gr.Row():
example = gr.Examples(
examples=[['./image/128_d4_32.tif','Tiff']],
fn=read_img_file,
inputs=[input_img,datatype],
outputs=[fullarray,original_img,original_lab],
cache_examples=True)
## BATCH MODE
with gr.Tab("Batch"):
with gr.Row(equal_height=True):
with gr.Column(scale=50):
binput_img=gr.File(label='Input image path',height=100,scale=3,file_count='multiple')
# with gr.Row():
with gr.Column(scale=50):
with gr.Row():
with gr.Column():
bdatatype=gr.Radio(['Tiff','Hdf5'],interactive=True, type="value",value='Tiff',label='Data type')
with gr.Column(scale=20):
boutpath=gr.Textbox(label='Output path',lines=1)
with gr.Row():
bcheck_button = gr.Button("Check file")
# with gr.Row():
with gr.Row(equal_height=True):
with gr.Column():
with gr.Row(equal_height=True):
with gr.Column(scale=50):
with gr.Row():
with gr.Column(scale=50):
bclipmode=gr.Radio(['Sequential','Random'],interactive=True, type="value",value='Sequential',label='Clip mode')
with gr.Column(scale=50):
bnpatch=gr.Number(label="N patches",value=50,precision=0,interactive=True)
with gr.Row():
bthresh = gr.Slider(minimum=0, maximum=1, value=0, step=0.1, interactive=True, label="threshhold")
with gr.Column(scale=50):
with gr.Row():
with gr.Column(scale=50):
bcropsize = gr.Number(label="Crop size",value=256,precision=0,interactive=True)
with gr.Column(scale=50):
bstride = gr.Number(label="Stride",value=256,precision=0,interactive=True)
with gr.Row():
brun_button = gr.Button("Start",size='lg',scale=2,variant="primary")
with gr.Row():
with gr.Column(scale=50):
boriginal_img=gr.Image(label='Input image')
with gr.Column(scale=50):
boriginal_lab=gr.Image(label='Input mask')
with gr.Column(scale=100):
bresult=gr.Textbox(label='Logging info',lines=8,show_label=True)
with gr.Row():
example = gr.Examples(
examples=[[glob.glob('./image/*.tif'),'Tiff','./image']],
fn=batch_check_file,
inputs=[binput_img,bdatatype,boutpath],
outputs=[bresult],
cache_examples=True)
input_img.change(read_img_file,[input_img,datatype],[fullarray,original_img,original_lab])
check_button.click(check_file,[fullarray,outpath],result)
run_button.click(data_crop,[fullarray,cropsize,stride,clipmode,npatch,thresh],[cropdata,result])
save_button.click(save_tile,[cropdata,outpath],result)
showtile.click(show_data,cropdata,[show_pathes,start])
tilecheck.click(show_data,[cropdata,start,dstride],[show_pathes,start])
bcheck_button.click(batch_check_file,[binput_img,bdatatype,boutpath],bresult)
brun_button.click(batch_clip_data,[binput_img,bdatatype,bcropsize,bstride,bclipmode,npatch,bthresh,boutpath],[boriginal_img,boriginal_lab,bresult],show_progress=True)
demo.queue()
demo.launch(debug=False,show_api=False,show_tips=False)