JunchuanYu commited on
Commit
3381189
1 Parent(s): ee39664

Upload run.py

Browse files
Files changed (1) hide show
  1. run.py +147 -0
run.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os,glob,h5py,time
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import numpy as np
5
+ from osgeo import gdal
6
+ from matplotlib import pyplot as plt
7
+ from tqdm import tqdm
8
+
9
+ title=(""" # <p align="center"> Remote Sensing Tile Dataset Creator 🛰️ <b>
10
+ ### <p align="center"> yujunchuan (yujunchuna@mail.cgs.gov.cn)<b>""")
11
+
12
+ with gr.Blocks(theme=gr.themes.Soft(),css="footer {visibility: hidden}") as demo:
13
+ # dtype=gr.State(value=[])
14
+ fullarray=gr.State(value=None)
15
+ cropdata=gr.State(value=None)
16
+ boriginal_img=gr.State(value=None)
17
+ boriginal_lab=gr.State(value=None)
18
+ gr.Markdown(title)
19
+
20
+
21
+ with gr.Tab("Basic Mode"):
22
+
23
+ with gr.Row():
24
+ with gr.Column(scale=50):
25
+ # input_img=gr.Textbox(label='Input image path' )
26
+ # input_lab=gr.Textbox(label='Input mask path')
27
+ input_img=gr.File(label='Input image path',height=100,scale=2)
28
+ outpath=gr.Textbox(label='Output path',lines=2)
29
+ with gr.Column(scale=50):
30
+ with gr.Row():
31
+ datatype=gr.Radio(['Tiff','Hdf5'],interactive=True, type="value",value='Tiff',label='Data type')
32
+ with gr.Row():
33
+ # with gr.Column():
34
+ check_button = gr.Button("Check file")
35
+ with gr.Row():
36
+ # with gr.Column():
37
+ result=gr.Textbox(label='Logging info',lines=2,show_label=True)
38
+
39
+ with gr.Row(equal_height=True):
40
+ with gr.Column():
41
+ with gr.Row(equal_height=True):
42
+ with gr.Column(scale=50):
43
+ with gr.Row():
44
+ with gr.Column(scale=50):
45
+ clipmode=gr.Radio(['Sequential','Random'],interactive=True, type="value",value='Sequential',label='Clip mode')
46
+ with gr.Column(scale=50):
47
+ npatch=gr.Number(label="N patches",value=50,precision=0,interactive=True)
48
+ with gr.Row():
49
+ thresh = gr.Slider(minimum=0, maximum=1, value=0, step=0.1, interactive=True, label="threshhold")
50
+ with gr.Column(scale=50):
51
+ with gr.Row(equal_height=True):
52
+ with gr.Column(scale=50):
53
+ cropsize = gr.Number(label="Crop size",value=256,precision=0,interactive=True)
54
+ run_button = gr.Button("Start",size='lg',scale=2,variant="primary")
55
+ with gr.Column(scale=50):
56
+ stride = gr.Number(label="Stride",value=256,precision=0,interactive=True)
57
+ # with gr.Row(equal_height=True):
58
+ save_button = gr.Button("Save",size='lg',scale=2,variant="primary")
59
+
60
+ with gr.Row(equal_height=True):
61
+ with gr.Column(scale=50):
62
+ original_img=gr.Image(label='Input image')
63
+ with gr.Column(scale=50):
64
+ original_lab=gr.Image(label='Input mask')
65
+ with gr.Column(scale=100):
66
+ show_pathes=gr.Image(label='Image tile')
67
+ with gr.Row(equal_height=True):
68
+ with gr.Column(scale=1):
69
+ start=gr.Number(label='Start number',value=0,precision=0,interactive=True)
70
+ with gr.Column(scale=1):
71
+ dstride=gr.Number(label='Show N files',value=10,precision=0,interactive=True)
72
+ with gr.Column(scale=2):
73
+ showtile=gr.Button('Show tile',size='lg',variant="primary")
74
+ tilecheck=gr.Button('Next tile',size='lg',variant="secondary")
75
+
76
+ with gr.Row():
77
+ example = gr.Examples(
78
+ examples=[['./image/128_d4_32.tif','Tiff']],
79
+ fn=read_img_file,
80
+ inputs=[input_img,datatype],
81
+ outputs=[fullarray,original_img,original_lab],
82
+ cache_examples=False)
83
+ ## BATCH MODE
84
+ with gr.Tab("Batch"):
85
+ with gr.Row(equal_height=True):
86
+ with gr.Column(scale=50):
87
+ binput_img=gr.File(label='Input image path',height=100,scale=3,file_count='multiple')
88
+ # with gr.Row():
89
+ with gr.Column(scale=50):
90
+ with gr.Row():
91
+ with gr.Column():
92
+ bdatatype=gr.Radio(['Tiff','Hdf5'],interactive=True, type="value",value='Tiff',label='Data type')
93
+ with gr.Column(scale=20):
94
+ boutpath=gr.Textbox(label='Output path',lines=1)
95
+ with gr.Row():
96
+ bcheck_button = gr.Button("Check file")
97
+ # with gr.Row():
98
+
99
+ with gr.Row(equal_height=True):
100
+ with gr.Column():
101
+ with gr.Row(equal_height=True):
102
+ with gr.Column(scale=50):
103
+ with gr.Row():
104
+ with gr.Column(scale=50):
105
+ bclipmode=gr.Radio(['Sequential','Random'],interactive=True, type="value",value='Sequential',label='Clip mode')
106
+ with gr.Column(scale=50):
107
+ bnpatch=gr.Number(label="N patches",value=50,precision=0,interactive=True)
108
+ with gr.Row():
109
+ bthresh = gr.Slider(minimum=0, maximum=1, value=0, step=0.1, interactive=True, label="threshhold")
110
+
111
+ with gr.Column(scale=50):
112
+ with gr.Row():
113
+ with gr.Column(scale=50):
114
+ bcropsize = gr.Number(label="Crop size",value=256,precision=0,interactive=True)
115
+ with gr.Column(scale=50):
116
+ bstride = gr.Number(label="Stride",value=256,precision=0,interactive=True)
117
+ with gr.Row():
118
+ brun_button = gr.Button("Start",size='lg',scale=2,variant="primary")
119
+
120
+ with gr.Row():
121
+ with gr.Column(scale=50):
122
+ boriginal_img=gr.Image(label='Input image')
123
+ with gr.Column(scale=50):
124
+ boriginal_lab=gr.Image(label='Input mask')
125
+ with gr.Column(scale=100):
126
+ bresult=gr.Textbox(label='Logging info',lines=8,show_label=True)
127
+
128
+ with gr.Row():
129
+ example = gr.Examples(
130
+ examples=[[glob.glob('./image/*.tif'),'Tiff','./image']],
131
+ fn=batch_check_file,
132
+ inputs=[binput_img,bdatatype,boutpath],
133
+ outputs=[bresult],
134
+ cache_examples=False)
135
+
136
+ input_img.change(read_img_file,[input_img,datatype],[fullarray,original_img,original_lab])
137
+ check_button.click(check_file,[fullarray,outpath],result)
138
+ run_button.click(data_crop,[fullarray,cropsize,stride,clipmode,npatch,thresh],[cropdata,result])
139
+ save_button.click(save_tile,[cropdata,outpath],result)
140
+ showtile.click(show_data,cropdata,[show_pathes,start])
141
+ tilecheck.click(show_data,[cropdata,start,dstride],[show_pathes,start])
142
+
143
+ bcheck_button.click(batch_check_file,[binput_img,bdatatype,boutpath],bresult)
144
+ brun_button.click(batch_clip_data,[binput_img,bdatatype,bcropsize,bstride,bclipmode,npatch,bthresh,boutpath],[boriginal_img,boriginal_lab,bresult],show_progress=True)
145
+
146
+ demo.queue()
147
+ demo.launch(debug=True,show_api=False,show_tips=False)