#include"run.h" bool RUN::initConfig(const char* modelpath, int* beltregion, float threshold) { net = cv::dnn::readNetFromONNX(modelpath); x1 = beltregion[0]; y1 = beltregion[1]; x2 = beltregion[2]; y2 = beltregion[3]; threshold_ = threshold; return true; } int RUN::detect(std::vector& images) { try { cv::Rect roi(x1, y1, x2 - x1, y2 - y1); std::vector r_ims; for (int i = 0; i < 3; ++i) { std::vector splited; cv::Mat resized; cv::split(images[i](roi), splited); cv::resize(splited[2], resized, cv::Size(224, 224)); r_ims.emplace_back(resized); } cv::Mat input_image(cv::Size(224, 224), CV_32FC3); merge(r_ims, input_image); cv::Mat blob = cv::dnn::blobFromImage(input_image, 1 / 255.); net.setInput(blob); std::vector outputs; net.forward(outputs, net.getUnconnectedOutLayersNames()); float* data = (float*)outputs[0].data; if (data[0] > threshold_) { return 0;//运行 } else { return 1;//停止 } } catch (const std::exception& ex) { std::string errorMessage = "runModel-"; errorMessage += ex.what(); throw std::runtime_error(errorMessage); } }