WINDOWS下 YOLOV3/YOLOV4 CPU 檢測驗證
實踐環境:
VS2015 + OPENCV4.5 (release build)
cpu :i7
對比了 darket 和 opencv dnn
darknet:
./darknet_no_gpu.exe detector test data/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights -i 0 -thresh 0.25 data/dog.jpg --img-size 416 -ext_output
opencv-dnn:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include<opencv2\dnn\dnn.hpp>
#include<opencv2\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace cv::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 imag
std::vector<std::string> classes;
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, cv::Mat& frame)
{
//Draw a rectangle displaying the bounding box
cv::rectangle(frame, cv::Point(left, top), cv::Point(right, bottom), cv::Scalar(0, 0, 255));
//Get the label for the class name and its confidence
std::string label = cv::format("%.2f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ":" + label;
}
else
{
std::cout << "classes is empty..." << std::endl;
}
//Display the label at the top of the bounding box
int baseLine;
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = std::max(top, labelSize.height);
cv::putText(frame, label, cv::Point(left, top), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255, 255, 255));
}
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(cv::Mat& frame, std::vector<cv::Mat>& outs)
{
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<cv::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)
{
cv::Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
cv::Point classIdPoint;
double confidence;
// Get the value and location of the maximum score
cv::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(cv::Rect(left, top, width, height));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
std::vector<int> indices;
cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
cv::Rect box = boxes[idx];
drawPred(classIds[idx], confidences[idx], box.x, box.y,
box.x + box.width, box.y + box.height, frame);
}
}
// Get the names of the output layers
std::vector<cv::String> getOutputsNames(const cv::dnn::Net& net)
{
static std::vector<cv::String> names;
if (names.empty())
{
//Get the indices of the output layers, i.e. the layers with unconnected outputs
std::vector<int> outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
std::vector<cv::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;
}
int main()
{
// Give the configuration and weight files for the model
string modelConfiguration = "D:\\yolov3\\cfg\\yolov3.cfg";
string modelWeights = "D:\\yolov3\\yolov3.weights";
// Load names of classes
string classesFile = "D:\\yolov3\\data\\coco.names";
ifstream ifs(classesFile.c_str());
string line;
clock_t start, finish;
while (getline(ifs, line))
{
classes.push_back(line);
}
//Load the network
Net net = readNetFromDarknet(modelConfiguration, modelWeights);
cv::Mat blob;
Mat frame0;
//Load test image
for (int i = 0; i < 100; ++i) {
Mat frame = imread("D:\\yolov3\\data\\dog.jpg");
//start time
start = clock();
//Create a 4D blob from a frame. 建立神經網路輸入影像
blobFromImage(frame, blob, 1 / 255.0, cv::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);
finish = clock();
cout << "Run time is " << double(finish - start) / CLOCKS_PER_SEC << endl;
frame0 = frame;
}
//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(frame0, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
imshow("result", frame0);
//儲存影像
imwrite("D:\\yolov3\\result.jpg", frame0);
cv::waitKey(0);
}
檢測結果 上看,幾位速度快的自行車都不要了。 唉。 yolov3-tiny opencv dnn 還多認了一輛跑車。
再看看 yolov3 opencv dnn
相關文章
- 目標檢測網路之 YOLOv3YOLO
- 深度剖析目標檢測演算法YOLOV4演算法YOLO
- 電梯電車識別 yolov5 yolov4 yolov3YOLO
- 手把手教你如何將 yolov3/yolov4 轉為 caffe 模型YOLO模型
- 網站漏洞檢測 身份驗證碼與重要操作驗證碼安全問題網站
- 這才是目標檢測YOLOv3的真實面目YOLO
- 目標檢測 YOLO v3 驗證 COCO 模型YOLO模型
- win10 下的YOLOv3 訓練 wider_face 資料集檢測人臉Win10YOLOIDE
- 使用 C++ 呼叫 YOLOv3 模型進行物體檢測C++YOLO模型
- 重型包裝檢測運輸驗證哪裡可做?
- 驗證ADG的壞塊檢測和自動修復
- 基於OpenCV和YOLOv3深度學習的目標檢測OpenCVYOLO深度學習
- 實時物體檢測:YOLO,YOLOv2和YOLOv3(一)YOLO
- selenium-java被檢測導致滑塊驗證失敗Java
- SSL證書檢測工具有什麼用?如何檢測SSL證書?
- windows 下檢視埠占用Windows
- 直播app原始碼,進行身份驗證時,檢測身份證位數夠不夠APP原始碼
- pytorch實現yolov3(5) 實現端到端的目標檢測PyTorchYOLO
- 基於YOLOv4的目標檢測系統(附MATLAB程式碼+GUI實現)YOLOMatlabGUI
- OpenCV4.4剛剛釋出!支援YOLOv4、EfficientDet檢測模型,SIFT移至主庫!OpenCVYOLO模型
- 關於Android P 黑名單的檢測 windows下的 veridex的使用AndroidWindowsIDE
- 使用nslookup檢視SSL安全證書域名驗證資訊
- java檢測當前CPU負載狀態的方法Java負載
- yolov4YOLO
- Windows下繫結執行緒到指定的CPU核心Windows執行緒
- windows10硬碟怎麼檢測_win10硬碟檢測的方法Windows硬碟Win10
- 香港抽血檢驗性別Y該怎麼預約檢測?尖沙咀基因檢測
- 簡訊驗證碼測試項
- 檢測伺服器物理機CPU是否滿足超頻伺服器
- 為你的App增加WIFI認證檢測,讓使用者體驗更加絲滑APPWiFi
- win10正版驗證方法_怎麼檢測win10系統是否是正版Win10
- 深度學習2.0-25.Train-Val-Test劃分檢測過擬合(交叉驗證)深度學習AI
- 福祿克的驗證測試和認證測試的區別
- 在香港尖沙咀基因檢測中心6周查血驗男女檢測後的感想,分享親身經驗
- apt-key 金鑰管理,apt-secure 原理 驗證鏈 驗證測試APT
- 進擊的YOLOv3,目標檢測網路的巔峰之作 | 內附實景大片YOLO
- Laravel 的 Validation 檢索驗證錯誤訊息Laravel
- 益普索報告:檢驗與檢測的未來