读取mtsd文件,并调用模型预测
void CTestDllDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CGetFilePath mGetFilePath;
vector m_vSrcImgPath;
string path = "F:/1234/171220184711_00002";
mGetFilePath.get_AllImg_FileNames(path, m_vSrcImgPath);
int m_iImgDataW = 500;
int m_iImgDataH = 860;
BYTE** m_ppImgData = NULL;
if (m_ppImgData != NULL)
ReSize2dDataPointer(m_ppImgData, m_iImgDataW*m_iImgDataH, 0, 0);
ReSize2dDataPointer(m_ppImgData, 0, m_iImgDataW*m_iImgDataH, m_vSrcImgPath.size());
for (int i = 0; i < m_vSrcImgPath.size(); i++)
{
CString m_strImgPath = StringAbout::toCString(m_vSrcImgPath[i]);
CFileFind findFile;
if (!findFile.FindFile(m_strImgPath))
{
return;
}
BYTE* pOctdata = new BYTE[m_iImgDataW * m_iImgDataH];
memset(pOctdata, 0, sizeof(BYTE));
DWORD dwReadBytes = CProcessFile::ReadBinaryFile(&pOctdata[0],
m_iImgDataW * m_iImgDataH, m_strImgPath);
if (m_iImgDataW * m_iImgDataH != dwReadBytes)
{
return; //读取图形数据失败
}
for (int j = 0; j < m_iImgDataW * m_iImgDataH;j++)
{
m_ppImgData[i][j] = pOctdata[j];
}
}
//DllBase* m_dll = GetDllBase();
////m_dll.LoadNet();
String model = "E:/_darknet/x64/data/METAL/darknet19_448.cfg";
String weights = "E:/_darknet/x64/backup/_darknet19_448_final.weights";
string label = "E:/_darknet/x64/data/METAL/labels.txt";
LoadNet(model, weights, label);
ForCast(m_ppImgData, m_vSrcImgPath.size(), m_iImgDataW, m_iImgDataH);
}
加载模型:
void CTestDllDlg::LoadNet(String model, String weights, String label)
{
//加载网络模
net = readNetFromDarknet(model, weights);
if (net.empty())
{
printf("Could not load net...\n");
//return 0;
}
/*要求网络在支持的地方使用特定的计算后端
*如果opencv是用intel的推理引擎库编译的,那么dnn_backend_default意味着dnn_backend_interrusion_引擎
*否则等于dnn_backend_opencv。
*/
net.setPreferableBackend(DNN_BACKEND_OPENCV);
//要求网络对特定目标设备进行计算
net.setPreferableTarget(DNN_TARGET_CPU);
//2.加载分类信息
//vector classNamesVec;
ifstream classNamesFile(label.c_str());
if (classNamesFile.is_open())
{
string className = "";
while (std::getline(classNamesFile, className))
m_classes.push_back(className);
}
}
预测内存图片
void CTestDllDlg::ForCast(BYTE** mImgs, int img_size, int width, int height)
{
std::vector m_vImgs;
unsigned char* tmpImg = new unsigned char[height*width*3];
/*for (int i = 0; i < height; i++)
{
tmpImg[i] = new unsigned char[width];
}*/
for (int i = 0; i < 1/*img_size*/; i++)
{
int iCount = 3;
int iPos = 0;
int iWNum = width * 3;
int k = 0;
for (int n = 0; n< width; ++n)
{
iPos = n * iCount;
for (int m = 0; m< height; ++m)
{
k = m * iWNum + iPos;
tmpImg[k] = (BYTE)mImgs[i][n * height + m];
tmpImg[k + 1] = (BYTE)mImgs[i][n * height + m];
tmpImg[k + 2] = (BYTE)mImgs[i][n * height + m];
}
}
Mat mimg=Mat(height, width, CV_8UC3, (unsigned char*)tmpImg).clone();
m_vImgs.push_back(mimg);
imshow("test", mimg);
waitKey();
}
// BYTE *pdata = new BYTE[width * height * 3];
/*for (int i = 0; i < width; i++)
{
delete[] tmpImg[i];
}*/
delete[] tmpImg;
Mat inputBlob = blobFromImage(m_vImgs[0], 1 / 255.F, Size(width, height), Scalar(), true, false);
net.setInput(inputBlob, "data");
//4.检测和显示
//获得“dectection_out"的输出
vector outs;
net.forward(outs, getOutputsNames(net));
vector layersTimings;
double freq = getTickFrequency() / 1000;
double time = net.getPerfProfile(layersTimings) / freq;
//如果是目标检测任务绘制矩形框
for (int i = 0; i < outs.size(); i++)
{
//reshape the blob to 1x1000 matrix // 1000个分类
Mat probMat = outs[i].reshape(1, 1);
Point classNumber;
double classProb;
// 可能性最大的一个
minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
// 分类索引号
int classIdx = classNumber.x;
printf("current image classification : %s, possible : %.8f \n", m_classes.at(classIdx).c_str(), classProb);
string label = format("class: ") + m_classes.at(classIdx) + format(" ,time: %.2f ms", time);
putText(m_vImgs[i], label, Point(20, 20), 0, 0.5, Scalar(0, 0, 255));
string winName = format("OCT%d", i);
namedWindow(winName);
imshow(winName, m_vImgs[i]);
waitKey();
}
}
获得模型各层ming名称
vector CTestDllDlg::getOutputsNames(Net&net)
{
static vector names;
if (names.empty())
{
//返回加载模型中所有层的输入和输出形状(shape)
vector outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
vector 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;
}