diff --git a/Cigarette/CaptureThread.cpp b/Cigarette/CaptureThread.cpp index 5ca7612..fa956f9 100644 --- a/Cigarette/CaptureThread.cpp +++ b/Cigarette/CaptureThread.cpp @@ -47,7 +47,12 @@ void CaptureThread::process( void ) #ifndef IMM_PROCESS EventCallback eventCallback(&pEventCtrl_); eventCallback.p_unit_queue_ = p_unit_queue; +#ifdef SYNC_CAMERA + eventCallback.p_image_sync_arr_ = p_image_sync_arr; + eventCallback.p_image_sync_queue_ = p_image_sync_queue; +#else eventCallback.p_image_queue_ = p_image_queue; +#endif eventCallback.p_result_wait_queue_ = p_result_wait_queue; eventCallback.p_shooted_queue_ = p_shooted_queue; eventCallback.p_double_queue_ = p_double_queue; diff --git a/Cigarette/CaptureThread.h b/Cigarette/CaptureThread.h index 1c492a3..93db8e5 100644 --- a/Cigarette/CaptureThread.h +++ b/Cigarette/CaptureThread.h @@ -17,7 +17,7 @@ #include -#include +#include "common.h" extern SingleCamInfoStruct SingleCamInfo[NumberOfSupportedCameras]; @@ -115,7 +115,12 @@ public: QTimer* m_Timer; uint64_t m_cntGrabbedImages = 0; uint64_t m_cntLastGrabbedImages = 0; +#ifdef SYNC_CAMERA + ImageSyncArr *p_image_sync_arr; + SyncQueue>>* p_image_sync_queue; +#else SyncQueue > *p_image_queue; +#endif ASyncQueue *p_unit_queue; ASyncQueue *p_result_queue; ASyncQueue *p_result_wait_queue; @@ -139,7 +144,12 @@ class EventCallback : public mvIMPACT::acquire::ComponentCallback { public: ASyncQueue *p_unit_queue_; +#ifdef SYNC_CAMERA + ImageSyncArr *p_image_sync_arr_; + SyncQueue>> *p_image_sync_queue_; +#else SyncQueue > *p_image_queue_; +#endif ASyncQueue *p_result_wait_queue_; ASyncQueue *p_shooted_queue_; ASyncQueue *p_double_queue_; @@ -214,7 +224,20 @@ public: cv::Mat roi = long_image(r); image.copyTo(roi); } +#ifdef SYNC_CAMERA + { + std::lock_guard locker(p_image_sync_arr_->lock); + p_image_sync_arr_->image_sync_arr.at(pCaptureThread->Local_Num) = std::make_pair(unit_count, long_image); + p_image_sync_arr_->collect_cnt++; + if (p_image_sync_arr_->collect_cnt == NumberOfSupportedCameras) + { + p_image_sync_queue_->put(p_image_sync_arr_->image_sync_arr); + p_image_sync_arr_->collect_cnt = 0; + } + } +#else p_image_queue_->put(std::make_pair(unit_count, long_image)); +#endif p_shooted_queue_->put(true); } p_unit_queue_->clear(); diff --git a/Cigarette/CaptureThreadBasler.cpp b/Cigarette/CaptureThreadBasler.cpp index e5045c2..36fe988 100644 --- a/Cigarette/CaptureThreadBasler.cpp +++ b/Cigarette/CaptureThreadBasler.cpp @@ -31,7 +31,12 @@ void CaptureThreadBasler::process(void) pDev_->RegisterConfiguration(CfgEvent, Pylon::RegistrationMode_Append, Pylon::Cleanup_None); CSampleImageEventHandler ImageEvent; +#ifdef SYNC_CAMERA + ImageEvent.p_image_sync_arr_ = p_image_sync_arr; + ImageEvent.p_image_sync_queue_ = p_image_sync_queue; +#else ImageEvent.p_image_queue_ = p_image_queue; +#endif ImageEvent.p_unit_queue_ = p_unit_queue; ImageEvent.p_shooted_queue_ = p_shooted_queue; ImageEvent.p_debug_queue_ = p_debug_queue; @@ -41,12 +46,17 @@ void CaptureThreadBasler::process(void) pDev_->RegisterImageEventHandler(&ImageEvent, Pylon::RegistrationMode_Append, Pylon::Cleanup_None); CSampleCameraEventHandler BurstEvent; +#ifdef SYNC_CAMERA + BurstEvent.p_image_sync_arr_ = p_image_sync_arr; + BurstEvent.p_image_sync_queue_ = p_image_sync_queue; +#else + BurstEvent.p_image_queue_ = p_image_queue; +#endif BurstEvent.p_unit_queue_ = p_unit_queue; BurstEvent.p_result_wait_queue_ = p_result_wait_queue; BurstEvent.p_result_queue_ = p_result_queue; BurstEvent.p_shooted_queue_ = p_shooted_queue; BurstEvent.p_double_queue_ = p_double_queue; - BurstEvent.p_image_queue_ = p_image_queue; BurstEvent.pDev__ = pDev_; BurstEvent.pCaptureThreadBasler = this; diff --git a/Cigarette/CaptureThreadBasler.h b/Cigarette/CaptureThreadBasler.h index aa5129b..8c46a83 100644 --- a/Cigarette/CaptureThreadBasler.h +++ b/Cigarette/CaptureThreadBasler.h @@ -111,7 +111,12 @@ private slots: public: int Local_Num; int Shoot_Num; - SyncQueue > *p_image_queue; +#ifdef SYNC_CAMERA + ImageSyncArr *p_image_sync_arr; + SyncQueue>>* p_image_sync_queue; +#else + SyncQueue >* p_image_queue; +#endif ASyncQueue *p_unit_queue; ASyncQueue *p_result_queue; ASyncQueue *p_result_wait_queue; @@ -150,7 +155,12 @@ public: ASyncQueue *p_result_queue_; ASyncQueue *p_double_queue_; ASyncQueue *p_shooted_queue_; - SyncQueue >* p_image_queue_; +#ifdef SYNC_CAMERA + ImageSyncArr *p_image_sync_arr_; + SyncQueue>> *p_image_sync_queue_; +#else + SyncQueue >* p_image_queue_; +#endif Pylon::CBaslerUniversalInstantCamera* pDev__; CaptureThreadBasler* pCaptureThreadBasler = NULL; // Only very short processing tasks should be performed by this method. Otherwise, the event notification will block the @@ -272,7 +282,20 @@ public: cv::Mat roi = long_image(r); image.copyTo(roi); } - p_image_queue_->put(std::make_pair(unit_count, long_image)); +#ifdef SYNC_CAMERA + { + std::lock_guard locker(p_image_sync_arr_->lock); + p_image_sync_arr_->image_sync_arr.at(pCaptureThreadBasler->Local_Num) = std::make_pair(unit_count, long_image); + p_image_sync_arr_->collect_cnt++; + if (p_image_sync_arr_->collect_cnt == NumberOfSupportedCameras) + { + p_image_sync_queue_->put(p_image_sync_arr_->image_sync_arr); + p_image_sync_arr_->collect_cnt = 0; + } + } +#else + p_image_queue_->put(std::make_pair(unit_count, long_image)); +#endif p_shooted_queue_->put(true); } p_unit_queue_->clear(); @@ -289,7 +312,12 @@ class CSampleImageEventHandler : public Pylon::CBaslerUniversalImageEventHandler { public: ASyncQueue* p_unit_queue_; - SyncQueue >* p_image_queue_; +#ifdef SYNC_CAMERA + ImageSyncArr *p_image_sync_arr_; + SyncQueue>> *p_image_sync_queue_; +#else + SyncQueue >* p_image_queue_; +#endif SyncQueue* p_debug_queue_; ASyncQueue *p_shooted_queue_; Pylon::CBaslerUniversalInstantCamera* pDev__; diff --git a/Cigarette/CaptureThreadHIK.cpp b/Cigarette/CaptureThreadHIK.cpp index 8879dfb..ac86ae0 100644 --- a/Cigarette/CaptureThreadHIK.cpp +++ b/Cigarette/CaptureThreadHIK.cpp @@ -38,7 +38,20 @@ inline void FallingGpioEventfunction(MV_EVENT_OUT_INFO* pEventInfo, void* pUser) cv::Mat roi = long_image(r); image.copyTo(roi); } +#ifdef SYNC_CAMERA + { + std::lock_guard locker(CaptureThreadHIKptr->p_image_sync_arr->lock); + CaptureThreadHIKptr->p_image_sync_arr->image_sync_arr.at(CaptureThreadHIKptr->Local_Num) = std::make_pair(unit_count, long_image); + CaptureThreadHIKptr->p_image_sync_arr->collect_cnt++; + if (CaptureThreadHIKptr->p_image_sync_arr->collect_cnt == NumberOfSupportedCameras) + { + CaptureThreadHIKptr->p_image_sync_queue->put(CaptureThreadHIKptr->p_image_sync_arr->image_sync_arr); + CaptureThreadHIKptr->p_image_sync_arr->collect_cnt = 0; + } + } +#else CaptureThreadHIKptr->p_image_queue->put(std::make_pair(unit_count, long_image)); +#endif CaptureThreadHIKptr->p_shooted_queue->put(true); } CaptureThreadHIKptr->p_unit_queue->clear(); diff --git a/Cigarette/CaptureThreadHIK.h b/Cigarette/CaptureThreadHIK.h index 9a36e16..c16c5ba 100644 --- a/Cigarette/CaptureThreadHIK.h +++ b/Cigarette/CaptureThreadHIK.h @@ -3,7 +3,7 @@ #define CaptureThreadHIKH CaptureThreadHIKH //----------------------------------------------------------------------------- #include "hikcamera.h" - +#include "common.h" #include #include #include @@ -80,7 +80,12 @@ private slots: void fpsTimeout(void); public: int Local_Num; - SyncQueue > *p_image_queue; +#ifdef SYNC_CAMERA + ImageSyncArr *p_image_sync_arr; + SyncQueue>>* p_image_sync_queue; +#else + SyncQueue >* p_image_queue; +#endif ASyncQueue *p_unit_queue; ASyncQueue *p_result_queue; ASyncQueue *p_result_wait_queue; diff --git a/Cigarette/Cigarette.vcxproj b/Cigarette/Cigarette.vcxproj index 31f9c98..4ffdbe7 100644 --- a/Cigarette/Cigarette.vcxproj +++ b/Cigarette/Cigarette.vcxproj @@ -347,6 +347,7 @@ + diff --git a/Cigarette/Cigarette.vcxproj.filters b/Cigarette/Cigarette.vcxproj.filters index d77a18f..c080f5e 100644 --- a/Cigarette/Cigarette.vcxproj.filters +++ b/Cigarette/Cigarette.vcxproj.filters @@ -260,6 +260,9 @@ Source Files + + Source Files + @@ -355,6 +358,9 @@ Header Files + + Header Files + diff --git a/Cigarette/cigarette.cpp b/Cigarette/cigarette.cpp index 0df79eb..61fbc29 100644 --- a/Cigarette/cigarette.cpp +++ b/Cigarette/cigarette.cpp @@ -48,7 +48,12 @@ void *HIKCamHandle[NumberOfSupportedCameras]; CaptureThreadHIK* pHIKCaptureThread[NumberOfSupportedCameras]; SyncQueue> *g_save_queue; //图片保存队列 +#ifdef SYNC_CAMERA +ImageSyncArr g_image_sync_arr; +SyncQueue>> *g_image_sync_queue; //int表示一个目标拍了几张 +#else SyncQueue > *g_image_queue[NumberOfSupportedCameras]; //int表示一个目标拍了几张 +#endif ASyncQueue *g_shooted_queue[NumberOfSupportedCameras]; // ASyncQueue *g_result_wait_queue[NumberOfSupportedCameras]; // ASyncQueue *g_double_queue[NumberOfSupportedCameras]; // @@ -156,10 +161,16 @@ Cigarette::Cigarette(QWidget *parent) #endif last_shift = 256; +#ifdef SYNC_CAMERA + g_image_sync_queue = new SyncQueue>>(Queue_Size); + g_image_sync_queue->name = format("sync_image"); +#endif for(int i=0;i >(Queue_Size); g_image_queue[i]->name = format("image_%d", i); +#endif g_result_queue[i] = new ASyncQueue(Queue_Size); g_result_queue[i]->name = format("result_%d", i); g_result_wait_queue[i] = new ASyncQueue(Queue_Size); @@ -315,6 +326,10 @@ Cigarette::Cigarette(QWidget *parent) // 剔除值清0 int ret = m_PLCDevice->write_bit_2_plc(g_modbus_conf.shift, 1);//给PLC发送换班消息 +#ifdef SYNC_CAMERA + CreatWorkThread(0, 0, this); +#endif + //创建相机工作线程 for (int i = 0; i < NumberOfSupportedCameras; i++) { @@ -340,7 +355,13 @@ Cigarette::Cigarette(QWidget *parent) QMessageBox::information(NULL, QStringLiteral("系统自检失败"), QStringLiteral("AI模型1初始化失败,请检查程序完整性"), QMessageBox::Ok); exit(-1); } +#ifdef SYNC_CAMERA + debug_thread[i].init(g_debug_queue[i],i); + connect(&debug_thread[i], SIGNAL(notify(int,int,cv::Mat)), this, SLOT(OnNotifyHub(int,int,cv::Mat))); + debug_thread[i].start_work(); +#else CreatWorkThread(SingleCamInfo[i].CamClass, i, this); +#endif } else { QDateTime ts_start = QDateTime::currentDateTime(); @@ -653,6 +674,10 @@ void Cigarette::pause_work() void Cigarette::Exit() { this->on_btn_pause_released(); +#ifdef SYNC_CAMERA + sync_work_thread.stop(); + delete g_image_sync_queue; +#endif for(int i=0;isetStyleSheet(tr("background-color: rgb(255, 255, 0);")); } } - work_thread[i].stop(); debug_thread[i].stop(); +#ifdef SYNC_CAMERA + +#else + work_thread[i].stop(); delete g_image_queue[i]; +#endif delete g_result_queue[i]; delete g_result_wait_queue[i]; delete g_double_queue[i]; @@ -2776,6 +2805,16 @@ void Cigarette::init_plc_value() void Cigarette::CreatWorkThread(int classid,int Num,Cigarette* classptr) { +#ifdef SYNC_CAMERA + sync_work_thread.init(g_image_sync_queue, g_result_queue[0]); + connect(&sync_work_thread, SIGNAL(notify(int,int,cv::Mat)), classptr, SLOT(OnNotifyHub(int,int,cv::Mat))); + connect(&sync_work_thread, SIGNAL(display_timecost(int,int)), classptr, SLOT(OnDisplayTimeCostHub(int,int))); + connect(&sync_work_thread, SIGNAL(display_check_total(int,long)), classptr, SLOT(OnDisplayCheckNumberHub(int,long))); + connect(&sync_work_thread, SIGNAL(display_jd_no(int,QString)), classptr, SLOT(OnDisplayJdNoHub(int,QString))); + connect(&sync_work_thread, SIGNAL(event_ok(int)), classptr, SLOT(OnOKHub(int))); + connect(&sync_work_thread, SIGNAL(event_ng(int)), classptr, SLOT(OnNGHub(int))); + sync_work_thread.start_work(); +#else work_thread[Num].init(g_image_queue[Num], g_result_queue[Num], classid, Num); connect(&work_thread[Num], SIGNAL(notify(int,int,cv::Mat)), classptr, SLOT(OnNotifyHub(int,int,cv::Mat))); connect(&work_thread[Num], SIGNAL(display_timecost(int,int)), classptr, SLOT(OnDisplayTimeCostHub(int,int))); @@ -2787,6 +2826,7 @@ void Cigarette::CreatWorkThread(int classid,int Num,Cigarette* classptr) debug_thread[Num].init(g_debug_queue[Num],Num); connect(&debug_thread[Num], SIGNAL(notify(int,int,cv::Mat)), classptr, SLOT(OnNotifyHub(int,int,cv::Mat))); debug_thread[Num].start_work(); +#endif } #define InitPtrMat_init(a,b)\ @@ -2908,8 +2948,12 @@ bool Cigarette::ControlCamOpenOrClose(int Num,bool OpenOrClose) pac->gain.write(g_sys_conf.gain[Num]); } pCaptureThread[Num] = new CaptureThread(BalluffCamera::devMgr[SingleCamInfo[Num].unfiltered_num], false, pFI[Num],Num); - +#ifdef SYNC_CAMERA + pCaptureThread[Num]->p_image_sync_queue = g_image_sync_queue; + pCaptureThread[Num]->p_image_sync_arr = &g_image_sync_arr; +#else pCaptureThread[Num]->p_image_queue = g_image_queue[Num]; +#endif pCaptureThread[Num]->p_result_queue = g_result_queue[Num]; pCaptureThread[Num]->p_result_wait_queue = g_result_wait_queue[Num]; pCaptureThread[Num]->p_double_queue = g_double_queue[Num]; @@ -3024,8 +3068,12 @@ bool Cigarette::ControlCamOpenOrClose(int Num,bool OpenOrClose) baslerCamera->BinningVertical.SetValue(2); pBaslerCaptureThread[Num] = new CaptureThreadBasler(baslerCamera, false, Num,g_sys_conf.shoot[Num]); - - pBaslerCaptureThread[Num]->p_image_queue = g_image_queue[Num]; +#ifdef SYNC_CAMERA + pCaptureThread[Num]->p_image_sync_queue = g_image_sync_queue; + pCaptureThread[Num]->p_image_sync_arr = &g_image_sync_arr; +#else + pCaptureThread[Num]->p_image_queue = g_image_queue[Num]; +#endif pBaslerCaptureThread[Num]->p_result_queue = g_result_queue[Num]; pBaslerCaptureThread[Num]->p_result_wait_queue = g_result_wait_queue[Num]; pBaslerCaptureThread[Num]->p_double_queue = g_double_queue[Num]; @@ -3150,7 +3198,12 @@ bool Cigarette::ControlCamOpenOrClose(int Num,bool OpenOrClose) pHIKCaptureThread[Num] = new CaptureThreadHIK(camhandle, false,Num); - pHIKCaptureThread[Num]->p_image_queue = g_image_queue[Num]; +#ifdef SYNC_CAMERA + pCaptureThread[Num]->p_image_sync_queue = g_image_sync_queue; + pCaptureThread[Num]->p_image_sync_arr = &g_image_sync_arr; +#else + pCaptureThread[Num]->p_image_queue = g_image_queue[Num]; +#endif pHIKCaptureThread[Num]->p_result_queue = g_result_queue[Num]; pHIKCaptureThread[Num]->p_debug_queue = g_debug_queue[Num]; pHIKCaptureThread[Num]->p_result_wait_queue = g_result_wait_queue[Num]; diff --git a/Cigarette/cigarette.h b/Cigarette/cigarette.h index a6b50a7..385f7e3 100644 --- a/Cigarette/cigarette.h +++ b/Cigarette/cigarette.h @@ -10,7 +10,11 @@ #include "balluffcamera.h" #include "baslercamera.h" #include "hikcamera.h" +#ifdef SYNC_CAMERA +#include "syncworkthread.h" +#else #include "workthread.h" +#endif #include "db_label.h" #include "common.h" #include "modbus.h" @@ -162,8 +166,12 @@ public: }; widget_info display_lable_info[NumberOfSupportedCameras][2]; widget_info rotate_info[NumberOfSupportedCameras]; - + +#ifdef SYNC_CAMERA + SyncWorkThread sync_work_thread; +#else WorkThread work_thread[NumberOfSupportedCameras]; +#endif DebugThread debug_thread[NumberOfSupportedCameras]; struct export_info { diff --git a/Cigarette/common.h b/Cigarette/common.h index 4465bf4..6cf97f4 100644 --- a/Cigarette/common.h +++ b/Cigarette/common.h @@ -23,6 +23,8 @@ //#define identify_Hik_YSXID//识别海康相机YSXID //#define __ExportData // 输出检测数据到XML文档 #define DRAW_RECT // 鼠标画框功能 +//#define SYNC_CAMERA //相机同步处理图片 + #define Queue_Size 15 #define Unit_Queue_Size Queue_Size*3 @@ -151,6 +153,20 @@ public: } }; +#ifdef SYNC_CAMERA +class ImageSyncArr +{ +public: + std::mutex lock; + std::vector> image_sync_arr = std::vector>(NumberOfSupportedCameras); + uint8_t collect_cnt; + + ImageSyncArr() + { + collect_cnt = 0; + } +}; +#endif //Modbus地址设置 class ModbusConf { diff --git a/Cigarette/syncworkthread.cpp b/Cigarette/syncworkthread.cpp new file mode 100644 index 0000000..d363d72 --- /dev/null +++ b/Cigarette/syncworkthread.cpp @@ -0,0 +1,327 @@ +#include "workthread.h" +#include "alg_jd.h" +#include "common.h" +#include "balluffcamera.h" +#include "baslercamera.h" +#include "threadSend.h" +#include +#include "exportData.h" +#include + +extern AlgJd alg_jd[NumberOfSupportedCameras]; //检测胶点的AI算法 +extern ConfPath g_conf_path; +extern SysConf g_sys_conf; //系统配置参数 +extern DisplayLabelConf g_display_label_conf[NumberOfSupportedCameras]; + +extern int rotationAngle[NumberOfSupportedCameras]; //图片旋转角度 +extern bool isNeedRotate[NumberOfSupportedCameras]; + +extern SyncQueue >* g_save_queue; //图片保存队列 +extern SyncQueue>* g_image_queue[NumberOfSupportedCameras]; //int表示一个目标拍了几张 +#ifdef __UDPSend +extern SyncQueue<_UDPSendInfo>* UDP_Info_queue; +#endif +#ifdef __TCPSend +extern SyncQueue<_TCPSendInfo>* TCP_Info_queue; +#endif +#ifdef __ExportData +extern ExportDataThread exportDataThread; +#endif +extern PLCDevice* m_PLCDevice; +extern bool g_debug_mode; + +SyncWorkThread::~SyncWorkThread() +{ + stop(); + std::vector> queue; + local_g_image_sync_queue->put(queue); + quit(); + wait(); +} + +void SyncWorkThread::init(SyncQueue>>* image_ptr, ASyncQueue* result_ptr) +{ + local_g_image_sync_queue = image_ptr; + local_g_result_queue = result_ptr; + b_quit = false; + frame_total = 0; +} +void SyncWorkThread::start_work() +{ + start(HighestPriority); +} +void SyncWorkThread::stop() +{ + b_quit = true; +} + +void SyncWorkThread::run() +{ + try{ + uint32_t result_index[NumberOfSupportedCameras] = {0}; + while (!b_quit) { + QDateTime now_ts = QDateTime::currentDateTime(); + std::vector> element_vec; + local_g_image_sync_queue->take(element_vec); + + bool IsNGForAll = false; + for(int i=0;i element; + int unit_count = element.first; + cv::Mat image = element.second; + + {//不要删掉这个括号,用来定义锁的作用域 + std::lock_guard locker(g_sys_conf.lock); + local_SysConf.save = g_sys_conf.save; + local_SysConf.shoot[local_camera_number] = g_sys_conf.shoot[local_camera_number]; + local_SysConf.MisMatchAct = g_sys_conf.MisMatchAct; + local_SysConf.ConfThreshold = g_sys_conf.ConfThreshold;// + for (int i = 0; i < 3; i++)local_SysConf.no[local_camera_number][i] = g_sys_conf.no[local_camera_number][i]; + +#ifdef DRAW_RECT + std::lock_guard locker2(g_display_label_conf[local_camera_number].lock); + local_DisplayLabelConf.leftButtonDownFlag = g_display_label_conf[local_camera_number].leftButtonDownFlag; + local_DisplayLabelConf.Flag[0] = g_display_label_conf[local_camera_number].Flag[0]; + local_DisplayLabelConf.Flag[1] = g_display_label_conf[local_camera_number].Flag[1]; + local_DisplayLabelConf.originalPoint = g_display_label_conf[local_camera_number].originalPoint; + local_DisplayLabelConf.processPoint = g_display_label_conf[local_camera_number].processPoint; + local_DisplayLabelConf.RectVet[0] = g_display_label_conf[local_camera_number].RectVet[0]; + local_DisplayLabelConf.RectVet[1] = g_display_label_conf[local_camera_number].RectVet[1]; +#endif + } + + #ifdef __UDPSend + _UDPSendInfo UDPSendInfo; + UDPSendInfo.FrameID = info_frame; + UDPSendInfo.index = local_camera_number; + #endif + +#ifdef __TCPSend + _TCPSendInfo TCPSendInfo; +#endif + if (!image.data) + { + continue; //图像为空,跳过 + } + if (image.channels() == 1) + { + cv::cvtColor(image, image, CV_BGR2RGB); //灰度图像转为彩色图像 + } + if (local_SysConf.shoot[local_camera_number] == unit_count) + { + std::vector vec_in; + int w = image.cols; + int h = image.rows / unit_count; + for (int index = 0; index < unit_count; index++) { + cv::Rect temp_Rect(0, h * index, w, h); + cv::Mat temp_image = image(temp_Rect).clone(); + if (isNeedRotate[local_camera_number]) { + if (rotationAngle[local_camera_number] != (cv::ROTATE_90_COUNTERCLOCKWISE + 1)) + { + cv::rotate(temp_image, temp_image, rotationAngle[local_camera_number]); + } + } + vec_in.push_back(temp_image.clone()); + } + + std::vector vec_out; + std::vector > > vec_results; + QDateTime ts_start = QDateTime::currentDateTime(); + if(unit_count == 1){ + std::vector > results; + cv::Mat imagein,imageout; + imagein = vec_in[0]; + alg_jd[local_camera_number].detect(imagein, imageout, results); + vec_out.push_back(imageout.clone()); + vec_results.push_back(results); + }else{ + alg_jd[local_camera_number].detect_batch(vec_in, vec_out, vec_results); + } + QDateTime ts_jd = QDateTime::currentDateTime(); + int time_process = ts_start.msecsTo(ts_jd); + emit display_timecost(local_camera_number, time_process); +#ifdef __UDPSend + UDPSendInfo.timecost = QString::number(time_process); +#endif + cv::Mat image1; + cv::Mat image2; + + QString jd_no; + for (int index = 0; index < unit_count; index++) { + jd_no += QString::number(vec_results[index].size()) + ","; + } + jd_no.chop(1); + emit display_jd_no(local_camera_number, jd_no); +#ifdef __UDPSend + UDPSendInfo.JD = jd_no; +#endif + bool IsNG = false; + int ngReason = 0; + QMap ng_reason_maps; + ng_reason_maps[0] = "unknow"; + ng_reason_maps[1] = "less_than_setting"; + ng_reason_maps[2] = "too_diff_from_model"; + ng_reason_maps[3] = "out_of_setting_range"; + for(int index=0;indexput(std::make_pair(filename, m)); +#ifdef __TCPSend + TCPSendInfo.pics_name = filename; + TCP_Info_queue->put(TCPSendInfo); +#endif + } + } + + if (unit_count >= 2){ + image1 = vec_out[(result_index[i]) % 2].clone(); +#ifdef DRAW_RECT + IsNG|=CheckSelectRects(image1,vec_results,(result_index[i]) % 2, local_DisplayLabelConf, 0); + if (IsNG) { + ngReason = 2; + } +#endif + }else { + image1 = vec_out[0].clone(); +#ifdef DRAW_RECT + IsNG|=CheckSelectRects(image1,vec_results,0,local_DisplayLabelConf,0); + if (IsNG) { + ngReason = 2; + } +#endif + } +#ifdef DRAW_RECT + DrawSelectRects(image1, local_DisplayLabelConf, 0); +#endif + + if (unit_count >= 3) { + image2 = vec_out[2].clone(); +#ifdef DRAW_RECT + DrawSelectRects(image2, local_DisplayLabelConf, 1); + IsNG|=CheckSelectRects(image1,vec_results,2,local_DisplayLabelConf,1); + if (IsNG) { + ngReason = 3; + } +#endif + } + result_index[i]++; + + if (!IsNG) + { + if (!g_debug_mode) + { + emit event_ok(local_camera_number); + local_g_result_queue->put(true); + } + } + else + { + if (!g_debug_mode) + { + emit event_ng(local_camera_number); + local_g_result_queue->put(false); + IsNGForAll = TRUE; + } + + if ((local_SysConf.save == 2) || (local_SysConf.save == 1)) + { + for(int index=0;indexput(std::make_pair(file_name.toLocal8Bit().constData(), m)); +#ifdef __TCPSend + TCPSendInfo.pics_name = file_name.toLocal8Bit().constData(); + TCP_Info_queue->put(TCPSendInfo); +#endif + m = vec_out[index].clone(); + file_name = g_conf_path.save_pics_path + "/ng_result/" + + now_ts.toString("yyyy-MM-dd") + "/" + + QString::number(local_camera_number + 1) + "/" + QString::number(index + 1) + "/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; + remotePath = "/image/ng_result/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; + //g_save_queue->put(std::make_pair(file_name.toStdString(), m)); + g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + +#ifdef __TCPSend + TCPSendInfo.pics_name = file_name.toLocal8Bit().constData(); + TCP_Info_queue->put(TCPSendInfo); +#endif + } + } + } + if (!g_debug_mode) + { + emit display_check_total(local_camera_number, ++frame_total); + //exportDataInfo.cameraTotal = frame_total; + + emit notify(local_camera_number, 0, image1); + if (unit_count >= 3) + emit notify(local_camera_number, 1, image2); + } + + } + else + { + //保证不错位 + if (!g_debug_mode) + { + if (local_SysConf.MisMatchAct == 1)//as ng + emit event_ng(local_camera_number); + else if (local_SysConf.MisMatchAct == 0)//as ok + emit event_ok(local_camera_number); + emit display_check_total(local_camera_number, ++frame_total); + qDebug() << local_camera_number << "#camera# " << now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx " << unit_count; + } + } +#ifdef __UDPSend + UDP_Info_queue->put(UDPSendInfo); +#endif + } + if(IsNGForAll) + { + /*to do somthing*/ + } + } + } + catch (cv::Exception& e) + { + const char* err_msg = e.what(); + std::cout << "exception caught: " << err_msg << std::endl; + } +} \ No newline at end of file diff --git a/Cigarette/syncworkthread.h b/Cigarette/syncworkthread.h new file mode 100644 index 0000000..f200475 --- /dev/null +++ b/Cigarette/syncworkthread.h @@ -0,0 +1,43 @@ +#pragma once +#include +#include +#include +#include +#include +#include "SyncQueue.h" +#include "ASyncQueue.h" +#include "common.h" + +class SyncWorkThread : public QThread +{ + Q_OBJECT +signals: + void notify(int Num,int Cnt,cv::Mat); + void display_timecost(int Num,int ms); + void display_check_total(int Num,long no); + void display_jd_no(int Num,QString jd_no); + + void event_ok(int Num); + void event_ng(int Num); + +public: + SyncWorkThread(QObject *parent = 0): QThread(parent) + { + + } + ~SyncWorkThread(); + + void init(SyncQueue>>* image_ptr, ASyncQueue* result_ptr); + void start_work(); + void stop(); +protected: + void run(); +public: + int local_camera_number; + SyncQueue>>* local_g_image_sync_queue; + ASyncQueue* local_g_result_queue; + bool b_quit; + long frame_total; + SysConf local_SysConf; + DisplayLabelConf local_DisplayLabelConf; +};