Commit f2f74c45 张士柳

1 个父辈 ce080e8c
...@@ -787,7 +787,7 @@ namespace eyemLib_Sharp ...@@ -787,7 +787,7 @@ namespace eyemLib_Sharp
#region 测试接口 #region 测试接口
//测试接口 //测试接口
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemAllMindImpl(EyemImage tpImage); private static extern int eyemLibImpl(EyemImage tpImage, out EyemImage tpDstImg);
#endregion #endregion
#region 日志功能 #region 日志功能
...@@ -861,7 +861,7 @@ namespace eyemLib_Sharp ...@@ -861,7 +861,7 @@ namespace eyemLib_Sharp
//flag = eyemImageAbs(image1, ref tpDstImg); //flag = eyemImageAbs(image1, ref tpDstImg);
flag = eyemAllMindImpl(image); flag = eyemLibImpl(image, out tpDstImg);
#region Test Blob #region Test Blob
...@@ -1017,12 +1017,12 @@ namespace eyemLib_Sharp ...@@ -1017,12 +1017,12 @@ namespace eyemLib_Sharp
//获取用于制作模板的图像 //获取用于制作模板的图像
//flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg); //flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);
//Bitmap bitmap = eyemCvtToBitmap(tpDstImg); Bitmap bitmap = eyemCvtToBitmap(tpDstImg);
//if (bitmap != null) if (bitmap != null)
//{ {
// bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file); bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
//} }
//创建模板匹配模型 //创建模板匹配模型
EyemRect tpRoi2 = new EyemRect(); EyemRect tpRoi2 = new EyemRect();
......
...@@ -859,7 +859,7 @@ extern "C" { ...@@ -859,7 +859,7 @@ extern "C" {
EXPORTS int eyemReleaseModel(IntPtr &hModelID); EXPORTS int eyemReleaseModel(IntPtr &hModelID);
EXPORTS int eyemTrackFeature(EyemImage tpPrevImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum, int *ipResults, EyemImage *tpDstImg); EXPORTS int eyemTrackFeature(EyemImage tpPrevImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum, int *ipResults, EyemImage *tpDstImg);
EXPORTS int eyemAOIForTSAV(EyemImage tpRefImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum); EXPORTS int eyemAOIForTSAV(EyemImage tpRefImg, EyemImage tpNextImg, EyemRect3 *tpRois, int iRoiNum);
EXPORTS int eyemAllMindImpl(EyemImage tpImage); EXPORTS int eyemLibImpl(EyemImage tpImage, EyemImage *tpDstImg);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
此文件类型无法预览
...@@ -5989,62 +5989,17 @@ int eyemAOIForTSAV(EyemImage tpRefImg, EyemImage tpNextImg, EyemRect3 *tpRois, i ...@@ -5989,62 +5989,17 @@ int eyemAOIForTSAV(EyemImage tpRefImg, EyemImage tpNextImg, EyemRect3 *tpRois, i
//} //}
//移除低置信度边界框 int eyemLibImpl(EyemImage tpImage, EyemImage *tpDstImg)
static void postprocess(cv::Mat& frame, const 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++) {
//从网络输出中扫描所有边界框
//保留高置信度选框
cv::Mat tm = outs[i];
//目标数据data:x,y,w,h为百分比,x,y为目标中心点坐标
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;//置信度
//取得最大分数值与索引
cv::minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > 0.5) {
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));
}
}
}
//低置信度
std::vector<int> indices;//保存没有重叠边框的索引
//该函数用于抑制重叠边框
cv::dnn::NMSBoxes(boxes, confidences, 0.5, 0.4, 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);
cv::rectangle(frame, box, cv::Scalar(0, 0, 255));
}
}
int eyemAllMindImpl(EyemImage tpImage)
{ {
cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone(); cv::Mat image = cv::Mat(tpImage.iHeight, tpImage.iWidth, MAKETYPE(tpImage.iDepth, tpImage.iChannels), tpImage.vpImage).clone();
if (image.empty()) if (image.empty())
return FUNC_IMAGE_NOT_EXIST; return FUNC_IMAGE_NOT_EXIST;
if (image.channels() != 3) {
cv::cvtColor(image, image, cv::COLOR_BGRA2BGR);
}
#pragma region wechat_qrcode #pragma region wechat_qrcode
//cv::Ptr<wechat_qrcode::WeChatQRCode> detector; //cv::Ptr<wechat_qrcode::WeChatQRCode> detector;
...@@ -6065,6 +6020,15 @@ int eyemAllMindImpl(EyemImage tpImage) ...@@ -6065,6 +6020,15 @@ int eyemAllMindImpl(EyemImage tpImage)
#pragma endregion #pragma endregion
//加载类名
std::vector<std::string> classes;
std::string classFile = ".\\darknet\\detect.names";
std::ifstream ifs(classFile.c_str());
std::string line;
while (std::getline(ifs, line)) classes.push_back(line);
//加载网络 //加载网络
cv::dnn::Net net = cv::dnn::readNet(".\\darknet\\detect.cfg", ".\\darknet\\detect.weights"); cv::dnn::Net net = cv::dnn::readNet(".\\darknet\\detect.cfg", ".\\darknet\\detect.weights");
net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV); net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
...@@ -6087,9 +6051,15 @@ int eyemAllMindImpl(EyemImage tpImage) ...@@ -6087,9 +6051,15 @@ int eyemAllMindImpl(EyemImage tpImage)
//为网络输入新值 //为网络输入新值
net.setInput(blob); net.setInput(blob);
double begin0 = (double)cv::getTickCount();
//获取预测结果 //获取预测结果
std::vector<cv::Mat> outputBlobs; std::vector<cv::Mat> outputBlobs;
net.forward(outputBlobs, outBlobNames); net.forward(outputBlobs, outBlobNames);
std::cout << ((double)cv::getTickCount() - begin0) / cv::getTickFrequency() << std::endl;
std::vector<int> classIds;//储存识别类的索引
std::vector<float> confidences;//储存置信度
std::vector<cv::Rect> bboxes;//储存边框
for (int n = 0; n < outputBlobs.size(); n++) { for (int n = 0; n < outputBlobs.size(); n++) {
for (int row = 0; row < outputBlobs[n].rows; row++) { for (int row = 0; row < outputBlobs[n].rows; row++) {
...@@ -6101,31 +6071,71 @@ int eyemAllMindImpl(EyemImage tpImage) ...@@ -6101,31 +6071,71 @@ int eyemAllMindImpl(EyemImage tpImage)
//取得最大分数值与索引 //取得最大分数值与索引
cv::minMaxLoc(prob, 0, &confidence, 0, &classIdPoint); cv::minMaxLoc(prob, 0, &confidence, 0, &classIdPoint);
//如果置信度大于阈值
if (confidence > 0.35) {
cv::Mat dt = outputBlobs[n]; cv::Mat dt = outputBlobs[n];
cv::Mat x= outputBlobs[n](cv::Range(row, row + 1), cv::Range(0, 5)); cv::Mat x = outputBlobs[n](cv::Range(row, row + 1), cv::Range(0, 5));
//如果置信度大于阈值 int cx = cvRound(x.ptr<float>(0)[0] * (float)image.cols);
if (confidence > 0.5) { int cy = cvRound(x.ptr<float>(0)[1] * (float)image.rows);
/*int centerX = (int)(data[0] * frame.cols); int w = cvRound(x.ptr<float>(0)[2] * (float)image.cols);
int centerY = (int)(data[1] * frame.rows); int h = cvRound(x.ptr<float>(0)[3] * (float)image.rows);
int width = (int)(data[2] * frame.cols);
int height = (int)(data[3] * frame.rows); int left = cx - w / 2;
int left = centerX - width / 2; int top = cy - h / 2;
int top = centerY - height / 2;*/
//classIds.push_back(classIdPoint.x); classIds.push_back(classIdPoint.x);
//confidences.push_back((float)confidence); confidences.push_back((float)confidence);
//boxes.push_back(cv::Rect(left, top, width, height)); bboxes.push_back(cv::Rect(left, top, w, h));
} }
} }
} }
//cv::Mat showMat; //cv::Mat showMat;
//cv::cvtColor(image) //cv::cvtColor(image)
//绘制结果
postprocess(image, outputBlobs); std::vector<int> indices;
//非极大值抑制
cv::dnn::NMSBoxes(bboxes, confidences, 0.5, 0.4, indices);
for (int i = 0; i < indices.size(); i++) {
int idx = indices[i];
cv::Rect bbox = bboxes[idx] & cv::Rect(0, 0, image.cols, image.rows);
//标签
std::string label = cv::format("%.2f", confidences[idx]);
if (!classes.empty()) {
label = classes[classIds[idx]] + ":" + label;
}
int baseLine;
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
cv::Scalar pal(0, 255, 0);
cv::rectangle(image, bbox, pal);
cv::rectangle(image, cv::Point(bbox.x, bbox.y - labelSize.height - baseLine), cv::Point(bbox.x + labelSize.width, bbox.y), pal, cv::FILLED);
cv::putText(image, label, cv::Point(bbox.tl().x, bbox.tl().y - baseLine), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0), 1);
}
///<输出计数结果标记图像
{
tpDstImg->iWidth = image.cols; tpDstImg->iHeight = image.rows; tpDstImg->iDepth = image.depth(); tpDstImg->iChannels = image.channels();
//内存尺寸
int _Size = tpDstImg->iWidth*tpDstImg->iHeight*tpDstImg->iChannels * sizeof(uint8_t);
//分配初始化内存
tpDstImg->vpImage = (uint8_t *)malloc(_Size);
if (NULL == tpDstImg->vpImage)
return FUNC_NOT_ENOUGH_MEM;
memset(tpDstImg->vpImage, 0, _Size);
//拷贝数据
memcpy(tpDstImg->vpImage, image.data, _Size);
}
return FUNC_OK; return FUNC_OK;
} }
\ No newline at end of file \ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!