|
| 1 | +// |
| 2 | +// Created by DefTruth on 2022/7/2. |
| 3 | +// |
| 4 | + |
| 5 | +#include "ncnn_face_parsing_bisenet.h" |
| 6 | + |
| 7 | +using ncnncv::NCNNFaceParsingBiSeNet; |
| 8 | + |
| 9 | +NCNNFaceParsingBiSeNet::NCNNFaceParsingBiSeNet(const std::string &_param_path, |
| 10 | + const std::string &_bin_path, |
| 11 | + unsigned int _num_threads, |
| 12 | + unsigned int _input_height, |
| 13 | + unsigned int _input_width) : |
| 14 | + BasicNCNNHandler(_param_path, _bin_path, _num_threads), |
| 15 | + input_height(_input_height), input_width(_input_width) |
| 16 | +{ |
| 17 | +} |
| 18 | + |
| 19 | +void NCNNFaceParsingBiSeNet::transform(const cv::Mat &mat, ncnn::Mat &in) |
| 20 | +{ |
| 21 | + cv::Mat mat_rs; |
| 22 | + cv::resize(mat, mat_rs, cv::Size(input_width, input_height)); |
| 23 | + // will do deepcopy inside ncnn |
| 24 | + in = ncnn::Mat::from_pixels(mat_rs.data, ncnn::Mat::PIXEL_BGR2RGB, input_width, input_height); |
| 25 | + in.substract_mean_normalize(mean_vals, norm_vals); |
| 26 | +} |
| 27 | + |
| 28 | +void NCNNFaceParsingBiSeNet::detect(const cv::Mat &mat, types::FaceParsingContent &content, |
| 29 | + bool minimum_post_process) |
| 30 | +{ |
| 31 | + if (mat.empty()) return; |
| 32 | + |
| 33 | + // 1. make input tensor |
| 34 | + ncnn::Mat input; |
| 35 | + this->transform(mat, input); |
| 36 | + // 2. inference & extract |
| 37 | + auto extractor = net->create_extractor(); |
| 38 | + extractor.set_light_mode(false); // default |
| 39 | + extractor.set_num_threads(num_threads); |
| 40 | + extractor.input("input", input); |
| 41 | + // 3. generate mask |
| 42 | + this->generate_mask(extractor, mat, content, minimum_post_process); |
| 43 | +} |
| 44 | + |
| 45 | +static inline uchar __argmax_find(float *mutable_ptr, const unsigned int &step) |
| 46 | +{ |
| 47 | + std::vector<float> logits(19, 0.f); |
| 48 | + for (unsigned int i = 0; i < 19; ++i) |
| 49 | + logits[i] = *(mutable_ptr + i * step); |
| 50 | + uchar label = 0; |
| 51 | + float max_logit = logits[0]; |
| 52 | + for (unsigned int i = 1; i < 19; ++i) |
| 53 | + { |
| 54 | + if (logits[i] > max_logit) |
| 55 | + { |
| 56 | + max_logit = logits[i]; |
| 57 | + label = (uchar) i; |
| 58 | + } |
| 59 | + } |
| 60 | + return label; |
| 61 | +} |
| 62 | + |
| 63 | +static const uchar part_colors[20][3] = { |
| 64 | + {255, 0, 0}, |
| 65 | + {255, 85, 0}, |
| 66 | + {255, 170, 0}, |
| 67 | + {255, 0, 85}, |
| 68 | + {255, 0, 170}, |
| 69 | + {0, 255, 0}, |
| 70 | + {85, 255, 0}, |
| 71 | + {170, 255, 0}, |
| 72 | + {0, 255, 85}, |
| 73 | + {0, 255, 170}, |
| 74 | + {0, 0, 255}, |
| 75 | + {85, 0, 255}, |
| 76 | + {170, 0, 255}, |
| 77 | + {0, 85, 255}, |
| 78 | + {0, 170, 255}, |
| 79 | + {255, 255, 0}, |
| 80 | + {255, 255, 85}, |
| 81 | + {255, 255, 170}, |
| 82 | + {255, 0, 255}, |
| 83 | + {255, 85, 255} |
| 84 | +}; |
| 85 | + |
| 86 | +void NCNNFaceParsingBiSeNet::generate_mask(ncnn::Extractor &extractor, const cv::Mat &mat, |
| 87 | + types::FaceParsingContent &content, |
| 88 | + bool minimum_post_process) |
| 89 | +{ |
| 90 | + ncnn::Mat output; |
| 91 | + extractor.extract("out", output); |
| 92 | +#ifdef LITENCNN_DEBUG |
| 93 | + BasicNCNNHandler::print_shape(output, "out"); |
| 94 | +#endif |
| 95 | + const unsigned int h = mat.rows; |
| 96 | + const unsigned int w = mat.cols; |
| 97 | + |
| 98 | + const unsigned int out_h = input_height; |
| 99 | + const unsigned int out_w = input_width; |
| 100 | + const unsigned int channel_step = out_h * out_w; |
| 101 | + |
| 102 | + float *output_ptr = (float *) output.data; |
| 103 | + std::vector<uchar> elements(channel_step, 0); // allocate |
| 104 | + for (unsigned int i = 0; i < channel_step; ++i) |
| 105 | + elements[i] = __argmax_find(output_ptr + i, channel_step); |
| 106 | + |
| 107 | + cv::Mat label(out_h, out_w, CV_8UC1, elements.data()); |
| 108 | + |
| 109 | + if (!minimum_post_process) |
| 110 | + { |
| 111 | + const uchar *label_ptr = label.data; |
| 112 | + cv::Mat color_mat(out_h, out_w, CV_8UC3, cv::Scalar(255, 255, 255)); |
| 113 | + for (unsigned int i = 0; i < color_mat.rows; ++i) |
| 114 | + { |
| 115 | + cv::Vec3b *p = color_mat.ptr<cv::Vec3b>(i); |
| 116 | + for (unsigned int j = 0; j < color_mat.cols; ++j) |
| 117 | + { |
| 118 | + if (label_ptr[i * out_w + j] == 0) continue; |
| 119 | + p[j][0] = part_colors[label_ptr[i * out_w + j]][0]; |
| 120 | + p[j][1] = part_colors[label_ptr[i * out_w + j]][1]; |
| 121 | + p[j][2] = part_colors[label_ptr[i * out_w + j]][2]; |
| 122 | + } |
| 123 | + } |
| 124 | + if (out_h != h || out_w != w) |
| 125 | + cv::resize(color_mat, color_mat, cv::Size(w, h)); |
| 126 | + cv::addWeighted(mat, 0.4, color_mat, 0.6, 0., content.merge); |
| 127 | + } |
| 128 | + if (out_h != h || out_w != w) cv::resize(label, label, cv::Size(w, h)); |
| 129 | + |
| 130 | + content.label = label; |
| 131 | + content.flag = true; |
| 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 | + |
0 commit comments