segmentation.py 5.9 KB
Newer Older
W
wangguanzhong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. 
#   
# Licensed under the Apache License, Version 2.0 (the "License");   
# you may not use this file except in compliance with the License.  
# You may obtain a copy of the License at   
#   
#     http://www.apache.org/licenses/LICENSE-2.0    
#   
# Unless required by applicable law or agreed to in writing, software   
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
# See the License for the specific language governing permissions and   
# limitations under the License.

import os
import math
import glob
import json
from collections import defaultdict

import cv2
import paddle
import numpy as np
from PIL import Image

from ppcv.utils.logger import setup_logger
from ppcv.core.workspace import register

from .base import OutputBaseOp

logger = setup_logger('SegOutput')


@register
class SegOutput(OutputBaseOp):
    def __init__(self, model_cfg, env_cfg):
        super().__init__(model_cfg, env_cfg)

    def __call__(self, inputs):
        total_res = []
        for input in inputs:
            fn, _, seg_map = input.values()
            res = dict(filename=fn, seg_map=seg_map.tolist())
            if self.save_res or self.return_res:
                total_res.append(res)

            if self.save_img:
                seg_map = get_pseudo_color_map(seg_map)
                file_name = os.path.split(fn)[-1]
                out_path = os.path.join(self.output_dir, file_name)
                seg_map.save(out_path)
                logger.info('Save output image to {}'.format(out_path))

        if self.save_res:
            res_file_name = 'seg_output.json'
            out_path = os.path.join(self.output_dir, res_file_name)
            with open(out_path, 'w') as f:
                json.dump(total_res, f)
            logger.info('Save output result to {}'.format(out_path))

        if self.return_res:
            return total_res


@register
class HumanSegOutput(OutputBaseOp):
    def __init__(self, model_cfg, env_cfg):
        super().__init__(model_cfg, env_cfg)

    def __call__(self, inputs):
        total_res = []
        for input in inputs:
            fn, img, seg_map = input.values()
            res = dict(filename=fn, seg_map=seg_map.tolist())
            if self.save_res or self.return_res:
                total_res.append(res)

            if self.save_img:
                alpha = seg_map[1]
                alpha = cv2.resize(alpha, (img.shape[1], img.shape[0]))
                alpha = (alpha * 255).astype('uint8')
                img = img[:, :, ::-1]
                res_img = np.concatenate(
                    [img, alpha[:, :, np.newaxis]], axis=-1)

                filename = os.path.basename(fn).split('.')[0]
                out_path = os.path.join(self.output_dir, filename + ".png")
                cv2.imwrite(out_path, res_img)
                logger.info('Save output image to {}'.format(out_path))

        if self.save_res:
            res_file_name = 'humanseg_output.json'
            out_path = os.path.join(self.output_dir, res_file_name)
            with open(out_path, 'w') as f:
                json.dump(total_res, f)
            logger.info('Save output result to {}'.format(out_path))

        if self.return_res:
            return total_res


@register
class MattingOutput(OutputBaseOp):
    def __init__(self, model_cfg, env_cfg):
        super().__init__(model_cfg, env_cfg)

    def __call__(self, inputs):
        total_res = []
        for input in inputs:
            fn, img, seg_map = input.values()
            res = dict(filename=fn, seg_map=seg_map.tolist())
            if self.save_res or self.return_res:
                total_res.append(res)

            if self.save_img:
                alpha = seg_map.squeeze()
                alpha = cv2.resize(alpha, (img.shape[1], img.shape[0]))
                alpha = (alpha * 255).astype('uint8')

                filename = os.path.basename(fn).split('.')[0]
                out_path = os.path.join(self.output_dir, filename + ".png")
                cv2.imwrite(out_path, alpha)
                logger.info('Save output image to {}'.format(out_path))

        if self.save_res:
            res_file_name = 'matting_output.json'
            out_path = os.path.join(self.output_dir, res_file_name)
            with open(out_path, 'w') as f:
                json.dump(total_res, f)
            logger.info('Save output result to {}'.format(out_path))

        if self.return_res:
            return total_res


def get_pseudo_color_map(pred, color_map=None):
    """
    Get the pseudo color image.

    Args:
        pred (numpy.ndarray): the origin predicted image.
        color_map (list, optional): the palette color map. Default: None,
            use paddleseg's default color map.

    Returns:
        (numpy.ndarray): the pseduo image.
    """
    pred_mask = Image.fromarray(pred.astype(np.uint8), mode='P')
    if color_map is None:
        color_map = get_color_map_list(256)
    pred_mask.putpalette(color_map)
    return pred_mask


def get_color_map_list(num_classes, custom_color=None):
    """
    Returns the color map for visualizing the segmentation mask,
    which can support arbitrary number of classes.

    Args:
        num_classes (int): Number of classes.
        custom_color (list, optional): Save images with a custom color map. Default: None, use paddleseg's default color map.

    Returns:
        (list). The color map.
    """

    num_classes += 1
    color_map = num_classes * [0, 0, 0]
    for i in range(0, num_classes):
        j = 0
        lab = i
        while lab:
            color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
            color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
            color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
            j += 1
            lab >>= 3
    color_map = color_map[3:]

    if custom_color:
        color_map[:len(custom_color)] = custom_color
    return color_map