From 5889ec6109adef15fe4aa42cce5dd47273bf5300 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Mon, 14 Sep 2015 20:36:29 -0400 Subject: [PATCH] Made OpenCL GPU detector threadsafe --- src/openalpr/detection/detectorocl.cpp | 54 +++++++++++++++++++------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/openalpr/detection/detectorocl.cpp b/src/openalpr/detection/detectorocl.cpp index 8ee295b..4c73595 100644 --- a/src/openalpr/detection/detectorocl.cpp +++ b/src/openalpr/detection/detectorocl.cpp @@ -18,23 +18,26 @@ */ #include "detectorocl.h" +#include #if OPENCV_MAJOR_VERSION == 3 using namespace cv; using namespace std; +tthread::mutex ocl_detector_mutex_m; + + namespace alpr { DetectorOCL::DetectorOCL(Config* config) : Detector(config) { + tthread::lock_guard guard(ocl_detector_mutex_m); + cv::ocl::setUseOpenCL(true); - - - if (!ocl::haveOpenCL()) { this->loaded = false; @@ -91,34 +94,55 @@ namespace alpr vector DetectorOCL::doCascade(Mat orig_frame, int offset_x, int offset_y) { + int w = orig_frame.size().width; int h = orig_frame.size().height; float scale_factor = computeScaleFactor(w, h); - UMat openclFrame; - orig_frame.copyTo(openclFrame); - vector plates; - equalizeHist( openclFrame, openclFrame ); - - if (scale_factor != 1.0) - resize(openclFrame, openclFrame, Size(w * scale_factor, h * scale_factor)); - //-- Detect plates timespec startTime; getTimeMonotonic(&startTime); float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * scale_factor; float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor; + Size minSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor); Size maxSize(maxWidth, maxHeight); - plate_cascade.detectMultiScale( openclFrame, plates, config->detection_iteration_increase, config->detectionStrictness, - 0, - //0|CV_HAAR_SCALE_IMAGE, - minSize, maxSize ); + // If we have an OpenCL core available, use it. Otherwise use CPU + if (ocl_detector_mutex_m.try_lock()) + { + UMat openclFrame; + orig_frame.copyTo(openclFrame); + + equalizeHist( openclFrame, openclFrame ); + + if (scale_factor != 1.0) + resize(openclFrame, openclFrame, Size(w * scale_factor, h * scale_factor)); + + plate_cascade.detectMultiScale( openclFrame, plates, config->detection_iteration_increase, config->detectionStrictness, + 0, + minSize, maxSize ); + + ocl_detector_mutex_m.unlock(); + } + else + { + equalizeHist( orig_frame, orig_frame ); + + if (scale_factor != 1.0) + resize(orig_frame, orig_frame, Size(w * scale_factor, h * scale_factor)); + + plate_cascade.detectMultiScale( orig_frame, plates, config->detection_iteration_increase, config->detectionStrictness, + 0, + minSize, maxSize ); + } + + + if (config->debugTiming)