OpenCV 实时目标检测

devtools/2024/9/23 8:14:07/

0.概述

1.原理介绍

2.代码实现

#include <iostream>
#include <opencv2/opencv.hpp>int main() {// Load pre-trained MobileNet SSD model and configurationstd::string model = "path_to_mobilenet_iter_73000.caffemodel";std::string config = "path_to_deploy.prototxt";cv::dnn::Net net = cv::dnn::readNetFromCaffe(config, model);// Use webcam for real-time detectioncv::VideoCapture cap(0);if (!cap.isOpened()) {std::cerr << "Error: Couldn't open the webcam." << std::endl;return -1;}while (true) {cv::Mat frame;cap >> frame;// Prepare the frame for the neural networkcv::Mat blob = cv::dnn::blobFromImage(frame, 0.007843, cv::Size(300, 300), 127.5);net.setInput(blob);// Forward passcv::Mat detection = net.forward();// Process the detectionfor (int i = 0; i < detection.size[2]; i++) {float confidence = detection.at<float>(0, 0, i, 2);if (confidence > 0.2) {  // Threshold for confidenceint classId = static_cast<int>(detection.at<float>(0, 0, i, 1));int left = static_cast<int>(detection.at<float>(0, 0, i, 3) * frame.cols);int top = static_cast<int>(detection.at<float>(0, 0, i, 4) * frame.rows);int right = static_cast<int>(detection.at<float>(0, 0, i, 5) * frame.cols);int bottom = static_cast<int>(detection.at<float>(0, 0, i, 6) * frame.rows);// Draw bounding box for detected objectcv::rectangle(frame, cv::Point(left, top), cv::Point(right, bottom), cv::Scalar(0, 255, 0), 2);}}// Display the frame with detectionscv::imshow("Real-time Object Detection", frame);// Exit on pressing 'q'if (cv::waitKey(1) == 'q') break;}cap.release();cv::destroyAllWindows();return 0;
}


http://www.ppmy.cn/devtools/42253.html

相关文章

AI绘图Stable Diffusion,如何无损高清放大图片,保姆级教程建议收藏!

前言 我们在用 stable diffusion 制作AI图片时&#xff0c;默认生成图片的尺寸为512*512&#xff0c;即使是竖图一般也就是512*768&#xff0c;如果再把尺寸设置大一些&#xff0c;就会因为硬件算力不够而造成系统崩溃&#xff0c;今天就来跟大家聊一聊&#xff0c;如何将制作…

电脑复制和粘贴的时候会出现Hello!

电脑不管是Microsoft Excel还是Microsoft Word复制之后粘贴过来就出现HELLO&#xff0c;当复制粘贴文件的时候就会出现WINFILE&#xff1b; 具体现象看下面两个图片&#xff1a; 这是因为winfile 文件病毒&#xff08;幽灵蠕虫病毒&#xff09;,每月的28号发作&#xff1b; 症状…

HOG(Histogram of Oriented Gradients)

1.概要 HOG (Histogram of Oriented Gradients) 是一种在计算机视觉和图像处理中广泛使用的特征描述符&#xff0c;主要用于对象检测、图像识别等领域。HOG 通过计算和统计图像局部区域的梯度方向直方图来构成特征。以下是 HOG 的主要步骤&#xff1a; 预处理&#xff1a;图像…

《Tam》论文笔记(下)

3 Method 3.1. The Overview of Temporal Adaptive Module 正如我们在第1节中讨论的&#xff0c;视频数据通常表现出由相机运动和速度变化等因素引起的复杂时间动态。因此&#xff0c;我们的目标是通过引入具有视频特定内核的时间自适应模块 (TAM) 来解决这个问题&#xff0c…

C# 结合 JavaScript 对 Web 控件进行数据输入验证

目录 关于数据验证 范例运行环境 验证设计 JavaScript 方法 设计 实现 调用示例 C# 方法 设计 实现 调用示例 小结 关于数据验证 在 Web 应用的录入界面&#xff0c;数据验证是一项重要的实现功能&#xff0c;数据验证是指确认 Web 控件输入或选择的数据&#xff…

机器学习作业4——朴素贝叶斯分类器

目录 一、理论 一个例子&#xff1a; 二、代码 对于代码的解释&#xff1a; 1.fit函数&#xff1a; 2.predict函数: 三、实验结果 原因分析&#xff1a; 一、理论 朴素贝叶斯分类器基于贝叶斯定理进行分类&#xff0c;通过后验概率来判断将新数据归为哪一类。通过利用贝…

探索 Element:开源聊天平台的新趋势

探索 Element&#xff1a;开源聊天平台的新趋势 随着数字化时代的发展&#xff0c;人们对于隐私和数据安全的关注日益增加&#xff0c;对开源和去中心化平台的需求也在逐渐增加。在这个背景下&#xff0c;Element作为一种开源聊天平台&#xff0c;吸引了越来越多的关注和用户。…

SQL慢查询学习篇

https://www.cnblogs.com/isyues/p/17733015.html 1. 对扫到的SQL慢查询语句执行 explain explain select task_id, channel, count(task_id) as count from tablename where send_time > "2024-05-10 16:13:59" and send_time < "2024-05-14 16:13:59…