admin
2025-04-25 38501ea276d66487f3ff6cce1d3b07824b42abf6
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
#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<cv::Mat>& images)
{      
    try
    {
        cv::Rect roi(x1, y1, x2 - x1, y2 - y1);        
        std::vector<cv::Mat> r_ims;
        for (int i = 0; i < 3; ++i) { 
            std::vector<cv::Mat> 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<cv::Mat> 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);
    }
    
}