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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 75 76 77 78 79 80 81 82 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 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
| #include "RobotDetector.h"
#define IMAGE_LEN_F 640.0f #define IMAGE_LEN 640
RobotDetector::RobotDetector(){}
RobotDetector::~RobotDetector(){}
bool RobotDetector::init(std::string xml_file, double cof_threshold, double nms_area_threshold){ _xml_path = xml_file; _cof_threshold = cof_threshold; _nms_area_threshold = nms_area_threshold; Core ie; std::vector<std::string> availableDev = ie.GetAvailableDevices(); for(int i=0;i<availableDev.size();i++){ std::cout << "Openvino Supported Device Name: " << availableDev[i].c_str() << std::endl; } auto cnnNetwork = ie.ReadNetwork(_xml_path); InputsDataMap inputInfo(cnnNetwork.getInputsInfo()); InputInfo::Ptr& input = inputInfo.begin()->second; _input_name = inputInfo.begin()->first; input->setPrecision(Precision::FP32); input->getInputData()->setLayout(Layout::NCHW); ICNNNetwork::InputShapes inputShapes = cnnNetwork.getInputShapes(); SizeVector& inSizeVector = inputShapes.begin()->second; cnnNetwork.reshape(inputShapes); _outputinfo = OutputsDataMap(cnnNetwork.getOutputsInfo()); for (auto &output : _outputinfo) { output.second->setPrecision(Precision::FP32); } _network = ie.LoadNetwork(cnnNetwork, "CPU"); std::cout << "Running on CPU\n"; return true; }
bool RobotDetector::parseYolov5(const Blob::Ptr &blob, float cof_threshold, std::vector<cv::Rect>& o_rect, std::vector<float>& o_rect_cof, std::vector<int> &classId) { const int net_grid_h = static_cast<int>(blob->getTensorDesc().getDims()[2]); const int net_grid_w = static_cast<int>(blob->getTensorDesc().getDims()[3]); const int anchor_num = static_cast<int>(blob->getTensorDesc().getDims()[1]); const int item_size = 11;
if (net_grid_h != net_grid_w) { return false; } std::vector<int> anchors = get_anchors(net_grid_h); LockedMemory<const void> blobMapped = as<MemoryBlob>(blob)->rmap(); const float *output_blob = blobMapped.as<float *>(); int net_grid = net_grid_h; std::size_t gi = net_grid*item_size; std::size_t ggi = net_grid*gi; std::size_t anchor_n = anchor_num; for(int n = 0; n < anchor_num; ++n) for(int i = 0; i < net_grid; ++i) for(int j = 0; j < net_grid; ++j){ double box_prob = sigmoid(output_blob[n*ggi + i*gi + j*item_size + 4]); if(box_prob < cof_threshold) continue;
double x = output_blob[n*ggi + i*gi + j*item_size + 0]; double y = output_blob[n*ggi + i*gi + j*item_size + 1]; double w = output_blob[n*ggi + i*gi + j*item_size + 2]; double h = output_blob[n*ggi + i*gi + j*item_size+ 3];
double max_prob = 0; int idx=0; for(int t = 5; t < item_size; ++t){ double tp= sigmoid(output_blob[n*ggi + i*gi + j*item_size + t]); if(tp > max_prob){ max_prob = tp; idx = t-5; } }
float cof = box_prob * max_prob; if(cof < cof_threshold) continue;
x = (sigmoid(x)*2 - 0.5 + j)*IMAGE_LEN_F/net_grid; y = (sigmoid(y)*2 - 0.5 + i)*IMAGE_LEN_F/net_grid; w = pow(sigmoid(w)*2,2) * anchors[n*2]; h = pow(sigmoid(h)*2,2) * anchors[n*2 + 1];
double r_x = x - w/2; double r_y = y - h/2; cv::Rect rect = cv::Rect(round(r_x),round(r_y),round(w),round(h)); o_rect.push_back(rect); o_rect_cof.push_back(cof); classId.push_back(idx); } return true; }
bool RobotDetector::process_frame(cv::Mat& inframe, std::vector<Object>& detected_objects){ if(inframe.empty()){ std::cout << "无效图片输入" << std::endl; return false; } cv::Mat resize_img = letterBox(inframe); std::size_t img_size = 640 * 640;
InferRequest infer_request = _network.CreateInferRequest(); Blob::Ptr frameBlob = infer_request.GetBlob(_input_name); LockedMemory<void> blobMapped = as<MemoryBlob>(frameBlob)->wmap(); float* blob_data = blobMapped.as<float*>();
for(std::size_t row = 0;row <IMAGE_LEN;row++){ for(std::size_t col =0;col <IMAGE_LEN;col++){ for(std::size_t ch=0;ch<3;ch++){ blob_data[img_size*ch + row*IMAGE_LEN + col] = float(resize_img.at<cv::Vec3b>(row,col)[ch]/255.0f); } } } infer_request.Infer();
std::vector<cv::Rect> origin_rect; std::vector<float> origin_rect_cof; std::vector<int> classId;
std::vector<Blob::Ptr> blobs; for (auto &output : _outputinfo) { auto output_name = output.first; Blob::Ptr blob = infer_request.GetBlob(output_name); blobs.push_back(blob); } for(int i=0;i<blobs.size();i++){ float th = 0.5; if(i == 0) { th = 0.55; } if(i == 1) { th = 0.45; } if(i == 2) { th = 0.40; }
std::vector<cv::Rect> origin_rect_temp; std::vector<float> origin_rect_cof_temp; std::vector<int> classId_temp; parseYolov5(blobs[i], th, origin_rect_temp, origin_rect_cof_temp, classId); origin_rect.insert(origin_rect.end(), origin_rect_temp.begin(), origin_rect_temp.end()); origin_rect_cof.insert(origin_rect_cof.end(), origin_rect_cof_temp.begin(), origin_rect_cof_temp.end()); classId.insert(classId.end(), classId_temp.begin(), classId_temp.end()); }
std::vector<int> final_id; cv::dnn::NMSBoxes(origin_rect, origin_rect_cof, _cof_threshold, _nms_area_threshold, final_id); for(int i = 0; i < final_id.size(); ++i){ cv::Rect resize_rect= origin_rect[final_id[i]]; cv::Rect rawrect = detect2origin(resize_rect, _ratio, _topPad, _leftPad); detected_objects.push_back(Object{ origin_rect_cof[final_id[i]], classId[final_id[i]], rawrect }); } return true; }
double RobotDetector::sigmoid(double x){ return (1 / (1 + exp(-x))); }
const int anchorBig = 640/8; const int anchorMid = 640/16; const int anchorSml = 640/32;
const int aBig[6] = {10,13,16,30,32,23}; const int aMid[6] = {30,61,62,45,59,119}; const int aSml[6] = {116,90,156,198,373,326};
std::vector<int> RobotDetector::get_anchors(int net_grid) { std::vector<int> anchors(6); if(net_grid == anchorBig) { anchors.insert(anchors.begin(),aBig,aBig+6); } else if(net_grid == anchorMid) { anchors.insert(anchors.begin(),aMid,aMid+6); } else if(net_grid == anchorSml) { anchors.insert(anchors.begin(),aSml,aSml+6); } return anchors; }
cv::Mat RobotDetector::letterBox(cv::Mat src) { if(src.empty()) { std::cout <<"input image invalid" << std::endl; return cv::Mat();} int in_w = src.cols; int in_h = src.rows; int tar_w = 640; int tar_h = 640; _ratio = std::min(float(tar_h)/in_h,float(tar_w)/in_w); int inside_w = std::round(in_w * _ratio); int inside_h = std::round(in_h * _ratio); int pad_w = tar_w - inside_w; int pad_h = tar_h - inside_h; cv::Mat resize_img; cv::resize(src,resize_img,cv::Size(inside_w,inside_h)); cv::cvtColor(resize_img,resize_img,cv::COLOR_BGR2RGB); pad_w = pad_w / 2; pad_h = pad_h / 2; _topPad = int(std::round(pad_h - 0.1)); _btmPad = int(std::round(pad_h + 0.1)); _leftPad = int(std::round(pad_w - 0.1)); _rightPad = int(std::round(pad_w + 0.1));
cv::copyMakeBorder(resize_img,resize_img, _topPad, _btmPad, _leftPad, _rightPad,cv::BORDER_CONSTANT,cv::Scalar(114,114,114)); return resize_img; }
cv::Rect RobotDetector::detect2origin(const cv::Rect &det_rect, float rate_to, int top, int left) { int inside_x = det_rect.x - left; int inside_y = det_rect.y - top; int ox = std::round(float(inside_x)/rate_to); int oy = std::round(float(inside_y)/rate_to); int ow = std::round(float(det_rect.width)/rate_to); int oh = std::round(float(det_rect.height)/rate_to);
cv::Rect origin_rect(ox,oy,ow,oh); return origin_rect; }
|