#include #include #include #include #include #include #include #include "imageBuffer.h" #include "modulecomm.h" #include "rawpic.pb.h" #include "turnstile.pb.h" #include "yolodetect.h" #include "detect_turnstile.h" using namespace std; using namespace cv; bool start_up = true; bool broken_flag = false; bool test_video = false; string video_path = "20201231144029.avi"; void * g_turnstile; string turnstiledata="turnstiledata"; void * mpa_camera; string cameradata="picfront"; typedef struct frame_info { cv::Mat frame; long long timestamp; }frame_info; ConsumerProducerQueue * imageBuffer = new ConsumerProducerQueue(5,true); //读取视频数据 void ReadFunc(int n) { cv::VideoCapture cap(video_path); if(!cap.isOpened()) { cout<<"camera failed to open"<isFull()) { continue; } if(cap.read(frame)) { frameInfo.frame = frame; imageBuffer->add(frameInfo); } else { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } } //从共享内存中接收摄像头数据 void Listencamera(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) { if(nSize<1000)return; iv::vision::rawpic pic; if(false == pic.ParseFromArray(strdata,nSize)) { std::cout<<"picview Listenpic fail."< buff(pic.picdata().data(),pic.picdata().data() + pic.picdata().size()); mat = cv::imdecode(buff,cv::IMREAD_COLOR); } frame_info img_info; img_info.frame = mat; img_info.timestamp = pic.time(); imageBuffer->add(img_info); mat.release(); } //向共享内存中存入闸机检测结果 void SendDetectResult(frame_info &img_info, bool broken_flag, void* g_name) { iv::vision::turnstile detectResult; detectResult.set_state(broken_flag); iv::vision::rawpic cameraPic; cameraPic.set_time(img_info.timestamp); cameraPic.set_elemsize(img_info.frame.elemSize()); cameraPic.set_width(img_info.frame.cols); cameraPic.set_height(img_info.frame.rows); cameraPic.set_mattype(img_info.frame.type()); std::vector param = std::vector(2); param[0] = cv::IMWRITE_JPEG_QUALITY; param[1] = 95; // default(95) 0-100 std::vector buff; cv::imencode(".jpg",img_info.frame,buff,param); cameraPic.set_picdata(buff.data(),buff.size()); buff.clear(); cameraPic.set_type(2); detectResult.mutable_pic()->CopyFrom(cameraPic); std::string out_result = detectResult.SerializeAsString(); iv::modulecomm::ModuleSendMsg(g_name,out_result.data(),out_result.length()); } int main(int argc, char** argv ) { if(argc==3) { test_video = argv[1]; video_path = argv[2]; } if(argc==2) test_video = argv[1]; if(test_video) std::thread * readthread = new std::thread(ReadFunc,1); else mpa_camera= iv::modulecomm::RegisterRecv(&cameradata[0],Listencamera); g_turnstile = iv::modulecomm::RegisterSend(&turnstiledata[0],1000000,1); NetworkInfo networkInfo; networkInfo.networkType = "yolov4-turnstile"; networkInfo.configFilePath = "model/yolov4-turnstile.cfg"; networkInfo.wtsFilePath = "model/yolov4-turnstile.weights"; networkInfo.deviceType = "kGPU"; networkInfo.inputBlobName = "data"; std::string modelname = "model/yolov4-turnstile.engine"; IExecutionContext* yolo_context{nullptr}; YoloDetect detector(networkInfo,modelname); vector classes = {"open", "close"}; if(start_up) { //加载网络模型,0是指定第一块GPU cudaSetDevice(0); if(!detector.loadModel(yolo_context)) { cout<<"load yolo model failed"< trackers; KalmanTracker::kf_count = 0; // tracking id relies on this, so we have to reset it in each seq. int frame_count = 0; bool calculation_flag = false; frame_info frameInfo; Mat frame; long long millseconds; //时间戳 double waittime = (double)getTickCount(); while (1) { // if(frame_count>45*30) //{ //calculation_flag = true; //break; //} if(imageBuffer->isEmpty()) { double waittotal = (double)getTickCount() - waittime; if(waittotal/cv::getTickFrequency()>30.0) { cout<<"Cant't get frame and quit"<consume(frameInfo); frame = frameInfo.frame; frame_count++; //cout<<"Frame_count: "< detect_results; vector outs; td::bbox_t result; if(detector.process(*yolo_context,frame,detect_results,0.4,0.4)) { for (size_t i = 0; i < detect_results.size(); i++) { cv::Rect r = detector.get_rect(frame, detect_results[i].bbox,detector.m_input_w,detector.m_input_h); result.x = r.x; result.y = r.y; result.w = r.width; result.h = r.height; result.prob = detect_results[i].det_confidence; result.obj_id = detect_results[i].class_id; outs.push_back(result); } } double infertime = (double)getTickCount() - start; //std::cout<< "Total Cost of infertime: " <track_result; //tracking turnstile bool track_flag = td::TrackTurnstile(frame_count,trackers,outs,track_result); td::Drawer(frame, track_result, classes); double total = (double)getTickCount() - start; std::cout<< "Total Cost of Detection: " <0 && calculation_flag) { cout<<">>>>>>>>Start Calculation<<<<<<<<"<= 15*30) { for(unsigned int j=0;j12*30) { broken_flag = true; cout<<"The turnstile is broken"<destroy(); return 0; }