module.py 5.5 KB
Newer Older
C
chenjian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# copyright (c) 2020 PaddlePaddle 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 argparse
import json
import os
import time
from typing import Union

import cv2
import numpy as np
jm_12138's avatar
jm_12138 已提交
22 23 24 25 26 27 28

from .det_keypoint_unite_infer import predict_with_given_det
from .infer import Detector
from .keypoint_infer import KeyPointDetector
from .preprocess import base64_to_cv2
from .preprocess import decode_image
from .visualize import visualize_pose
C
chenjian 已提交
29 30 31 32 33 34

from paddlehub.module.module import moduleinfo
from paddlehub.module.module import runnable
from paddlehub.module.module import serving


C
fix  
chenjian 已提交
35 36 37 38 39 40
@moduleinfo(
    name="pp-tinypose",
    type="CV/keypoint_detection",
    author="paddlepaddle",
    author_email="",
    summary="PP-TinyPose is a real-time keypoint detection model optimized by PaddleDetecion for mobile devices.",
jm_12138's avatar
jm_12138 已提交
41
    version="1.1.0")
C
chenjian 已提交
42 43 44 45 46 47 48
class PP_TinyPose:
    """
    PP-TinyPose Model.
    """

    def __init__(self):
        self.det_model_dir = os.path.join(self.directory, 'model/picodet_s_320_coco_lcnet/')
C
chenjian 已提交
49
        self.keypoint_model_dir = os.path.join(self.directory, 'model/tinypose_256x192/')
C
chenjian 已提交
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
        self.detector = Detector(self.det_model_dir)
        self.topdown_keypoint_detector = KeyPointDetector(self.keypoint_model_dir)

    def predict(self,
                img: Union[str, np.ndarray],
                save_path: str = "pp_tinypose_output",
                visualization: bool = False,
                use_gpu=False):
        if use_gpu:
            device = 'GPU'
        else:
            device = 'CPU'
        if self.detector.device != device:
            self.detector = Detector(self.det_model_dir, device=device)
            self.topdown_keypoint_detector = KeyPointDetector(self.keypoint_model_dir, device=device)

        self.visualization = visualization
        store_res = []

        # Decode image in advance in det + pose prediction
        image, _ = decode_image(img, {})
        results = self.detector.predict_image([image], visual=False)
        results = self.detector.filter_box(results, 0.5)
        if results['boxes_num'] > 0:
            keypoint_res = predict_with_given_det(image, results, self.topdown_keypoint_detector, 1, False)
jm_12138's avatar
jm_12138 已提交
75
            save_name = img if isinstance(img, str) else os.path.join(save_path, (str(time.time()) + '.png'))
C
chenjian 已提交
76 77 78 79 80
            store_res.append(
                [save_name, keypoint_res['bbox'], [keypoint_res['keypoint'][0], keypoint_res['keypoint'][1]]])
            if not os.path.exists(save_path):
                os.makedirs(save_path)
            if self.visualization:
jm_12138's avatar
jm_12138 已提交
81 82
                if isinstance(img, np.ndarray):
                    cv2.imwrite(save_name, img)
C
chenjian 已提交
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
                visualize_pose(save_name, keypoint_res, visual_thresh=0.5, save_dir=save_path)
        return store_res

    @serving
    def serving_method(self, images: list, **kwargs):
        """
        Run as a service.
        """
        images_decode = [base64_to_cv2(image) for image in images]
        results = self.predict(img=images_decode[0], **kwargs)
        results = json.dumps(results)
        return results

    @runnable
    def run_cmd(self, argvs: list):
        """
        Run as a command.
        """
        self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
                                              prog='hub run {}'.format(self.name),
                                              usage='%(prog)s',
                                              add_help=True)
        self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
        self.arg_config_group = self.parser.add_argument_group(
            title="Config options", description="Run configuration for controlling module behavior, not required.")
        self.add_module_config_arg()
        self.add_module_input_arg()
        args = self.parser.parse_args(argvs)
        results = self.predict(img=args.input_path,
                               save_path=args.output_dir,
                               visualization=args.visualization,
                               use_gpu=args.use_gpu)

        return results

    def add_module_config_arg(self):
        """
        Add the command config options.
        """

        self.arg_config_group.add_argument('--output_dir',
                                           type=str,
                                           default='pp_tinypose_output',
                                           help="The directory to save output images.")
        self.arg_config_group.add_argument('--visualization',
                                           type=bool,
                                           default=True,
                                           help="whether to save output as images.")

        self.arg_config_group.add_argument('--use_gpu', action='store_true', help="use GPU or not")

    def add_module_input_arg(self):
        """
        Add the command input options.
        """
        self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")