From 300aba7cb523c1f61ce68eebc8ba46dfe76295e6 Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Fri, 31 Jan 2014 15:15:33 -0800 Subject: [PATCH] reimplemented threading in opencv plugin --- plugins/murphyplugin/murphysource.cpp | 13 +++++++------ plugins/opencvlux/CMakeLists.txt | 23 ++++++++++++++++++++--- plugins/opencvlux/opencvluxplugin.cpp | 23 ++++++++++++++++++----- plugins/opencvlux/opencvluxplugin.h | 9 ++++++--- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/plugins/murphyplugin/murphysource.cpp b/plugins/murphyplugin/murphysource.cpp index e2ad228..624200c 100644 --- a/plugins/murphyplugin/murphysource.cpp +++ b/plugins/murphyplugin/murphysource.cpp @@ -28,11 +28,12 @@ #include -#ifdef USE_QT_CORE +/*#ifdef USE_QT_CORE #include -#else +#else*/ + #include -#endif +//#endif // #include // #include @@ -281,14 +282,14 @@ MurphySource::MurphySource(AbstractRoutingEngine *re, map 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); } diff --git a/plugins/opencvlux/CMakeLists.txt b/plugins/opencvlux/CMakeLists.txt index b1b4f92..251bc75 100644 --- a/plugins/opencvlux/CMakeLists.txt +++ b/plugins/opencvlux/CMakeLists.txt @@ -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) diff --git a/plugins/opencvlux/opencvluxplugin.cpp b/plugins/opencvlux/opencvluxplugin.cpp index 233cbfe..ba46785 100644 --- a/plugins/opencvlux/opencvluxplugin.cpp +++ b/plugins/opencvlux/opencvluxplugin.cpp @@ -22,6 +22,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include #include +#include +#include +#include + #ifdef OPENCL #include #endif @@ -257,13 +261,17 @@ static int grabImage(void *data) *(shared->m_capture) >> m_image; } - /*if(shared->threaded) + if(shared->threaded) { + QFutureWatcher *watcher = new QFutureWatcher(); + QObject::connect(watcher, &QFutureWatcher::finished, shared->parent, &OpenCvLuxPlugin::imgProcResult); + QFuture 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 *watcher = dynamic_cast* >(sender()); + uint lux = watcher->result(); + shared->parent->updateProperty(lux); +} TrafficLight::Color detectLight(cv::Mat img, OpenCvLuxPlugin::Shared *shared) { diff --git a/plugins/opencvlux/opencvluxplugin.h b/plugins/opencvlux/opencvluxplugin.h index c863970..81207cf 100644 --- a/plugins/opencvlux/opencvluxplugin.h +++ b/plugins/opencvlux/opencvluxplugin.h @@ -25,11 +25,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include #include +#include + 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: -- 2.7.4