visualizer.py 6.4 KB
Newer Older
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
# Copyright (c) 2022 VisualDL Authors. All Rights Reserve.
#
# 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 numpy as np

__all__ = [
    'visualize_detection', 'visualize_keypoint_detection',
    'visualize_face_detection', 'visualize_face_alignment',
    'visualize_segmentation', 'visualize_matting', 'visualize_ocr',
    'visualize_headpose'
]


def visualize_detection(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
30 31
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    boxes = np.array(data['boxes'])
    scores = np.array(data['scores'])
    label_ids = np.array(data['label_ids'])
    masks = np.array(data['masks'])
    contain_masks = data['contain_masks']
    detection_result = fd.C.vision.DetectionResult()
    detection_result.boxes = boxes
    detection_result.scores = scores
    detection_result.label_ids = label_ids
    detection_result.masks = masks
    detection_result.contain_masks = contain_masks
    result = fd.vision.vis_detection(image, detection_result)
    return result


def visualize_keypoint_detection(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
52 53
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    keypoints = np.array(data['keypoints'])
    scores = np.array(data['scores'])
    num_joints = np.array(data['num_joints'])

    detection_result = fd.C.vision.KeyPointDetectionResult()
    detection_result.keypoints = keypoints
    detection_result.scores = scores
    detection_result.num_joints = num_joints

    result = fd.vision.vis_keypoint_detection(image, detection_result)
    return result


def visualize_face_detection(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
72 73
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    data = np.array(data['data'])
    scores = np.array(data['scores'])
    landmarks = np.array(data['landmarks'])
    landmarks_per_face = data['landmarks_per_face']

    detection_result = fd.C.vision.FaceDetectionResult()
    detection_result.data = data
    detection_result.scores = scores
    detection_result.landmarks = landmarks
    detection_result.landmarks_per_face = landmarks_per_face

    result = fd.vision.vis_face_detection(image, detection_result)
    return result


def visualize_face_alignment(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
94 95
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    landmarks = np.array(data['landmarks'])

    facealignment_result = fd.C.vision.FaceAlignmentResult()
    facealignment_result.landmarks = landmarks

    result = fd.vision.vis_face_alignment(image, facealignment_result)
    return result


def visualize_segmentation(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
110 111
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    label_ids = np.array(data['label_ids'])
    score_map = np.array(data['score_map'])
    shape = np.array(data['shape'])

    segmentation_result = fd.C.vision.SegmentationResult()
    segmentation_result.shape = shape
    segmentation_result.score_map = score_map
    segmentation_result.label_ids = label_ids

    result = fd.vision.vis_segmentation(image, segmentation_result)
    return result


def visualize_matting(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
130 131
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    alpha = np.array(data['alpha'])
    foreground = np.array(data['foreground'])
    contain_foreground = data['contain_foreground']
    shape = np.array(data['shape'])

    matting_result = fd.C.vision.MattingResult()
    matting_result.alpha = alpha
    matting_result.foreground = foreground
    matting_result.contain_foreground = contain_foreground
    matting_result.shape = shape

    result = fd.vision.vis_matting(image, matting_result)
    return result


def visualize_ocr(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
152 153
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    boxes = np.array(data['boxes'])
    text = np.array(data['text'])
    rec_scores = np.array(data['rec_scores'])
    cls_scores = np.array(data['cls_scores'])
    cls_labels = data['cls_labels']

    ocr_result = fd.C.vision.OCRResult()
    ocr_result.boxes = boxes
    ocr_result.text = text
    ocr_result.rec_scores = rec_scores
    ocr_result.cls_scores = cls_scores
    ocr_result.cls_labels = cls_labels

    result = fd.vision.vis_ppocr(image, ocr_result)
    return result


def visualize_headpose(image, data):
    try:
        import fastdeploy as fd
    except Exception:
        raise RuntimeError(
C
chenjian 已提交
176 177
            "fastdeploy is required for visualizing results,please refer to "
            "https://github.com/PaddlePaddle/FastDeploy to install fastdeploy")
178 179 180 181 182 183 184
    euler_angles = np.array(data['euler_angles'])

    headpose_result = fd.C.vision.HeadPoseResult()
    headpose_result.euler_angles = euler_angles

    result = fd.vision.vis_headpose(image, headpose_result)
    return result