int main(int argc, char** argv) {
tf_bin_load();
Mat src = imread("D:\\okay_workspace\\project_data\\test01.jpg");
//resizeWindow();
//imshow("ori", src);
if (src.empty()) {
printf("load image error......");
}
//Net net=readNetFromTensorflow()
Net net = readNetFromCaffe(pbtxt_path, model_path);
//使用openvino inference_engine infer,default:DNN_BACKEND_OPENCV,DNN_BACKEND_INFERENCE_ENGINE
net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);//设置计算后台
net.setPreferableTarget(DNN_TARGET_CPU);//指定IOT设备运行环境
// image-->bob tensor
printf("start load peleeeNet ....\n");
//caffe python: blob = dnn.blobFromImage(img_ori, 0.017, (304, 304), (103.94, 116.78, 123.68))
Mat blob = blobFromImage(src, 0.017, Size(304, 304), Scalar(103.94, 116.78, 123.68),true,false,5);
net.setInput(blob);
Mat detection = net.forward();
//获取infer时间
vector<double>layerTimings;//
double freq = getTickFrequency() / 1000;
double time = net.getPerfProfile(layerTimings) / freq;//net.getPerfProfile(layerTimings)返回每一层infer时间
ostringstream ss;
ss << "inference:" << time << "ms";
putText(src, ss.str(), Point(60, 200), FONT_HERSHEY_COMPLEX, 3, Scalar(255, 0, 0), 2, 8);
//0:index,1:class_name,2:score,boxes(3,4,5,6)
// h,w
float threshold = 0.25;
Mat detectionMat(detection.size[2],detection.size[3],CV_32F,detection.ptr<float>());
//detectionMat = detectionMat.size[0];
for (int i = 0; i < detectionMat.rows; i++) {
float confidence = detectionMat.at<float>(i, 2);
if (confidence > threshold) {
rsize_t object_index = (rsize_t)detectionMat.at<float>(i,1);
//xmin,ymin,xmax,ymax
float xmin = detectionMat.at<float>(i, 3)*src.cols;
float ymin = detectionMat.at<float>(i, 4)*src.rows;
float xmax = detectionMat.at<float>(i, 5)*src.cols;
float ymax = detectionMat.at<float>(i, 6)*src.rows;
Rect object_box((int)xmin, (int)ymin, (int)(xmax-xmin),(int)(ymax-ymin));
rectangle(src, object_box, Scalar(0, 255, 0), 2,8,0);
}
}
namedWindow("peleeNet_detection",0);
resizeWindow("peleeNet_detection",Size(800,1000));//h,w
imshow("peleeNet_detection", src);
waitKey(0);
destroyAllWindows();
return 0;
}