#include <fstream>
#include <sstream>
#include <iostream>
#include "common.hpp"
std::string keys =
"{ help h | | Print help message. }"
"{ @alias | | 用於從 models.yml 檔案中提取預處理引數的模型別名。 }"
"{ zoo | models.yml | 包含預處理引數檔案的可選路徑 }"
"{ input i | | 輸入影像或影片檔案的路徑。跳過此引數可從攝像頭捕獲幀。}"
"{ initial_width | 0 | 透過初始調整大小將輸入影像預處理為特定寬度。}"
"{ initial_height | 0 | 透過初始調整大小將輸入影像預處理為特定高度。}"
"{ std | 0.0 0.0 0.0 | 透過除以標準差對輸入影像進行預處理。}"
"{ crop | false | 透過中心裁剪對輸入影像進行預處理。}"
"{ framework f | | 模型原始框架的可選名稱。如果未設定,則自動檢測。 }"
"{ needSoftmax | false | 使用 Softmax 對網路輸出進行後處理。}"
"{ classes | | 包含類別名稱文字檔案的可選路徑。 }"
"{ backend | 0 | 選擇一個計算後端: "
"0: 自動(預設), "
"1: Halide 語言 (http://halide-lang.org/), "
"2:Intel 深度學習推理引擎 (https://software.intel.com/openvino-toolkit), "
"3: OpenCV 實現, "
"4: VKCOM, "
"5:CUDA, "
"6:WebNN }"
"{ target | 0 | 選擇一個目標計算裝置: "
"0: CPU 目標(預設), "
"1: OpenCL, "
"2: OpenCL fp16(半精度浮點), "
"3: VPU, "
"4: Vulkan, "
"6: CUDA, "
"7: CUDA fp16(半精度浮點預處理)}";
using namespace dnn;
std::vector<std::string> classes;
int main(
int argc,
char** argv)
{
const std::string modelName = parser.get<
String>(
"@alias");
const std::string zooFile = parser.get<
String>(
"zoo");
keys += genPreprocArguments(modelName, zooFile);
parser.about("Use this script to run classification deep learning networks using OpenCV.");
if (argc == 1 || parser.has("help"))
{
parser.printMessage();
return 0;
}
int rszWidth = parser.get<int>("initial_width");
int rszHeight = parser.get<int>("initial_height");
float scale = parser.get<float>("scale");
bool swapRB = parser.get<bool>("rgb");
bool crop = parser.get<bool>("crop");
int inpWidth = parser.get<int>("width");
int inpHeight = parser.get<int>("height");
int backendId = parser.get<int>("backend");
int targetId = parser.get<int>("target");
bool needSoftmax = parser.get<bool>("needSoftmax");
std::cout<<"mean: "<<mean<<std::endl;
std::cout<<
"std: "<<
std<<std::endl;
if (parser.has("classes"))
{
std::string file = parser.get<
String>(
"classes");
std::ifstream ifs(file.c_str());
if (!ifs.is_open())
CV_Error(Error::StsError,
"File " + file +
" not found");
std::string line;
while (std::getline(ifs, line))
{
classes.push_back(line);
}
}
if (!parser.check())
{
parser.printErrors();
return 1;
}
Net net =
readNet(model, config, framework);
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
static const std::string kWinName = "Deep learning image classification in OpenCV";
if (parser.has("input"))
else
{
cap >> frame;
if (frame.empty())
{
break;
}
if (rszWidth != 0 && rszHeight != 0)
{
}
if (
std.val[0] != 0.0 &&
std.val[1] != 0.0 &&
std.val[2] != 0.0)
{
}
net.setInput(blob);
int classId;
double confidence;
Mat prob = net.forward();
double t1;
prob = net.forward();
for(int i = 0; i < 200; i++) {
prob = net.forward();
classId = classIdPoint.
x;
}
if (needSoftmax == true)
{
float maxProb = 0.0;
maxProb = *std::max_element(prob.
begin<
float>(), prob.
end<
float>());
cv::exp(prob-maxProb, softmaxProb);
classId = classIdPoint.
x;
}
std::string label =
format(
"1輪推理時間:%.2f ms", t1);
label =
format(
"%s: %.4f", (classes.empty() ?
format(
"Class #%d", classId).c_str()
classes[classId].c_str()),
confidence);
}
return 0;
}
如果陣列沒有元素,則返回 true。
int64_t int64
Mat reshape(int cn, int rows=0) const
更改二維矩陣的形狀和/或通道數,而不復制資料。
MatIterator_< _Tp > end()
返回矩陣迭代器並將其設定為矩陣的最後一個元素之後。
MatIterator_< _Tp > begin()
返回矩陣迭代器並將其設定為矩陣的第一個元素。
_Tp x
點的 x 座標
定義 types.hpp:201
用於指定影像或矩形大小的模板類。
Definition types.hpp:335
一個用於測量流逝時間的類。
定義 utility.hpp:326
void start()
開始計時。
定義 utility.hpp:335
void stop()
停止計時。
定義 utility.hpp:341
void reset()
重置內部值。
定義 utility.hpp:430
double getTimeMilli() const
返回經過的時間(毫秒)。
定義 utility.hpp:365
用於從影片檔案、影像序列或攝像頭捕獲影片的類。
Definition videoio.hpp:772
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
開啟影片檔案、捕獲裝置或 IP 影片流以進行影片捕獲。
void exp(InputArray src, OutputArray dst)
計算每個陣列元素的指數。
void divide(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
對兩個陣列進行逐元素除法,或用陣列除以標量。
Scalar sum(InputArray src)
計算陣列元素的和。
void minMaxLoc(InputArray src, double *minVal, double *maxVal=0, Point *minLoc=0, Point *maxLoc=0, InputArray mask=noArray())
查詢陣列中的全域性最小值和最大值。
std::string String
定義 cvstd.hpp:151
String format(const char *fmt,...)
使用類似 printf 的表示式格式化文字字串。
#define CV_Error(code, msg)
呼叫錯誤處理程式。
定義 base.hpp:399
#define CV_Assert(expr)
在執行時檢查條件,如果失敗則丟擲異常。
定義 base.hpp:423
Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size &size=Size(), const Scalar &mean=Scalar(), bool swapRB=false, bool crop=false, int ddepth=CV_32F)
從影像建立四維 blob。可選地調整影像大小並從中心裁剪,...
Net readNet(CV_WRAP_FILE_PATH const String &model, CV_WRAP_FILE_PATH const String &config="", const String &framework="")
讀取支援格式之一表示的深度學習網路。
void imshow(const String &winname, InputArray mat)
在指定視窗中顯示影像。
int waitKey(int delay=0)
等待按鍵按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
建立視窗。
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
繪製文字字串。
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3