image_demo.py 2.78 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
zhe chen's avatar
zhe chen committed
2
3
import os
import os.path as osp
4
5
from argparse import ArgumentParser

zhe chen's avatar
zhe chen committed
6
import cv2
7
import mmcv
zhe chen's avatar
zhe chen committed
8
9
import mmcv_custom  # noqa: F401,F403
import mmseg_custom  # noqa: F401,F403
10
from mmcv.runner import load_checkpoint
zhe chen's avatar
zhe chen committed
11
from mmseg.apis import inference_segmentor, init_segmentor, show_result_pyplot
12
from mmseg.core import get_classes
zhe chen's avatar
zhe chen committed
13
from mmseg.core.evaluation import get_palette
14
15
16


def test_single_image(model, img_name, out_dir, color_palette, opacity):
17
18
19
    # 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)):
zhe chen's avatar
zhe chen committed
20
        print(f'Skip {img_name} because it is not an image file.')
21
22
        return

23
    result = inference_segmentor(model, img_name)
24

25
26
27
28
29
30
    # show the results
    if hasattr(model, 'module'):
        model = model.module
    img = model.show_result(img_name, result,
                            palette=color_palette,
                            show=False, opacity=opacity)
31

32
33
34
35
    # save the results
    mmcv.mkdir_or_exist(out_dir)
    out_path = osp.join(out_dir, osp.basename(img_name))
    cv2.imwrite(out_path, img)
zhe chen's avatar
zhe chen committed
36
    print(f'Result is save at {out_path}')
37
38
39
40


def main():
    parser = ArgumentParser()
41
42
    parser.add_argument(
        'img', help='Image file or a directory contains images')
43
44
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
zhe chen's avatar
zhe chen committed
45
    parser.add_argument('--out', type=str, default='demo', help='out dir')
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--palette',
        default='ade20k',
        choices=['ade20k', 'cityscapes', 'cocostuff'],
        help='Color palette used for segmentation map')
    parser.add_argument(
        '--opacity',
        type=float,
        default=0.5,
        help='Opacity of painted segmentation map. In (0, 1] range.')
    args = parser.parse_args()

    # build the model from a config file and a checkpoint file
    model = init_segmentor(args.config, checkpoint=None, device=args.device)
    checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
    if 'CLASSES' in checkpoint.get('meta', {}):
        model.CLASSES = checkpoint['meta']['CLASSES']
    else:
        model.CLASSES = get_classes(args.palette)
67

68
69
    # check arg.img is directory of a single image.
    if osp.isdir(args.img):
70
71
72
        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)
73
    else:
74
75
76
        test_single_image(model, args.img, args.out,
                          get_palette(args.palette), args.opacity)

77
78

if __name__ == '__main__':
79
    main()