#include "BehaviorDetect.h"
BehaviorDetect::BehaviorDetect()
{
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line)) this->class_names.push_back(line);
this->num_class = class_names.size();
this->inpHeight = imgsize;
this->inpWidth = imgsize;
this->net = readNet(model_path);
this->net.setPreferableBackend(DNN_BACKEND_CUDA);
this->net.setPreferableTarget(DNN_TARGET_CUDA);
if (net.empty())
{
isBehaviorDetectOK = false;
return;
}
else
{
isBehaviorDetectOK = true;
}
}
BehaviorDetect::~BehaviorDetect()
{
}
void BehaviorDetect::detect(Mat srcimg, vector<struct_behavior> &res1)
{
res1.clear();
struct_behavior tempResult;
int newh = 0, neww = 0, top = 0, left = 0;
Mat cv_image = srcimg.clone();
Mat dst = this->resize_image(cv_image, &newh, &neww, &top, &left);
this->normalize(dst);
Mat blob = blobFromImage(dst);
this->net.setInput(blob);
vector<Mat> outs;
this->net.forward(outs, this->net.getUnconnectedOutLayersNames());
//generate proposals
vector<BoxInfo> generate_boxes;
const float* preds = (float*)outs[0].data;
generate_proposal(generate_boxes, preds);
nms(generate_boxes);
float ratioh = (float)cv_image.rows / newh;
float ratiow = (float)cv_image.cols / neww;
for (size_t i = 0; i < generate_boxes.size(); ++i)
{
int xmin = (int)max((generate_boxes[i].x1 - left)*ratiow, 0.f);
int ymin = (int)max((generate_boxes[i].y1 - top)*ratioh, 0.f);
int xmax = (int)min((generate_boxes[i].x2 - left)*ratiow, (float)cv_image.cols);
int ymax = (int)min((generate_boxes[i].y2 - top)*ratioh, (float)cv_image.rows);
rectangle(srcimg, Point(xmin, ymin), Point(xmax, ymax), Scalar(0, 0, 255), 2);
string label = format("%.2f", generate_boxes[i].score);
label = this->class_names[generate_boxes[i].label] + ":" + label;
putText(srcimg, label, Point(xmin, ymin - 5), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 1);
tempResult.confidence = generate_boxes[i].score;
tempResult.bbox = Rect(generate_boxes[i].x1, generate_boxes[i].y1, generate_boxes[i].x2- generate_boxes[i].x1, generate_boxes[i].y2- generate_boxes[i].y1);
tempResult.obj_name = this->class_names[generate_boxes[i].label];
res1.push_back(tempResult);
}
}
cv::Mat BehaviorDetect::resize_image(Mat srcimg, int *newh, int *neww, int *top, int *left)
{
int srch = srcimg.rows, srcw = srcimg.cols;
*newh = this->inpHeight;
*neww = this->inpWidth;
Mat dstimg;
if (this->keep_ratio && srch != srcw) {
float hw_scale = (float)srch / srcw;
if (hw_scale > 1) {
*newh = this->inpHeight;
*neww = int(this->inpWidth / hw_scale);
resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
*left = int((this->inpWidth - *neww) * 0.5);
copyMakeBorder(dstimg, dstimg, 0, 0, *left, this->inpWidth - *neww - *left, BORDER_CONSTANT, 0);
}
else {
*newh = (int)this->inpHeight * hw_scale;
*neww = this->inpWidth;
resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
*top = (int)(this->inpHeight - *newh) * 0.5;
copyMakeBorder(dstimg, dstimg, *top, this->inpHeight - *newh - *top, 0, 0, BORDER_CONSTANT, 0);
}
}
else {
resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
}
return dstimg;
}
void BehaviorDetect::normalize(Mat& img)
{
img.convertTo(img, CV_32F);
int i = 0, j = 0;
for (i = 0; i < img.rows; i++)
{
float* pdata = (float*)(img.data + i * img.step);
for (j = 0; j < img.cols; j++)
{
pdata[0] = (pdata[0] - this->mean[0]) / this->std[0];
pdata[1] = (pdata[1] - this->mean[1]) / this->std[1];
pdata[2] = (pdata[2] - this->mean[2]) / this->std[2];
pdata += 3;
}
}
}
void BehaviorDetect::softmax_(const float* x, float* y, int length)
{
float sum = 0;
int i = 0;
for (i = 0; i < length; i++)
{
y[i] = exp(x[i]);
sum += y[i];
}
for (i = 0; i < length; i++)
{
y[i] /= sum;
}
}
void BehaviorDetect::generate_proposal(vector<BoxInfo>& generate_boxes, const float* preds)
{
const int reg_1max = reg_max + 1;
const int len = this->num_class + 4 * reg_1max;
for (int n = 0; n < this->num_stages; n++)
{
const int stride_ = this->stride[n];
const int num_grid_y = (int)ceil((float)this->inpHeight / stride_);
const int num_grid_x = (int)ceil((float)this->inpWidth / stride_);
////cout << "num_grid_x=" << num_grid_x << ",num_grid_y=" << num_grid_y << endl;
for (int i = 0; i < num_grid_y; i++)
{
for (int j = 0; j < num_grid_x; j++)
{
int max_ind = 0;
float max_score = 0;
for (int k = 0; k < num_class; k++)
{
if (preds[k] > max_score)
{
max_score = preds[k];
max_ind = k;
}
}
if (max_score >= score_threshold)
{
const float* pbox = preds + this->num_class;
float dis_pred[4];
float* y = new float[reg_1max];
for (int k = 0; k < 4; k++)
{
softmax_(pbox + k * reg_1max, y, reg_1max);
float dis = 0.f;
for (int l = 0; l < reg_1max; l++)
{
dis += l * y[l];
}
dis_pred[k] = dis * stride_;
}
delete[] y;
/*float pb_cx = (j + 0.5f) * stride_ - 0.5;
float pb_cy = (i + 0.5f) * stride_ - 0.5;*/
float pb_cx = j * stride_;
float pb_cy = i * stride_;
float x0 = pb_cx - dis_pred[0];
float y0 = pb_cy - dis_pred[1];
float x1 = pb_cx + dis_pred[2];
float y1 = pb_cy + dis_pred[3];
generate_boxes.push_back(BoxInfo{ x0, y0, x1, y1, max_score, max_ind });
}
preds += len;
}
}
}
}
void BehaviorDetect::nms(vector<BoxInfo>& input_boxes)
{
sort(input_boxes.begin(), input_boxes.end(), [](BoxInfo a, BoxInfo b) { return a.score > b.score; });
vector<float> vArea(input_boxes.size());
for (int i = 0; i < int(input_boxes.size()); ++i)
{
vArea[i] = (input_boxes.at(i).x2 - input_boxes.at(i).x1 + 1)
* (input_boxes.at(i).y2 - input_boxes.at(i).y1 + 1);
}
vector<bool> isSuppressed(input_boxes.size(), false);
for (int i = 0; i < int(input_boxes.size()); ++i)
{
if (isSuppressed[i]) { continue; }
for (int j = i + 1; j < int(input_boxes.size()); ++j)
{
if (isSuppressed[j]) { continue; }
float xx1 = (max)(input_boxes[i].x1, input_boxes[j].x1);
float yy1 = (max)(input_boxes[i].y1, input_boxes[j].y1);
float xx2 = (min)(input_boxes[i].x2, input_boxes[j].x2);
float yy2 = (min)(input_boxes[i].y2, input_boxes[j].y2);
float w = (max)(float(0), xx2 - xx1 + 1);
float h = (max)(float(0), yy2 - yy1 + 1);
float inter = w * h;
float ovr = inter / (vArea[i] + vArea[j] - inter);
if (ovr >= this->nms_threshold)
{
isSuppressed[j] = true;
}
}
}
// return post_nms;
int idx_t = 0;
input_boxes.erase(remove_if(input_boxes.begin(), input_boxes.end(), [&idx_t, &isSuppressed](const BoxInfo& f) { return isSuppressed[idx_t++]; }), input_boxes.end());
}
vector<string> BehaviorDetect::getImages(string path)
{
vector<string> imagesList;
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
hFile = _findfirst(p.assign(path).append("\\*.jpg").c_str(), &fileinfo);
if (hFile != -1) {
do {
//cout << fileinfo.name << endl;
imagesList.push_back(fileinfo.name);//保存类名
} while (_findnext(hFile, &fileinfo) == 0);
}
return imagesList;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
OpenCV调用NanoDet-plus.rar (3个子文件)
OpenCV调用NanoDet-plus
BehaviorDetect.h 1KB
BehaviorDetect.cpp 7KB
main.cpp 1KB
共 3 条
- 1
资源评论
- E寻数据2023-07-08#标题与内容不符 #运行出错 #毫无价值
- zhong_12142023-12-20果断支持这个资源,资源解决了当前遇到的问题,给了新的灵感,感谢分享~
- 嵌入式之觅迹2023-09-15非常有用的资源,有一定的参考价值,受益匪浅,值得下载。
低空飞鱼
- 粉丝: 17
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 111111111111111111
- Screenshot_2024-04-30-21-47-24-26.jpg
- Cpp1.cpp1111111111
- 利用ERP流程操作的整个订单流程.ppt
- 最新二开版本源码博客论坛源码,UI很漂亮,可切换皮肤界面.rar
- ModStartBlog现代化个人博客系统 v5.2.0源码.rar
- 带posix库的mingw编译器
- SoraAI是一款功能强大的AI助手,由OpenAI开发,以其出色的语音识别技术、广泛的知识库和高度的人工智能特性而备受瞩目
- Thinkphp开发大气响应式个人博客青春博客网站源码.rar
- 最新PHP博客网站程序源码 ThinkPHP.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功