reimplemented threading in opencv plugin
authorKevron Rees <kevron.m.rees@intel.com>
Fri, 31 Jan 2014 23:15:33 +0000 (15:15 -0800)
committerKevron Rees <kevron.m.rees@intel.com>
Fri, 31 Jan 2014 23:15:33 +0000 (15:15 -0800)
plugins/murphyplugin/murphysource.cpp
plugins/opencvlux/CMakeLists.txt
plugins/opencvlux/opencvluxplugin.cpp
plugins/opencvlux/opencvluxplugin.h

index e2ad228..624200c 100644 (file)
 
 #include <murphy/common.h>
 
-#ifdef USE_QT_CORE
+/*#ifdef USE_QT_CORE
 #include <murphy/qt/qt-glue.h>
-#else
+#else*/
+
 #include <murphy/glib/glib-glue.h>
-#endif
+//#endif
 
 // #include <vehicleproperty.h>
 // #include <abstractpropertytype.h>
@@ -281,14 +282,14 @@ MurphySource::MurphySource(AbstractRoutingEngine *re, map<string, string> config
 
     // main loop integration
 
-#ifdef USE_QT_CORE
+/*#ifdef USE_QT_CORE
     m_ml = mrp_mainloop_qt_get();
     debugOut("Murphy plugin initialized using QT mainloop!");
-#else
+#else*/
     GMainLoop *g_ml = g_main_loop_new(NULL, TRUE);
     m_ml = mrp_mainloop_glib_get(g_ml);
     debugOut("Murphy plugin initialized using glib mainloop!");
-#endif
+//#endif
 
     setConfiguration(config);
 }
index b1b4f92..251bc75 100644 (file)
@@ -27,14 +27,31 @@ else(cuda)
        remove_definitions(-DCUDA)
 endif(cuda)
 
-include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${OpenCV_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
+find_package(Qt5Core REQUIRED)
+
+if(Qt5Core_FOUND)
+       message(STATUS "using Qt5")
+
+       set(QT_INCLUDE_DIRS ${Qt5Core_INCLUDE_DIRS} )
+       set(QT_LIBRARIES ${Qt5Core_LIBRARIES} )
+       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Core_EXECUTABLE_COMPILE_FLAGS}")
+       message(STATUS "size of void_p: ${CMAKE_SIZEOF_VOID_P}")
+       if(CMAKE_SIZEOF_VOID_P MATCHES "8")
+               set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcmodel=large")
+       endif(CMAKE_SIZEOF_VOID_P MATCHES "8")
+       add_definitions(${Qt5Core_DEFINITIONS})
+
+endif(Qt5Core_FOUND)
+set(CMAKE_AUTOMOC ON)
+
+include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${OpenCV_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${QT_INCLUDE_DIRS})
 
 set(opencvluxplugin_headers opencvluxplugin.h)
 set(opencvluxplugin_sources opencvluxplugin.cpp)
 
-add_library(opencvluxplugin MODULE ${opencvluxplugin_sources} ${opencvluxplugin_headers_moc})
+add_library(opencvluxplugin MODULE ${opencvluxplugin_sources})
 set_target_properties(opencvluxplugin PROPERTIES PREFIX "")
-target_link_libraries(opencvluxplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries} ${OpenCV_LIBS} ${Boost_LIBRARIES})
+target_link_libraries(opencvluxplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries} ${OpenCV_LIBS} ${Boost_LIBRARIES} ${QT_LIBRARIES})
 
 install(TARGETS opencvluxplugin LIBRARY DESTINATION lib${LIB_SUFFIX}/automotive-message-broker)
 
index 233cbfe..ba46785 100644 (file)
@@ -22,6 +22,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #include <iostream>
 #include <opencv2/imgproc/imgproc.hpp>
 
+#include <QFuture>
+#include <QFutureWatcher>
+#include <QtConcurrent/QtConcurrent>
+
 #ifdef OPENCL
 #include <opencv2/ocl/ocl.hpp>
 #endif
@@ -257,13 +261,17 @@ static int grabImage(void *data)
                *(shared->m_capture) >> m_image;
        }
 
-       /*if(shared->threaded)
+       if(shared->threaded)
        {
+               QFutureWatcher<uint> *watcher = new QFutureWatcher<uint>();
+               QObject::connect(watcher, &QFutureWatcher<uint>::finished, shared->parent, &OpenCvLuxPlugin::imgProcResult);
 
+               QFuture<uint> future = QtConcurrent::run( evalImage, m_image, shared);
+               watcher->setFuture(future);
        }
-       else*/
+       else
        {
-               int lux = evalImage(m_image,shared);
+               int lux = evalImage(m_image, shared);
                detectLight(m_image,shared);
                shared->parent->updateProperty(lux);
        }
@@ -359,9 +367,8 @@ void OpenCvLuxPlugin::updateProperty(uint lux)
 {
        VehicleProperty::ExteriorBrightnessType l(lux);
 
-       for(auto itr = replyQueue.begin(); itr != replyQueue.end(); itr++)
+       for(auto reply : replyQueue)
        {
-               AsyncPropertyReply* reply = *itr;
                reply->value = &l;
                reply->success = true;
                try{
@@ -384,7 +391,13 @@ void OpenCvLuxPlugin::updateProperty(uint lux)
 
 }
 
+void OpenCvLuxPlugin::imgProcResult()
+{
+       QFutureWatcher<uint> *watcher = dynamic_cast<QFutureWatcher<uint>* >(sender());
 
+       uint lux = watcher->result();
+       shared->parent->updateProperty(lux);
+}
 
 TrafficLight::Color detectLight(cv::Mat img, OpenCvLuxPlugin::Shared *shared)
 {
index c863970..81207cf 100644 (file)
@@ -25,11 +25,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #include <opencv/cv.h>
 #include <opencv2/highgui/highgui.hpp>
 
+#include <QObject>
+
 using namespace std;
 
-class OpenCvLuxPlugin: public AbstractSource
+class OpenCvLuxPlugin: public QObject, public AbstractSource
 {
-
+Q_OBJECT
 public:
 
        struct Shared
@@ -63,7 +65,8 @@ public:
        
        void updateProperty(uint lux);
 
-
+public slots:
+       void imgProcResult();
 
        
 private: /// methods: