Spaces:
Build error
Build error
app.py
CHANGED
|
@@ -43,7 +43,20 @@ else:
|
|
| 43 |
init_detector = sys.modules['init_detector']
|
| 44 |
|
| 45 |
########## utils
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
|
| 48 |
dim = None
|
| 49 |
(h, w) = image.shape[:2]
|
|
@@ -89,7 +102,8 @@ def loading_model():
|
|
| 89 |
from mmdet.apis import set_random_seed
|
| 90 |
set_random_seed(0, deterministic=False)
|
| 91 |
cfg = Config.fromfile('./cfgsn.py')
|
| 92 |
-
checkpoint = "./200s.pth"
|
|
|
|
| 93 |
model1 = init_detector(cfg, checkpoint, device="cpu")
|
| 94 |
if model1.cfg.data.test['type'] == 'ConcatDataset':
|
| 95 |
model1.cfg.data.test.pipeline = model1.cfg.data.test['datasets'][0].pipeline
|
|
@@ -114,21 +128,22 @@ def main():
|
|
| 114 |
|
| 115 |
st.sidebar.write(" ------ ")
|
| 116 |
st.sidebar.info('Options')
|
| 117 |
-
fcolor = st.sidebar.selectbox('boundary color', ['red','green','blue'])
|
| 118 |
extra_postprocess = st.sidebar.checkbox('perform extra postprocessing',value=True)
|
|
|
|
|
|
|
| 119 |
# do_thresh_box = st.sidebar.checkbox('adaptive threshold')
|
| 120 |
|
| 121 |
if btnpredicrupload and f is not None:
|
| 122 |
tfile = tempfile.NamedTemporaryFile(delete=True)
|
| 123 |
tfile.write(f.read())
|
| 124 |
imgpath=tfile.name
|
| 125 |
-
run_app(imgpath ,fcolor,extra_post=extra_postprocess)
|
| 126 |
if submitted:
|
| 127 |
st.empty()
|
| 128 |
directory ='./imgs/'
|
| 129 |
pic = os.path.join(directory, option)
|
| 130 |
imgpath=directory+option
|
| 131 |
-
run_app(imgpath ,fcolor,extra_post=extra_postprocess)
|
| 132 |
# inp = st.text_input('t2','')
|
| 133 |
# if st.button('run'):
|
| 134 |
# subprocess.run(inp.split(' '))
|
|
@@ -165,13 +180,17 @@ def run_app(imgpath,fcolor,**kwargs):
|
|
| 165 |
ff= fixer(img,preds,img_metas,downsample_ratio)
|
| 166 |
polys= ff.polysfin3
|
| 167 |
if kwargs['extra_post']:
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
|
| 171 |
# with open('./0.json','r') as f:
|
| 172 |
# res = json.load(f)
|
| 173 |
################
|
| 174 |
-
mm =
|
| 175 |
## mm=image_resize(mm, width = 640)
|
| 176 |
#######
|
| 177 |
# mm=preds[1]*preds[0]
|
|
|
|
| 43 |
init_detector = sys.modules['init_detector']
|
| 44 |
|
| 45 |
########## utils
|
| 46 |
+
def draw_conts(im,conts,fcolor,stroke):
|
| 47 |
+
opacity = .3
|
| 48 |
+
im =im.copy()
|
| 49 |
+
# fcolor =fcolor if fcolor else get_random_color()
|
| 50 |
+
frontmm = np.full_like(im,fcolor,np.uint8)
|
| 51 |
+
mask = np.zeros_like(im)
|
| 52 |
+
for cont in conts:
|
| 53 |
+
cv2.drawContours(mask, [cont], -1, color=[int(opacity*255)]*3, thickness=cv2.FILLED)
|
| 54 |
+
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
|
| 55 |
+
rgba = np.dstack((frontmm, mask))
|
| 56 |
+
im = utilss.overlay_image_alpha(im,rgba)
|
| 57 |
+
# drawline
|
| 58 |
+
cv2.drawContours(im, conts, -1, color=fcolor, thickness=stroke)
|
| 59 |
+
return im
|
| 60 |
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
|
| 61 |
dim = None
|
| 62 |
(h, w) = image.shape[:2]
|
|
|
|
| 102 |
from mmdet.apis import set_random_seed
|
| 103 |
set_random_seed(0, deterministic=False)
|
| 104 |
cfg = Config.fromfile('./cfgsn.py')
|
| 105 |
+
# checkpoint = "./200s.pth"
|
| 106 |
+
checkpoint = "./epoch_100.pth"
|
| 107 |
model1 = init_detector(cfg, checkpoint, device="cpu")
|
| 108 |
if model1.cfg.data.test['type'] == 'ConcatDataset':
|
| 109 |
model1.cfg.data.test.pipeline = model1.cfg.data.test['datasets'][0].pipeline
|
|
|
|
| 128 |
|
| 129 |
st.sidebar.write(" ------ ")
|
| 130 |
st.sidebar.info('Options')
|
|
|
|
| 131 |
extra_postprocess = st.sidebar.checkbox('perform extra postprocessing',value=True)
|
| 132 |
+
fcolor = st.sidebar.selectbox('boundary color', ['red','green','blue'],index =0)
|
| 133 |
+
fstroke = st.sidebar.selectbox('boundary thickness', ['1','2','3'] ,index=1 )
|
| 134 |
# do_thresh_box = st.sidebar.checkbox('adaptive threshold')
|
| 135 |
|
| 136 |
if btnpredicrupload and f is not None:
|
| 137 |
tfile = tempfile.NamedTemporaryFile(delete=True)
|
| 138 |
tfile.write(f.read())
|
| 139 |
imgpath=tfile.name
|
| 140 |
+
run_app(imgpath ,fcolor,extra_post=extra_postprocess,fstroke=fstroke)
|
| 141 |
if submitted:
|
| 142 |
st.empty()
|
| 143 |
directory ='./imgs/'
|
| 144 |
pic = os.path.join(directory, option)
|
| 145 |
imgpath=directory+option
|
| 146 |
+
run_app(imgpath ,fcolor,extra_post=extra_postprocess,fstroke=fstroke)
|
| 147 |
# inp = st.text_input('t2','')
|
| 148 |
# if st.button('run'):
|
| 149 |
# subprocess.run(inp.split(' '))
|
|
|
|
| 180 |
ff= fixer(img,preds,img_metas,downsample_ratio)
|
| 181 |
polys= ff.polysfin3
|
| 182 |
if kwargs['extra_post']:
|
| 183 |
+
try:
|
| 184 |
+
polys =find_polys4(ff)
|
| 185 |
+
except:
|
| 186 |
+
print('error polys4')
|
| 187 |
+
polys= ff.polysfin3
|
| 188 |
|
| 189 |
|
| 190 |
# with open('./0.json','r') as f:
|
| 191 |
# res = json.load(f)
|
| 192 |
################
|
| 193 |
+
mm = draw_conts(img,[poly2cont(p) for p in polys],fcolor,stroke=int(kwargs['fstroke']))
|
| 194 |
## mm=image_resize(mm, width = 640)
|
| 195 |
#######
|
| 196 |
# mm=preds[1]*preds[0]
|