Unverified Commit ff20ea39 authored by Masahiro Ogawa's avatar Masahiro Ogawa Committed by GitHub
Browse files

Sort images and check the input image is assumed image format. (#246)

parent 3e3b4a5d
...@@ -15,15 +15,21 @@ import os ...@@ -15,15 +15,21 @@ import os
def test_single_image(model, img_name, out_dir, color_palette, opacity): def test_single_image(model, img_name, out_dir, color_palette, opacity):
# check img_name is an image file or not
assumed_imgformat = ('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')
if (not img_name.lower().endswith(assumed_imgformat)):
print(f"Skip {img_name} because it is not an image file.")
return
result = inference_segmentor(model, img_name) result = inference_segmentor(model, img_name)
# show the results # show the results
if hasattr(model, 'module'): if hasattr(model, 'module'):
model = model.module model = model.module
img = model.show_result(img_name, result, img = model.show_result(img_name, result,
palette=color_palette, palette=color_palette,
show=False, opacity=opacity) show=False, opacity=opacity)
# save the results # save the results
mmcv.mkdir_or_exist(out_dir) mmcv.mkdir_or_exist(out_dir)
out_path = osp.join(out_dir, osp.basename(img_name)) out_path = osp.join(out_dir, osp.basename(img_name))
...@@ -33,7 +39,8 @@ def test_single_image(model, img_name, out_dir, color_palette, opacity): ...@@ -33,7 +39,8 @@ def test_single_image(model, img_name, out_dir, color_palette, opacity):
def main(): def main():
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument('img', help='Image file or a directory contains images') parser.add_argument(
'img', help='Image file or a directory contains images')
parser.add_argument('config', help='Config file') parser.add_argument('config', help='Config file')
parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument('checkpoint', help='Checkpoint file')
parser.add_argument('--out', type=str, default="demo", help='out dir') parser.add_argument('--out', type=str, default="demo", help='out dir')
...@@ -58,13 +65,16 @@ def main(): ...@@ -58,13 +65,16 @@ def main():
model.CLASSES = checkpoint['meta']['CLASSES'] model.CLASSES = checkpoint['meta']['CLASSES']
else: else:
model.CLASSES = get_classes(args.palette) model.CLASSES = get_classes(args.palette)
# check arg.img is directory of a single image. # check arg.img is directory of a single image.
if osp.isdir(args.img): if osp.isdir(args.img):
for img in os.listdir(args.img): for img in sorted(os.listdir(args.img)):
test_single_image(model, osp.join(args.img, img), args.out, get_palette(args.palette), args.opacity) test_single_image(model, osp.join(args.img, img),
args.out, get_palette(args.palette), args.opacity)
else: else:
test_single_image(model, args.img, args.out, get_palette(args.palette), args.opacity) test_single_image(model, args.img, args.out,
get_palette(args.palette), args.opacity)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment