ncnn

Форк
0
/
simplepose.cpp 
167 строк · 4.7 Кб
1
// Tencent is pleased to support the open source community by making ncnn available.
2
//
3
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4
//
5
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6
// in compliance with the License. You may obtain a copy of the License at
7
//
8
// https://opensource.org/licenses/BSD-3-Clause
9
//
10
// Unless required by applicable law or agreed to in writing, software distributed
11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
// specific language governing permissions and limitations under the License.
14

15
#include "net.h"
16

17
#include <algorithm>
18
#if defined(USE_NCNN_SIMPLEOCV)
19
#include "simpleocv.h"
20
#else
21
#include <opencv2/core/core.hpp>
22
#include <opencv2/highgui/highgui.hpp>
23
#include <opencv2/imgproc/imgproc.hpp>
24
#endif
25
#include <stdio.h>
26
#include <vector>
27

28
struct KeyPoint
29
{
30
    cv::Point2f p;
31
    float prob;
32
};
33

34
static int detect_posenet(const cv::Mat& bgr, std::vector<KeyPoint>& keypoints)
35
{
36
    ncnn::Net posenet;
37

38
    posenet.opt.use_vulkan_compute = true;
39

40
    // the simple baseline human pose estimation from gluon-cv
41
    // https://gluon-cv.mxnet.io/build/examples_pose/demo_simple_pose.html
42
    // mxnet model exported via
43
    //      pose_net.hybridize()
44
    //      pose_net.export('pose')
45
    // then mxnet2ncnn
46
    // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
47
    if (posenet.load_param("pose.param"))
48
        exit(-1);
49
    if (posenet.load_model("pose.bin"))
50
        exit(-1);
51

52
    int w = bgr.cols;
53
    int h = bgr.rows;
54

55
    ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, w, h, 192, 256);
56

57
    // transforms.ToTensor(),
58
    // transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
59
    // R' = (R / 255 - 0.485) / 0.229 = (R - 0.485 * 255) / 0.229 / 255
60
    // G' = (G / 255 - 0.456) / 0.224 = (G - 0.456 * 255) / 0.224 / 255
61
    // B' = (B / 255 - 0.406) / 0.225 = (B - 0.406 * 255) / 0.225 / 255
62
    const float mean_vals[3] = {0.485f * 255.f, 0.456f * 255.f, 0.406f * 255.f};
63
    const float norm_vals[3] = {1 / 0.229f / 255.f, 1 / 0.224f / 255.f, 1 / 0.225f / 255.f};
64
    in.substract_mean_normalize(mean_vals, norm_vals);
65

66
    ncnn::Extractor ex = posenet.create_extractor();
67

68
    ex.input("data", in);
69

70
    ncnn::Mat out;
71
    ex.extract("conv3_fwd", out);
72

73
    // resolve point from heatmap
74
    keypoints.clear();
75
    for (int p = 0; p < out.c; p++)
76
    {
77
        const ncnn::Mat m = out.channel(p);
78

79
        float max_prob = 0.f;
80
        int max_x = 0;
81
        int max_y = 0;
82
        for (int y = 0; y < out.h; y++)
83
        {
84
            const float* ptr = m.row(y);
85
            for (int x = 0; x < out.w; x++)
86
            {
87
                float prob = ptr[x];
88
                if (prob > max_prob)
89
                {
90
                    max_prob = prob;
91
                    max_x = x;
92
                    max_y = y;
93
                }
94
            }
95
        }
96

97
        KeyPoint keypoint;
98
        keypoint.p = cv::Point2f(max_x * w / (float)out.w, max_y * h / (float)out.h);
99
        keypoint.prob = max_prob;
100

101
        keypoints.push_back(keypoint);
102
    }
103

104
    return 0;
105
}
106

107
static void draw_pose(const cv::Mat& bgr, const std::vector<KeyPoint>& keypoints)
108
{
109
    cv::Mat image = bgr.clone();
110

111
    // draw bone
112
    static const int joint_pairs[16][2] = {
113
        {0, 1}, {1, 3}, {0, 2}, {2, 4}, {5, 6}, {5, 7}, {7, 9}, {6, 8}, {8, 10}, {5, 11}, {6, 12}, {11, 12}, {11, 13}, {12, 14}, {13, 15}, {14, 16}
114
    };
115

116
    for (int i = 0; i < 16; i++)
117
    {
118
        const KeyPoint& p1 = keypoints[joint_pairs[i][0]];
119
        const KeyPoint& p2 = keypoints[joint_pairs[i][1]];
120

121
        if (p1.prob < 0.2f || p2.prob < 0.2f)
122
            continue;
123

124
        cv::line(image, p1.p, p2.p, cv::Scalar(255, 0, 0), 2);
125
    }
126

127
    // draw joint
128
    for (size_t i = 0; i < keypoints.size(); i++)
129
    {
130
        const KeyPoint& keypoint = keypoints[i];
131

132
        fprintf(stderr, "%.2f %.2f = %.5f\n", keypoint.p.x, keypoint.p.y, keypoint.prob);
133

134
        if (keypoint.prob < 0.2f)
135
            continue;
136

137
        cv::circle(image, keypoint.p, 3, cv::Scalar(0, 255, 0), -1);
138
    }
139

140
    cv::imshow("image", image);
141
    cv::waitKey(0);
142
}
143

144
int main(int argc, char** argv)
145
{
146
    if (argc != 2)
147
    {
148
        fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
149
        return -1;
150
    }
151

152
    const char* imagepath = argv[1];
153

154
    cv::Mat m = cv::imread(imagepath, 1);
155
    if (m.empty())
156
    {
157
        fprintf(stderr, "cv::imread %s failed\n", imagepath);
158
        return -1;
159
    }
160

161
    std::vector<KeyPoint> keypoints;
162
    detect_posenet(m, keypoints);
163

164
    draw_pose(m, keypoints);
165

166
    return 0;
167
}
168

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.