1 Star 0 Fork 0

NJUPT-B523/Intelligent Traffic Violation Detection

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rightpicthread.cpp 18.09 KB
一键复制 编辑 原始数据 按行查看 历史
江斌 提交于 2021-08-22 15:41 . YDS 2019
/*****************************************************************************
* 右线程 功能:对分割成的左图像使用Tengine框架进行目标检测,将检测结果传输给违章
* 判定函数并保留违章证据,生成违章日志报告
* @ELVIS-W ZZH
* date:2019/9/23
* ***************************************************************************/
#include "rightpicthread.h"
//#include "mainwindow.h"
#include <QFileDialog>
#include <qdebug.h>
#include <liclabel.h>
#include "logprinttextbrowser.h"
#include <QTime>
#include <QSoundEffect>
#include <fstream>
#include "keep_rule_rate.h"
extern VideoLabel * videolabel;
extern float videowidth;
extern float videoheight;
extern float vidradio_w;
extern float vidradio_h;
extern cv::Mat img, r_img, light_img;
extern cv::Mat str_license;
extern cv::Mat right_license;
extern cv::Mat wrong_road_license;
extern cv::Rect car_roi_1st;
extern cv::Rect r_rect;
extern cv::Rect light_rect;
extern cv::Rect str_roi_1st;
extern cv::Rect str_roi_2nd;
extern cv::Rect right_roi_1st;
extern cv::Rect right_roi_2nd;
extern cv::Rect wrong_road_roi;
extern bool red_light;
extern float rate;
extern int total_num;
extern int illegal_num;
extern int img_h ;
extern int img_w ;
extern int img_size;
extern illegal_process_r ill_right;
extern keep_rule_rate keep_rule;
extern red_stright red_str;
extern red_right red_ri;
extern wrong_road wr_road;
extern Traffic_light light;
extern Object_detection object_R;
extern LicLabel *stright_pic;
extern LicLabel *right_pic;
extern LicLabel *wrongroad_pic;
extern LogPrintTextBrowser *logprinter;
extern LicLabel *str_ob_rate,*ob_rate_class,*tot_num;
extern VideoCapture capture;
extern bool key_to_detect;
extern QSoundEffect stright_sound;
extern QSoundEffect right_sound;
extern QSoundEffect wrongroad_sound;
extern void DelayMS(unsigned int ms);
RightPicThread::RightPicThread(QObject * par)
{
}
void RightPicThread::stop()
{
stopped=true;
}
void RightPicThread::begin()
{
stopped=false;
}
void RightPicThread::run()
{
int ret = -1;
int x = 1;
float *input_data_r = ( float * )malloc(sizeof(float) * img_size);
//初始化图
//int init_tengine(void)
// 创建图
//graph_t create_graph(const char * graph_name, const char * model_name, workspace_t ws);
graph_t graph_r = create_graph(nullptr, "tengine", "/home/openailab/Desktop/video_object_detection/MobileNetSSD_deploy_r.tmfile");
if(graph_r == nullptr)
{
std::cout << "Create graph failed\n";
std::cout << "errno: " << get_tengine_errno() << "\n";
}
// 获取图的输入张量,并设置张量的形状
int node_idx = 0;//输入节点索引值
int tensor_idx = 0;//张量索引值
//get_graph_input_tensor(graph_t graph, int input_node_idx, int tensor_idx);
tensor_t input_tensor = get_graph_input_tensor(graph_r, node_idx, tensor_idx);
if(input_tensor == nullptr)
{
std::printf("Cannot find input tensor,node_idx: %d,tensor_idx: %d\n", node_idx, tensor_idx);
}
//设置张量形状
//int set_tensor_shape(tensor_t tensor, int dims[], int dim_number);
int dims[] = {1, 3, img_h, img_w};
set_tensor_shape(input_tensor, dims, 4);
//预运行
//int prerun_graph(graph_t graph);
ret = prerun_graph(graph_r);
if(ret != 0)
{
std::cout << "Prerun graph failed, errno: " << get_tengine_errno() << "\n";
}
tensor_t out_tensor = get_graph_output_tensor(graph_r, 0, 0); //"detection_out");
int i = 0; //设置计数器
int j=0;
std::ofstream write;
ill_right.classify.push_back(ill_right.car1); //只把car1,car2放入一次,不用重复,其实就是先放,再修改!
ill_right.classify.push_back(ill_right.car2);
ill_right.classify.push_back(ill_right.car3);
ill_right.classify.push_back(ill_right.car4);
ill_right.classify.push_back(ill_right.car5);
ill_right.classify.push_back(ill_right.car6);
ill_right.classify.push_back(ill_right.car7);
ill_right.classify.push_back(ill_right.car8);
ill_right.classify.push_back(ill_right.car9);
std::vector<std::vector<float> >car;
std::vector<float> car_cpoint;
std::vector<float>tar_str,tar_right,tar_wr;
std::vector<float>dect_1st;
std::vector<float>dect_2nd;
std::vector<float>car1_fir,car1_sec;
std::vector<float>car2_fir,car2_sec;
std::vector<float>car3_fir,car3_sec;
std::vector<float>car4_fir,car4_sec;
std::vector<float>car5_fir,car5_sec;
std::vector<float>car6_fir,car6_sec;
std::vector<float>car7_fir,car7_sec;
std::vector<float>car8_fir,car8_sec;
std::vector<float>car9_fir,car9_sec;
while(!stopped)
{
capture >> img;
if (img.empty())
break ;
r_img = ill_right.slice(img, r_rect);
cv::resize(r_img, r_img, cv::Size(300, 300));
std::vector<cv::Rect> car_roi; //统计车辆ROI区域划定
car_roi = keep_rule.carpass_ROI(car_roi_1st);
std::vector<cv::Rect> rs_roi; //直行判定ROI区域划定
rs_roi = red_str.red_stright_ROI(str_roi_1st, str_roi_2nd);
std::vector<cv::Rect> rr_roi; //右转ROI区域划定
rr_roi = red_ri.red_right_ROI(right_roi_1st, right_roi_2nd);
std::vector<cv::Rect> wr_roi; //机动车闯入非机动车道ROI区域划定
wr_roi = wr_road.wrong_road_ROI(wrong_road_roi);
light_img = ill_right.slice(img, light_rect);
red_light = light.traffic_light(light_img);
if (!red_light && key_to_detect)
{
if(i % 2 == 0) //隔帧运行一次
{
object_R.get_input_data_ssd(r_img, input_data_r, img_h, img_w);
set_tensor_buffer(input_tensor, input_data_r, img_size * 4);//设置张量缓存数据区
ret = run_graph(graph_r, 1);//运行图
if(ret != 0)
{
std::cout << "Run graph failed, errno: " << get_tengine_errno() << "\n";
}
int out_dim[4];
get_tensor_shape(out_tensor, out_dim, 4);
float *outdata = ( float * )get_tensor_buffer(out_tensor);
int num = out_dim[1];
float show_threshold = 0.6;//设置置信度
object_R.post_process_ssd(r_img, show_threshold, outdata, num);
dect_1st.assign(object_R.detection.begin(),object_R.detection.end());
if(j != 0)
{
car = ill_right.obj_process(dect_2nd,dect_1st);
car_cpoint = ill_right.cpoint_cal(dect_1st);
//统计车辆数量
x = keep_rule.car_pass_result(car_roi,car_cpoint);
//遵章率
rate = (float(total_num)-float(illegal_num)+1.0)/(float(total_num)+1.0);
//闯直行红灯判别
tar_str = red_str.rules(rs_roi,car,img);
if (!red_str.second_time)
{
QImage Qimg_stright_lic = QImage( (const unsigned char*)(str_license.data),
str_license.cols, str_license.rows, QImage::Format_RGB888 );
stright_pic->setPixmap( QPixmap::fromImage(Qimg_stright_lic));
Qimg_stright_lic=Qimg_stright_lic.scaled(52,16,Qt::KeepAspectRatio);
stright_pic->setAlignment(Qt::AlignLeft);
stright_pic->setPixmap( QPixmap::fromImage(Qimg_stright_lic));
stright_pic->resize(52,16);
// stright_pic->setScaledContents(true);
stright_pic->show();
ill_right.pub_release(); //在时间不断前进时不断释放空间,定时清理
stright_sound.play();//play the warning tips
QDateTime curDateTime=QDateTime::currentDateTime();
QString timeprint=curDateTime.toString("yyyy-MM-dd hh:mm:ss");
QString logtoprint=timeprint+" 直行车道有车辆闯红灯";
logprinter->append(logtoprint);
std::string s = timeprint.toStdString();
write.open("log_file.txt",std::ios::out|std::ios::app);
write<<"**************************************************"<<"\n";
write<<"时间: "<<s<<"\n";
write<<"地点: 1号监控路口"<<"\n";
write<<"摄像头: 1号"<<"\n";
write<<"违章类型:"<<"直行车道闯红灯"<<"\n";
write.close();
illegal_num +=1;
red_str.first_time = true;
red_str.second_time = true;
}
//ill_right.punish(tar_str);
//闯右转红灯判别
tar_right = red_ri.rules(rr_roi,car,img);
if (!red_ri.second_time)
{
QImage Qimg_right_lic = QImage( (const unsigned char*)(right_license.data),
right_license.cols, right_license.rows, QImage::Format_RGB888 );
right_pic->setPixmap( QPixmap::fromImage(Qimg_right_lic));
Qimg_right_lic=Qimg_right_lic.scaled(52,16,Qt::KeepAspectRatio);
right_pic->setAlignment(Qt::AlignLeft);
right_pic->setPixmap( QPixmap::fromImage(Qimg_right_lic));
right_pic->resize(52,16);
// stright_pic->setScaledContents(true);
right_pic->show();
ill_right.pub_release(); //在时间不断前进时不断释放空间,定时清理
right_sound.play();
QDateTime curDateTime=QDateTime::currentDateTime();
QString timeprint=curDateTime.toString("yyyy-MM-dd hh:mm:ss");
QString logtoprint=timeprint+" 右转车道有车辆闯红灯";
logprinter->append(logtoprint);
std::string s = timeprint.toStdString();
write.open("log_file.txt",std::ios::out|std::ios::app);
write<<"**************************************************"<<"\n";
write<<"时间: "<<s<<"\n";
write<<"地点: 1号监控路口"<<"\n";
write<<"摄像头: 1号"<<"\n";
write<<"违章类型:"<<"右转车道闯红灯"<<"\n";
write.close();
illegal_num +=1;
red_ri.first_time = true;
red_ri.second_time = true;
}
//ill_right.punish(tar_right);
//机动车闯入非机动车道判别
tar_wr = wr_road.rules(wr_roi,car,img);
if (!wr_road.second_time)
{
QImage Qimg_wrongroad_lic = QImage( (const unsigned char*)(wrong_road_license.data),
wrong_road_license.cols, wrong_road_license.rows, QImage::Format_RGB888 );
wrongroad_pic->setPixmap( QPixmap::fromImage(Qimg_wrongroad_lic));
Qimg_wrongroad_lic=Qimg_wrongroad_lic.scaled(52,16,Qt::KeepAspectRatio);
wrongroad_pic->setAlignment(Qt::AlignLeft);
wrongroad_pic->setPixmap( QPixmap::fromImage(Qimg_wrongroad_lic));
wrongroad_pic->resize(52,16);
// stright_pic->setScaledContents(true);
wrongroad_pic->show();
ill_right.pub_release(); //在时间不断前进时不断释放空间,定时清理
wrongroad_sound.play();
QDateTime curDateTime=QDateTime::currentDateTime();
QString timeprint=curDateTime.toString("yyyy-MM-dd hh:mm:ss");
QString logtoprint=timeprint+" 机动车辆闯入非机动车道";
logprinter->append(logtoprint);
std::string s = timeprint.toStdString();
write.open("log_file.txt",std::ios::out|std::ios::app);
write<<"**************************************************"<<"\n";
write<<"时间: "<<s<<"\n";
write<<"地点: 1号监控路口"<<"\n";
write<<"摄像头: 1号"<<"\n";
write<<"违章类型:"<<"机动车辆闯入非机动车道"<<"\n";
write.close();
illegal_num +=1;
wr_road.first_time = true;
wr_road.second_time = true;
}
//ill_right.punish(tar_wr);
}
dect_2nd.assign(dect_1st.begin(),dect_1st.end());
if (i%75 == 0)
{
car1_fir.assign(ill_right.car1.begin(),ill_right.car1.end());
car2_fir.assign(ill_right.car2.begin(),ill_right.car2.end());
car3_fir.assign(ill_right.car3.begin(),ill_right.car3.end());
car4_fir.assign(ill_right.car4.begin(),ill_right.car4.end());
car5_fir.assign(ill_right.car5.begin(),ill_right.car5.end());
car6_fir.assign(ill_right.car6.begin(),ill_right.car6.end());
car7_fir.assign(ill_right.car7.begin(),ill_right.car7.end());
car8_fir.assign(ill_right.car8.begin(),ill_right.car8.end());
car9_fir.assign(ill_right.car9.begin(),ill_right.car9.end());
if (i != 0)
{
if ((int)car1_fir.size() == (int)car1_sec.size())
ill_right.car1.clear();
if ((int)car2_fir.size() == (int)car2_sec.size())
ill_right.car2.clear();
if ((int)car3_fir.size() == (int)car3_sec.size())
ill_right.car3.clear();
if ((int)car4_fir.size() == (int)car4_sec.size())
ill_right.car4.clear();
if ((int)car5_fir.size() == (int)car5_sec.size())
ill_right.car5.clear();
if ((int)car6_fir.size() == (int)car6_sec.size())
ill_right.car6.clear();
if ((int)car7_fir.size() == (int)car7_sec.size())
ill_right.car7.clear();
if ((int)car8_fir.size() == (int)car8_sec.size())
ill_right.car8.clear();
if ((int)car9_fir.size() == (int)car9_sec.size())
ill_right.car9.clear();
}
car1_sec.assign(car1_fir.begin(),car1_fir.end());
car2_sec.assign(car2_fir.begin(),car2_fir.end());
car3_sec.assign(car3_fir.begin(),car3_fir.end());
car4_sec.assign(car4_fir.begin(),car4_fir.end());
car5_sec.assign(car5_fir.begin(),car5_fir.end());
car6_sec.assign(car6_fir.begin(),car6_fir.end());
car7_sec.assign(car7_fir.begin(),car7_fir.end());
car8_sec.assign(car8_fir.begin(),car8_fir.end());
car9_sec.assign(car9_fir.begin(),car9_fir.end());
}
object_R.detection.clear();
r_img.convertTo(r_img,CV_8UC1);
j++;
}
}else
{
ill_right.pub_release(); //绿灯时清除
red_str.first_time = true;
red_str.second_time = true; //green_light start re-decision
red_ri.first_time = true;
red_ri.second_time = true;
wr_road.first_time = true;
wr_road.second_time = true;
}
i++;
tot_num->setText(QString::number(total_num));
str_ob_rate->setText(QString::number(rate*100)+"%");
if(0.95<=rate and rate<=1)
ob_rate_class->setText("良好 ");
else
ob_rate_class->setText("较差");
cv::imshow("r_rect", r_img);
cv::waitKey(2);
DelayMS(2);
/***********************************display module**************************************/
cv::Mat show_img;
cv::cvtColor( img, show_img, CV_BGR2RGB );
QImage Qimg = QImage( (const unsigned char*)(show_img.data), show_img.cols, show_img.rows, QImage::Format_RGB888 );
//显示图片
videowidth=Qimg.width(),videoheight=Qimg.height();
int Fwidth=0;
int Fheight=0; //缩放后的图片大小
float Mul; //记录图片与label大小的比例,用于缩放图片
if(videowidth/1080>=videoheight/720)
Mul=videowidth/1080;
else
Mul=videoheight/720;
Fwidth=videowidth/Mul;
Fheight=videoheight/Mul;
Qimg=Qimg.scaled(Fwidth,Fheight,Qt::KeepAspectRatio);
videolabel->setAlignment(Qt::AlignCenter);
//QPixmap myPixmap;
//myPixmap.fromImage(Qimg);
//videolabel->setPixmap(myPixmap);
videolabel->setPixmap(QPixmap::fromImage(Qimg));
//调整合理尺寸
videolabel->setScaledContents(true);
videolabel->show();
vidradio_w=videowidth/videolabel->width();
vidradio_h=videoheight/videolabel->height();
/**************************************************************************************/
if(i > 1000){
i = 0;
}
}
red_str.red_stright_release();
red_ri.red_right_release();
wr_road.wrong_road_release();
release_graph_tensor(input_tensor);
release_graph_tensor(out_tensor);
ret = postrun_graph(graph_r);
free(input_data_r);
destroy_graph(graph_r);
return;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/njupt-b523/intelligent-traffic-violation-detection.git
git@gitee.com:njupt-b523/intelligent-traffic-violation-detection.git
njupt-b523
intelligent-traffic-violation-detection
Intelligent Traffic Violation Detection
master

搜索帮助