Resize an input to half size to speed up
authorTae-Young Chung <ty83.chung@samsung.com>
Tue, 7 May 2024 08:23:29 +0000 (17:23 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Tue, 7 May 2024 08:23:29 +0000 (17:23 +0900)
Change-Id: Ib79b403534c8299960f910b8b3948f83dd478087
Signed-off-by: Tae-Young Chung <ty83.chung@samsung.com>
dump_headpose.jpg [deleted file]
dump_headpose_mvFace.jpg [deleted file]
inference/backends/private/include/PrivateInferenceFaceService.h
inference/backends/private/src/PrivateInferenceFaceService.cpp

diff --git a/dump_headpose.jpg b/dump_headpose.jpg
deleted file mode 100644 (file)
index 4e8d6d5..0000000
Binary files a/dump_headpose.jpg and /dev/null differ
diff --git a/dump_headpose_mvFace.jpg b/dump_headpose_mvFace.jpg
deleted file mode 100644 (file)
index 229b033..0000000
Binary files a/dump_headpose_mvFace.jpg and /dev/null differ
index bc700aa858abb3f617246aed432dbd0331660fcd..05be030186cb75a7f0b42406a961833d823c0f5e 100644 (file)
@@ -40,6 +40,11 @@ private:
        bool isDetectTask;
        bool isLandmarkDetectTask;
 
+       float _xDownSizeRatio;
+       float _yDownSizeRatio;
+       float _xInvRatio;
+       float _yInvRatio;
+
        void getLandmarks(cv::Mat &data, Points &landmarks, int roi_left = 0, int roi_right = 0);
 public:
        PrivateInferenceFaceService(std::vector<inference::TaskType> &tasks);
index 3f5bdaa7681339699f399750860ecaec9dae60e2..22e8cbbd67726f984c06be61a145f6962abee6b5 100644 (file)
@@ -40,6 +40,11 @@ PrivateInferenceFaceService::PrivateInferenceFaceService(std::vector<inference::
             isDetectTask = true;
             _pFace_detection_result = nullptr;
             _pBuffer = new unsigned char[DETECT_BUFFER_SIZE];
+
+            _xDownSizeRatio = 0.5f;
+            _yDownSizeRatio = 0.5f;
+            _xInvRatio = 1.0f / _xDownSizeRatio;
+            _yInvRatio = 1.0f / _yDownSizeRatio;
         }
             break;
         case TaskType::FACE_LANDMARK_DETECTION:
@@ -119,6 +124,8 @@ void PrivateInferenceFaceService::getLandmarks(cv::Mat &data, Points &landmarks,
                if (ret != MEDIA_VISION_ERROR_NONE)
                        throw runtime_error("Fail to get face landmark detection bound box.");
 
+        landmark.x *= _yInvRatio;
+        landmark.y *= _yInvRatio;
                landmark.x += roi_left;
                landmark.y += roi_right;
                landmarks.push_back(landmark);
@@ -135,35 +142,45 @@ void PrivateInferenceFaceService::invoke(BaseDataType &input)
        }
 
     ImageDataType &data = dynamic_cast<ImageDataType &>(input);
-    cv::Mat cvData(cv::Size(data.width, data.height), CV_MAKE_TYPE(CV_8U, data.byte_per_pixel), data.ptr);
+    // cv::Mat cvData(cv::Size(data.width, data.height), CV_MAKE_TYPE(CV_8U, data.byte_per_pixel), data.ptr);
+    cv::Mat cvData_(cv::Size(data.width, data.height), CV_MAKE_TYPE(CV_8U, data.byte_per_pixel), data.ptr);
 
+    // resize cvData then do inference then back to results.
+    cv::Mat cvData;
+    cv::resize(cvData_, cvData, cv::Size(static_cast<int>(cvData_.cols * _xDownSizeRatio), static_cast<int>(cvData_.rows * _yDownSizeRatio)));
     _result._rects.clear();
     _result._landmarks.clear();
     if (isDetectTask) {
         partial.reset();
-        _pFace_detection_result = facedetect_cnn(_pBuffer, data.ptr, data.width, data.height, (int)(data.width*data.byte_per_pixel));
+        // _pFace_detection_result = facedetect_cnn(_pBuffer, data.ptr, data.width, data.height, (int)(data.width*data.byte_per_pixel));
+        _pFace_detection_result = facedetect_cnn(_pBuffer, cvData.data, cvData.cols, cvData.rows, (int)(cvData.cols * cvData.channels()));
         SINGLEO_LOGD("PrivateFaceService (Detection) takes %d ms", partial.check_ms());
 
         for(int i = 0; i < (_pFace_detection_result ? *_pFace_detection_result : 0); i++) {
-            Rect rect;
+
             short * p = ((short*)(_pFace_detection_result + 1)) + 16*i;
             // int confidence = p[0];
-            rect.left = static_cast<int>(p[1]);
-            rect.top = static_cast<int>(p[2]);
-            rect.right = rect.left + static_cast<int>(p[3]);
-            rect.bottom = rect.top + static_cast<int>(p[4]);
-
-            _result._rects.push_back(rect);
+            int x = static_cast<int>(p[1]);
+            int y = static_cast<int>(p[2]);
+            int w = static_cast<int>(p[3]);
+            int h = static_cast<int>(p[4]);
 
             if (isLandmarkDetectTask) {
-                cv::Mat roiCvData = cvData(cv::Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top)).clone();
+                cv::Mat roiCvData = cvData(cv::Rect(x, y, w, h)).clone();
 
                 Points landmarks;
                 partial.reset();
-                getLandmarks(roiCvData, landmarks, rect.left, rect.top);
+                getLandmarks(roiCvData, landmarks, static_cast<int>(x * _xInvRatio), static_cast<int>(y * _yInvRatio));
                 SINGLEO_LOGD("PrivateFaceService (Landmark) takes %d ms", partial.check_ms());
                 _result._landmarks.push_back(landmarks);
             }
+
+            Rect rect = { .left = static_cast<int>(x * _xInvRatio),
+                          .right = static_cast<int>( (x + w) * _xInvRatio),
+                          .top = static_cast<int>( y * _yInvRatio),
+                          .bottom = static_cast<int>( (y + h) * _yInvRatio) };
+
+            _result._rects.push_back(rect);
         }
     } else if (isLandmarkDetectTask) {
         Points landmarks;
@@ -174,13 +191,14 @@ void PrivateInferenceFaceService::invoke(BaseDataType &input)
     SINGLEO_LOGD("PrivateFaceService takes %d ms", timer.check_ms());
     int id = 0;
     for (auto &rect : _result._rects) {
-        cv::rectangle(cvData, cv::Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top), cv::Scalar(0, 255, 0), 2);
+        cv::rectangle(cvData_, cv::Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top), cv::Scalar(0, 255, 0), 2);
         auto &points = _result._landmarks[id];
         for (auto &point : points)
-            cv::circle(cvData, cv::Point(point.x, point.y), 2, cv::Scalar(255, 255, 0), 2);
+            cv::circle(cvData_, cv::Point(point.x, point.y), 2, cv::Scalar(255, 255, 0), 2);
         id++;
     }
-    cv::cvtColor(cvData, cvData, cv::COLOR_RGB2BGR);
+    cv::cvtColor(cvData_, cvData_, cv::COLOR_RGB2BGR);
+    cv::imwrite("dump_privateFace.jpg", cvData_);
 }
 
 FaceResult& PrivateInferenceFaceService::result()