yolo-fastestv2部署 视频 检测

文章目录[隐藏]

训练 部署 见: https://blog.csdn.net/qq_36758461/article/details/121431614

这是 视频检测 并保存… 小白部署学习记录
整理下 文件 依赖思路 :

yolo-fastestv2.h 只做声明,声明了 TargetBox 和 yoloFastestv2 类, 编译后不产生代码;
yolo-fastestv2.cpp 进行 代码实现

TargetBox 用在 yolo-fastestv2.cpp 中 vector 数据类型中
yoloFastestv2 类封装了 具体的一些函数:

(1) yoloFastestv2::yoloFastestv2() 模型的参数配置
(2) yoloFastestv2::~yoloFastestv2() 退出类
(3) int yoloFastestv2::loadModel() ncnn 模型加载
(4) int yoloFastestv2::detection() 检测 -> 调用 predHandle, nmsHandle

  • int yoloFastestv2::nmsHandle() NMS处理
  • int yoloFastestv2::predHandle() 特征图后处理 -> 调用 getCategory
    • int yoloFastestv2::getCategory() 检测类别分数处理

video_demo.cpp

#include "yolo-fastestv2.h"

int main()
{
    static const char* class_names[] = {
        "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
        "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
        "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
        "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
        "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
        "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
        "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
        "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
        "hair drier", "toothbrush"
    };

//    static const char* class_names[] = {
//        "airplane", "ship", "storage tank", "baseball diamond", "tennis court",
//        "basketball court", "ground track field", "harbor", "bridge", "vehicle"
//    };
    //在yolo-fastestv2.h 中, 有一个类函数  yoloFastestv2
    yoloFastestv2 api;

    api.loadModel("./model/yolo-fastestv2-opt.param",
                  "./model/yolo-fastestv2-opt.bin");

    //cv::Mat cvImg = cv::imread("058.jpg");
    // Vector容器中存放自定义数据类型,存放目标框的信息(cls,score,x,y,w,h,area), 在 yolo-fastestv2.h  有声明
    std::vector<TargetBox> boxes;
    cv::VideoCapture capture;
    capture.open("../../person.mp4");
    //获取当前 视频信息
    cv::Size S = cv::Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH),
                          (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT));

    // -----------保存视频的检测结果--------------
    cv:: VideoWriter outputVideo;
    outputVideo.open("./out.mp4", CV_FOURCC('P','I','M','1'), 30.0, S, true);
    if (!outputVideo.isOpened()) {
        std::cout << "fail to open!" << std::endl;
        return -1;
    }
    // ---------------------------------

    cv::Mat frame;
    while (1) {
		capture >> frame;//读入视频的帧
		if (frame.empty()) break;

		// 检测 图像,结果保存在 boxes
        api.detection(frame, boxes);
        // 可视化,绘制框
        for (int i = 0; i < boxes.size(); i++) {
            std::cout<<boxes[i].x1<<" "<<boxes[i].y1<<" "<<boxes[i].x2<<" "<<boxes[i].y2
                     <<" "<<boxes[i].score<<" "<<boxes[i].cate<<std::endl;

            char text[256];
            sprintf(text, "%s %.1f%%", class_names[boxes[i].cate], boxes[i].score * 100);

            int baseLine = 0;
            cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);

            int x = boxes[i].x1;
            int y = boxes[i].y1 - label_size.height - baseLine;
            if (y < 0)
                y = 0;
            if (x + label_size.width > frame.cols)
                x = frame.cols - label_size.width;

            cv::rectangle(frame, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
                          cv::Scalar(255, 255, 255), -1);
            cv::putText(frame, text, cv::Point(x, y + label_size.height),
                        cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));

            cv::rectangle (frame, cv::Point(boxes[i].x1, boxes[i].y1),
                           cv::Point(boxes[i].x2, boxes[i].y2), cv::Scalar(255, 255, 0), 2, 2, 0);
        }

        cv:: namedWindow("img",CV_WINDOW_NORMAL);
		cv:: imshow("img", frame);

        // 保存 视频检测文件
        outputVideo.write(frame); //把图像写入视频流

		//按下ESC退出整个程序
        int c = cv::waitKey(30);
        if( char(c) == 27) return -1;
	}
//    cv::imwrite("output.png", cvImg);
    // 关闭释放
    capture.release();
    cv::waitKey(0);
    return 0;
}

版权声明:本文为CSDN博主「索隆啊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36758461/article/details/121539861

索隆啊

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

Meta-DETR | 图像级“元”学习提升目标检测精度

计算机视觉研究院专栏作者:Edison_GOne-shot目标检测旨在通过几个标注的样本来检测新的目标。之前的工作已经证明了元学习是一个很有前途的解决方案,它们中的大多数基本上是通过解决在区域上的元学习检测来进行分类

AP AR mAP ROC AUC(目标检测)

禁止转载!在做目标检测任务的时候,通常会制定规则来评估性能,就如下图所示,后面会慢慢道来其中的原理。 混淆矩阵中 TP、TN、FP、FN
在目标检测中,通常以IoU阈值作为

瑕疵检测(深度学习)

与通用目标检测的区别
相较与整张图片瑕疵区域的占比一般非常小,Faster R-CNN等检测模型对小物体检测不够好
深度学习从低层到高层不断去提炼高层语义信息,层数的增大细节的信息丢失得越多,对于缺