File size: 8,058 Bytes
d93b84f
b6d231f
de7d728
 
d93b84f
d0ea10b
3381189
 
 
 
 
 
d109810
3381189
ecbfde3
 
 
 
 
3381189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec190aa
3381189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec190aa
3381189
 
 
 
 
 
 
 
 
 
 
 
ecbfde3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

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)