Bläddra i källkod

first show sign detection

Your Name 1 år sedan
förälder
incheckning
f84bb9fefd

+ 82 - 0
src/detection/show_sign_detection/imageBuffer.h

@@ -0,0 +1,82 @@
+#ifndef IMAGEBUFFER_H
+#define IMAGEBUFFER_H
+
+#include <opencv2/opencv.hpp>
+#include <mutex>
+#include <condition_variable>
+#include <queue>
+template<typename T>
+class ConsumerProducerQueue
+{
+
+public:
+    ConsumerProducerQueue(int mxsz,bool dropFrame) :
+            maxSize(mxsz),dropFrame(dropFrame)
+    { }
+
+    bool add(T request)
+    {
+        std::unique_lock<std::mutex> lock(mutex);
+        if(dropFrame && isFull())
+        {
+            //lock.unlock();
+            //return false;
+            cpq.pop();
+            cpq.push(request);
+            cond.notify_all();
+            return true;
+        }
+        else {
+            cond.wait(lock, [this]() { return !isFull(); });
+            cpq.push(request);
+            //lock.unlock();
+            cond.notify_all();
+            return true;
+        }
+    }
+    void consume(T &request)
+    {
+        std::unique_lock<std::mutex> lock(mutex);
+        cond.wait(lock, [this]()
+        { return !isEmpty(); });
+        request = cpq.front();
+        cpq.pop();
+        //lock.unlock();
+        cond.notify_all();
+
+    }
+
+    bool isFull() const
+    {
+        return cpq.size() >= maxSize;
+    }
+
+    bool isEmpty() const
+    {
+        return cpq.size() == 0;
+    }
+
+    int length() const
+    {
+        return cpq.size();
+    }
+
+    void clear()
+    {
+        std::unique_lock<std::mutex> lock(mutex);
+        while (!isEmpty())
+        {
+            cpq.pop();
+        }
+        lock.unlock();
+        cond.notify_all();
+    }
+
+private:
+    std::condition_variable cond;  //条件变量允许通过通知进而实现线程同步
+    std::mutex mutex;     //提供了多种互斥操作,可以显式避免数据竞争
+    std::queue<T> cpq;    //容器适配器,它给予程序员队列的功能
+    int maxSize;
+    bool dropFrame;
+};
+#endif // IMAGEBUFFER_H

+ 116 - 0
src/detection/show_sign_detection/main.cpp

@@ -0,0 +1,116 @@
+#include <iostream>
+#include <opencv2/opencv.hpp>
+#include "opencv2/imgcodecs/legacy/constants_c.h"
+#include <thread>
+#include <vector>
+#include "modulecomm.h"
+#include "cameraobject.pb.h"
+#include "cameraobjectarray.pb.h"
+#include "rawpic.pb.h"
+#include "imageBuffer.h"
+#include "ivlog.h"
+#include "ivfault.h"
+
+bool ivlog_flag =true;
+
+iv::Ivfault *gfault = nullptr;
+iv::Ivlog *givlog = nullptr;
+
+ConsumerProducerQueue<cv::Mat> * imageBuffer_sign =
+        new ConsumerProducerQueue<cv::Mat>(5,true);
+
+void ListenSignImage(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."<<std::endl;
+        return;
+    }
+    cv::Mat mat(pic.height(),pic.width(),pic.mattype());
+    if(pic.type() == 1)
+        memcpy(mat.data,pic.picdata().data(),mat.rows*mat.cols*mat.elemSize());
+    else
+    {
+        //     mat.release();
+        std::vector<unsigned char> buff(pic.picdata().data(),pic.picdata().data() + pic.picdata().size());
+        mat = cv::imdecode(buff,CV_LOAD_IMAGE_COLOR);
+    }
+    imageBuffer_sign->add(mat);
+    mat.release();
+}
+
+
+void ListenSignArray(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
+{
+    //if(nSize<10)return;
+    iv::vision::cameraobjectarray light_array;
+    if(false == light_array.ParseFromArray(strdata,nSize))
+    {
+        std::cout<<"listen signarray data fail."<<std::endl;
+        return;
+    }
+    for (int i=0;i<light_array.obj_size();i++) {
+        std::cout<<" index: "<<light_array.obj(i).id()
+                 <<" type: "<<light_array.obj(i).type()
+                 <<" confidence: "<<light_array.obj(i).con()
+                 <<" x: "<<light_array.obj(i).x()
+                 <<" y: "<<light_array.obj(i).y()
+                 <<" w: "<<light_array.obj(i).w()
+                 <<" h: "<<light_array.obj(i).h()
+                 <<std::endl;
+    }
+}
+
+
+int main(int argc, char *argv[])
+{
+    gfault = new iv::Ivfault("show_sign_detection");
+    givlog = new iv::Ivlog("show_sign_detection");
+    gfault->SetFaultState(0,0,"show sign detection initialize.");
+
+
+    //void * mpa_signarray;
+    //mpa_signarray= iv::modulecomm::RegisterRecv("signarray",ListenSignArray);
+
+    void * mpa_camera;
+    mpa_camera= iv::modulecomm::RegisterRecv("signimage",ListenSignImage);
+    double waittime = (double)cv::getTickCount();
+    while(1)
+    {
+        cv::Mat sign_image;
+        if(imageBuffer_sign->isEmpty())
+        {
+            double waittotal = (double)cv::getTickCount() - waittime;
+            double totaltime = waittotal/cv::getTickFrequency();
+            //                    if(totaltime>10.0)
+            //                    {
+            //                        cout<<"Cant't get frame and quit"<<endl;
+            //                        lightstart = false;
+            //                        cv::destroyAllWindows();
+            //                        std::cout<<"------end program------"<<std::endl;
+            //                        break;
+            //                    }
+
+            bool print_flag = (totaltime > 1.0);
+            if (print_flag)
+                std::cout<<"Wait for frame "<<totaltime<<"s"<<std::endl;
+
+            if(ivlog_flag & print_flag)
+                givlog->verbose("Wait for frame %f s",totaltime);
+
+            std::this_thread::sleep_for(std::chrono::milliseconds(100));
+            continue;
+        }
+        imageBuffer_sign->consume(sign_image);
+        cv::namedWindow("signResult",cv::WINDOW_NORMAL);
+        cv::imshow("signResult",sign_image);
+        if(cv::waitKey(10)=='q')
+            return 0;
+        //std::this_thread::sleep_for(std::chrono::milliseconds(100));
+        //std::cout<<"show sign result"<<std::endl;
+        waittime = (double)cv::getTickCount();
+    }
+    return 0;
+}

+ 46 - 0
src/detection/show_sign_detection/show_signdetect.pro

@@ -0,0 +1,46 @@
+QT -= gui
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target
+
+
+SOURCES += main.cpp \
+           $$PWD/../../include/msgtype/rawpic.pb.cc \
+           $$PWD/../../include/msgtype/cameraobjectarray.pb.cc \
+           $$PWD/../../include/msgtype/cameraobject.pb.cc
+
+HEADERS += \
+    imageBuffer.h
+
+LIBS += -L"/usr/local/lib" \
+        -lprotobuf
+# c++
+LIBS += -L/usr/lib/aarch64-linux-gnu -lstdc++fs
+LIBS += -L/usr/lib/aarch64-linux-gnu/ -lglog
+
+!include(../../../include/common.pri ) {
+    error( "Couldn't find the common.pri file!" )
+}
+
+!include(../../../include/ivprotobuf.pri ) {
+    error( "Couldn't find the ivprotobuf.pri file!" )
+}
+!include(../../../include/ivopencv.pri ) {
+    error( "Couldn't find the ivopencv.pri file!" )
+}

+ 0 - 0
src/detection/sign_detection_show/main.cpp