OpenCV之YOLOv2-tiny目标检测( 三 )

(i, (int)objectClass + probability_index);if (confidence > confidenceThreshold){printf("confide:%.2f\n", confidence);float x = detectionMat.at(i, 0);float y = detectionMat.at(i, 1);float width = detectionMat.at(i, 2);float height = detectionMat.at(i, 3);int xLeftBottom = static_cast((x - width / 2) * frame.cols);int yLeftBottom = static_cast((y - height / 2) * frame.rows);int xRightTop = static_cast((x + width / 2) * frame.cols);int yRightTop = static_cast((y + height / 2) * frame.rows);Rect object(xLeftBottom, yLeftBottom,xRightTop - xLeftBottom,yRightTop - yLeftBottom);rectangle(frame, object, Scalar(0, 255, 0),2);if (objectClass < classNames.size()){//获取类别名称及其置信度string conf = format("%.2f", confidence);String label = String(classNames[objectClass]) + ": " + conf;int baseLine = 0;Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom),Size(labelSize.width, labelSize.height + baseLine)),Scalar(255, 255, 255), FILLED);putText(frame, label, Point(xLeftBottom, yLeftBottom + labelSize.height), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));}}}//FPS计算t = ((double)getTickCount() - t) / getTickFrequency();//求输入帧后经过的周期数/每秒系统计的周期数=一帧用时多少秒double fps = 1.0 / t;//求倒数得到每秒经过多少帧,即帧率string text = format("FPS:%.2f", fps);cv::putText(frame, text, Point(10, 50), FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0), 2, 8, 0);//FPS计算imshow("YOLOv2", frame);int c = cv::waitKey(1);if (c == 27){break;}}}void image_detection(string config, string weights, string classespath, string image_path){//读取图片Mat image = imread(image_path);if (image.channels() == 4){cvtColor(image, image, COLOR_BGRA2BGR);}//预处理Mat blob = blobFromImage(image, 1 / 255.0, Size(416, 416), Scalar());//加载网络Net net = readNetFromDarknet(config, weights);if (net.empty()){printf("Could not load net...\n");return;}#if 0//cpu推理net.setPreferableBackend(DNN_BACKEND_OPENCV);net.setPreferableTarget(DNN_TARGET_CPU);#elif 1//使用cuda加速net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA_FP16);#endif//获得分类名vector classNames = getclasses(classespath);net.setInput(blob, "data");Mat detectionMat = net.forward("detection_out");//推理时间vector layersTimings;double freq = getTickFrequency() / 1000;double time = net.getPerfProfile(layersTimings) / freq;ostringstream ss;ss << "FPS: " << 1000 / time << " ; time: " << time << " ms";putText(image, ss.str(), Point(10, 30), 0, 0.5, Scalar(0, 0, 255));for (int i = 0; i < detectionMat.rows;i++){Mat scores = detectionMat.row(i).colRange(5, detectionMat.cols);Point classIdPoint;double confidence;minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);int classid = classIdPoint.x;if (confidence > 0){printf("confide:%.2f\n", confidence);}if (confidence > confidenceThreshold){printf("confide2:%.2f\n", confidence);//x,y,w,h:中心坐标,边框w,hfloat x = detectionMat.at(i, 0) * image.cols;float y = detectionMat.at(i, 1) * image.rows;float width = detectionMat.at(i, 2) * image.cols;float height = detectionMat.at(i, 3) * image.rows;//左上角坐标int xLeftBottom = static_cast((x - width / 2));int yLeftBottom = static_cast((y - height / 2));//获取矩形框x,y,w,hRect object(xLeftBottom, yLeftBottom, static_cast(width), static_cast(height));//绘制矩形框rectangle(image, object, Scalar(0, 255, 0), 2);if (classid < classNames.size()){//获取类别名称及其置信度string conf = format("%.2f", confidence);String label = String(classNames[classid]) + ": " + conf;int baseLine = 0;//在图像上添加标签Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);rectangle(image, Rect(Point(xLeftBottom, yLeftBottom), Size(labelSize.width, labelSize.height + baseLine)), Scalar(255, 255, 255), FILLED);putText(image, label, Point(xLeftBottom, yLeftBottom + labelSize.height), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));}}}imshow("YOLOv2", image);cv::waitKey(0);}int main(){String config = "F:/data/CQU/VS/yolov2-tiny/yolov2-tiny-voc.cfg";String weights = "F:/data/CQU/VS/yolov2-tiny/yolov2-tiny-voc.weights";String classespath = "F:/data/CQU/VS/yolov2-tiny/voc.names";String video_path = "F:/data/CQU/VS/yolov2-tiny/1.mp4";String image_path = "F:/data/CQU/VS/yolov2-tiny/dog_cat.jpg";//image_detection(config, weights, classespath, image_path);detection(config, weights, classespath, video_path);}