def segmap(image): ''' return: uint8 mask image, bg=black. If image is not float32 nor bgr-3channel image, convert it. But snet input img(float32) pixel range: 0 ~ 1 is mandatory. ''' def assert_img_range(img): assert (1.0 >= img).all(), img.max() assert (img >= 0.0).all(), img.min() return img def decategorize(mask): return iu.decategorize(mask, iu.rgb2wk_map) with tf.Session() as sess: snet_in = consts.snet_in('0.1.0', sess) snet_out = consts.snet_out('0.1.0', sess) def snet(img): return sess.run(snet_out, feed_dict={snet_in: img}) return fp.go( image, iu.channel3img, iu.float32, # preprocess assert_img_range, lambda img: segment(snet, img), iu.map_max_row, decategorize, iu.uint8, # postprocess )
def imgpath2mask(imgpath): return fp.go( imgpath, lambda path: io.load(path, io.NDARR), core.segmap, io.segmap2mask )
def gen_mask_all(self): imgpath = state.now_image() if imgpath is None: return None mask = fp.go(imgpath, lambda path: io.load(path, io.NDARR), core.segmap, io.segmap2mask) io.save(state.now_mask(), mask) self.update_gui() return mask
def mask2segmap(mask): ''' convert mask(gui, file) to segmap(snet output) mask: np.uint8, bgra, {fg=red, bg=transparent} segmap: np.uint8, bgr, b=g=r {fg=white, bg=black} ''' return fp.go(mask, lambda m4: np.sum(m4[:, :, :3], axis=-1, dtype=np.uint8), lambda m1: np.expand_dims(m1, axis=-1), lambda m1: cv2.merge( (m1, m1, m1)))
def test_gen_mask(): open_project('./fixture/real_proj/') expected = fp.go( './fixture/real_proj/images/bgr1.png', cv2.imread, core.segmap, io.segmap2mask ) main_window.gen_mask() actual = cv2.imread( './fixture/real_proj/masks/bgr1.png', cv2.IMREAD_UNCHANGED) #NOTE: rgba -> 4channel, wb -> 1ch.. assert np.array_equal(actual, expected)
def set_project(prj_dirpath): assert Path(prj_dirpath, config.IMGDIR).exists() assert Path(prj_dirpath, config.MASKDIR).exists() global img_paths, mask_paths, _cursor img_paths = fp.go( Path(prj_dirpath) / config.IMGDIR, fu.children, fp.filter(iu.is_img_file), fu.human_sorted, tuple) mask_paths = tuple( fp.map( fp.pipe(fu.replace1(config.IMGDIR, config.MASKDIR), Path, lambda p: p.with_suffix('.png'), str), img_paths)) _cursor = 0
def set_project(prj_dirpath): assert Path(prj_dirpath, consts.IMGDIR).exists() assert Path(prj_dirpath, consts.MASKDIR).exists() assert Path(prj_dirpath, consts.PREV_IMGDIR).exists() assert Path(prj_dirpath, consts.PREV_MASKDIR).exists() global img_paths, mask_paths,\ prev_img_paths, prev_mask_paths, _cursor img_paths = fp.go( Path(prj_dirpath) / consts.IMGDIR, fu.children, fp.filter(iu.is_img_file), fu.human_sorted, fp.map(lambda pstr: pstr.replace('\\','/')), tuple ) mask_paths = tuple(fp.map( fp.pipe( fu.replace1(consts.IMGDIR, consts.MASKDIR), Path, lambda p:p.with_suffix('.png'), str, lambda pstr: pstr.replace('\\','/') ), img_paths )) prev_img_paths = tuple(fp.map( fu.replace1(consts.IMGDIR, consts.PREV_IMGDIR), img_paths )) prev_mask_paths = tuple(fp.map( fu.replace1(consts.MASKDIR, consts.PREV_MASKDIR), mask_paths )) _cursor = 0