使用YoloV3检测C++;项目VS 我遵循一个主题来安装YOLO,用于C++项目(),但我还有一个问题:我尝试在我的C++项目中启动YOLVO3。我构建了yolo_cpp_dll_no_gpu.sln(我没有gpu),所以我有darknet_no_gpu.exe
我想从我的网络摄像头中检测到帧上的一些对象。我获得了框架,但我不知道如何在C++项目中启动YOLO检测。也许它类似于Python命令,但是我找不到C++语法……/P> 你能帮我吗 编辑:(我提供更多细节)使用YoloV3检测C++;项目VS 我遵循一个主题来安装YOLO,用于C++项目(),但我还有一个问题:我尝试在我的C++项目中启动YOLVO3。我构建了yolo_cpp_dll_no_gpu.sln(我没有gpu),所以我有darknet_no_gpu.exe,c++,yolo,darknet,C++,Yolo,Darknet,我想从我的网络摄像头中检测到帧上的一些对象。我获得了框架,但我不知道如何在C++项目中启动YOLO检测。也许它类似于Python命令,但是我找不到C++语法……/P> 你能帮我吗 编辑:(我提供更多细节) 在Visual Studio 2019上的C++项目中,需要使用YOLVO3进行手检测。 所以我用python命令训练了Yolov3。我获得了.weights文件,当我在cmd上启动此命令时,检测工作正常: darknet_no_gpu探测器演示数据/obj.data cfg/yolov3
-
在Visual Studio 2019上的C++项目中,需要使用YOLVO3进行手检测。
- 所以我用python命令训练了Yolov3。我获得了.weights文件,当我在cmd上启动此命令时,检测工作正常: darknet_no_gpu探测器演示数据/obj.data cfg/yolov3-tiny.cfg yolov3-tiny_last.weights
- 现在,我想在VS项目中使用此检测。我构建了yolo\u cpp\u dll\u no\u gpu.sln以获得darknet\u no\u gpu.exe
- 在我的项目中,我在属性和依赖项中有include和library的路径
VideoCapture cap(0); // open camera
if (!cap.isOpened())
return -1;
cap.set(CAP_PROP_FRAME_WIDTH, 320);
cap.set(CAP_PROP_FRAME_HEIGHT, 240);
for (;;) {
cap >> frame;
if (frame.empty())
break;
`
因为我不能在VS中使用这个
darknet_no_gpu detector demo data/obj.data cfg/yolov3-tiny.cfg yolov3-tiny_last.weights frame
编辑2:
你好
在你的帮助下做了一些研究之后,我还有一个问题。。。
这是我的密码:
#include <Windows.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace dnn;
using namespace std;
// Initialize the parameters
float confThreshold = 0.5; // Confidence threshold
float nmsThreshold = 0.4; // Non-maximum suppression threshold
int inpWidth = 416; // Width of network's input image
int inpHeight = 416; // Height of network's input image
vector<string> classes;
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& out);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net);
int main(int argc, char** argv)
{
// Load names of classes
string classesFile = "obj.names";
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line)) classes.push_back(line);
// Give the configuration and weight files for the model
String modelConfiguration = "yolov3-tiny.cfg";
String modelWeights = "yolov3-tiny_last.weights";
// Load the network
Net net = readNetFromDarknet(modelConfiguration, modelWeights);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
// Open a video file or an image file or a camera stream.
string outputFile;
//VideoCapture cap;
VideoWriter video;
Mat frame;
Mat blob, ex;
VideoCapture cap(0); // ouvre la camera par défaut
if (!cap.isOpened()) // check si succès
return -1;
for (;;) {
// get frame from the video
cap >> frame;
cvtColor(frame, ex, COLOR_BGR2YCrCb);
cout << frame.size << endl;
imshow("e", ex);
// Stop the program if reached end of video
if (frame.empty()) {
cout << "Done processing !!!" << endl;
cout << "Output file is stored as " << outputFile << endl;
waitKey(3000);
break;
}
// Create a 4D blob from a frame.
blobFromImage(frame, blob, 1 / 255.0, Size(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
//Sets the input to the network
net.setInput(blob);
// Runs the forward pass to get output of the output layers
vector<Mat> outs;
net.forward(outs, getOutputsNames(net));
// Remove the bounding boxes with low confidence
postprocess(frame, outs);
// Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
vector<double> layersTimes;
double freq = getTickFrequency() / 1000;
double t = net.getPerfProfile(layersTimes) / freq;
string label = format("Inference time for a frame : %.2f ms", t);
putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
// Write the frame with the detection boxes
Mat detectedFrame;
frame.convertTo(detectedFrame, CV_8U);
imshow("f", frame);
Sleep(3000);
}
cap.release();
return 0;
}
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& outs)
{
vector<int> classIds;
vector<float> confidences;
vector<Rect> boxes;
for (size_t i = 0; i < outs.size(); ++i)
{
// Scan through all the bounding boxes output from the network and keep only the
// ones with high confidence scores. Assign the box's class label as the class
// with the highest score for the box.
float* data = (float*)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
Point classIdPoint;
double confidence;
// Get the value and location of the maximum score
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > confThreshold)
{
int centerX = (int)(data[0] * frame.cols);
int centerY = (int)(data[1] * frame.rows);
int width = (int)(data[2] * frame.cols);
int height = (int)(data[3] * frame.rows);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
vector<int> indices;
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
drawPred(classIds[idx], confidences[idx], box.x, box.y,
box.x + box.width, box.y + box.height, frame);
}
}
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
{
//Draw a rectangle displaying the bounding box
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
//Get the label for the class name and its confidence
string label = format("%.2f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ":" + label;
}
//Display the label at the top of the bounding box
int baseLine;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = max(top, labelSize.height);
rectangle(frame, Point(left, top - round(1.5 * labelSize.height)), Point(left + round(1.5 * labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
}
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net)
{
static vector<String> names;
if (names.empty())
{
//Get the indices of the output layers, i.e. the layers with unconnected outputs
vector<int> outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
vector<String> layersNames = net.getLayerNames();
// Get the names of the output layers in names
names.resize(outLayers.size());
for (size_t i = 0; i < outLayers.size(); ++i)
names[i] = layersNames[outLayers[i] - 1];
}
return names;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间dnn;
使用名称空间std;
//初始化参数
浮动阈值=0.5;//置信阈值
float nmsThreshold=0.4;//非最大抑制阈值
int inpWidth=416;//网络输入图像的宽度
int inpHeight=416;//网络输入图像的高度
向量类;
//使用非最大值抑制删除置信度较低的边界框
void后处理(Mat和frame、const vector和out);
//绘制预测的边界框
void drawPred(内部classId、浮动形态、内部左侧、内部顶部、内部右侧、内部底部、垫和框架);
//获取输出层的名称
向量getOutputsNames(const-Net和Net);
int main(int argc,字符**argv)
{
//加载类的名称
string classesFile=“obj.names”;
ifstream ifs(classesFile.c_str());
弦线;
while(getline(ifs,line))类。向后推(line);
//给出模型的配置和重量文件
String modelConfiguration=“yolov3 tiny.cfg”;
String modelWeights=“yolov3-tiny\u last.weights”;
//加载网络
Net=readNetFromDarknet(模型配置,模型权重);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
//打开视频文件、图像文件或相机流。
字符串输出文件;
//视频捕捉帽;
录像机录像;
垫架;
Mat blob,ex;
视频捕捉帽(0);// ouvra照相机
如果(!cap.isopend())//检查是否成功
返回-1;
对于(;;){
//从视频中获取帧
cap>>框架;
CVT颜色(框架、ex、颜色_BGR2YCrCb);
CUT欢迎使用堆栈溢出。请学习,特别是,你必须展示你的代码和解决问题的一些努力。你能澄清这个问题吗?你是想用你自己的C++代码使用YOLO还是你在YOLO运行中遇到问题?也许这个教程可以帮助你去栈溢出。请学习,在PullAR,你必须告诉我们你的代码和一些努力来解决你的问题。你能澄清这个问题吗?你是想用你自己的C++代码使用YOLO还是你有困难让YOLO运行?也许这个教程会有帮助。