visualizer.py 2.5 KB
Newer Older
D
dengkaipeng 已提交
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
# Copyright (c) 2020 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.

from __future__ import division
from __future__ import print_function

import numpy as np
from PIL import Image, ImageDraw

import logging
logger = logging.getLogger(__name__)

__all__ = ['draw_bbox']


D
dengkaipeng 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
def color_map(num_classes):
    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 = np.array(color_map).reshape(-1, 3)
    return color_map


D
dengkaipeng 已提交
42 43 44 45 46 47 48 49 50 51 52 53
def draw_bbox(image, catid2name, bboxes, threshold):
    """
    Draw bbox on image
    """
    bboxes = np.array(bboxes)
    if bboxes.shape[1] != 6:
        logger.info("No bbox detect")
        return image

    draw = ImageDraw.Draw(image)

    catid2color = {}
D
dengkaipeng 已提交
54
    color_list = color_map(len(catid2name))
D
dengkaipeng 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    for bbox in bboxes:
        catid, score, xmin, ymin, xmax, ymax = bbox

        if score < threshold:
            continue

        if catid not in catid2color:
            idx = np.random.randint(len(color_list))
            catid2color[catid] = color_list[idx]
        color = tuple(catid2color[catid])

        # draw bbox
        draw.line(
            [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
             (xmin, ymin)],
            width=2,
            fill=color)
72 73
        logger.info("detect {} at {} score: {:.2f}".format(catid2name[int(
            catid)], [xmin, ymin, xmax, ymax], score))
D
dengkaipeng 已提交
74 75 76 77 78 79 80 81 82

        # draw label
        text = "{} {:.2f}".format(catid2name[catid], score)
        tw, th = draw.textsize(text)
        draw.rectangle(
            [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
        draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))

    return image