Refact PrivateFaceService returns only one face and its landmarks
authorTae-Young Chung <ty83.chung@samsung.com>
Thu, 16 May 2024 08:05:48 +0000 (17:05 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Thu, 16 May 2024 08:05:51 +0000 (17:05 +0900)
A face region with a confidence under 30 is ignored.

Change-Id: Icc902a3ea8caf711e286fcc14ba2eb8804a2591b
Signed-off-by: Tae-Young Chung <ty83.chung@samsung.com>
inference/backends/private/src/PrivateInferenceFaceService.cpp

index 094b23aad268d485c92527dc45d78ae7535b3694..14cf8a852204ddf76c5bfef5320577689307d472 100644 (file)
@@ -201,53 +201,59 @@ void PrivateInferenceFaceService::invoke(BaseDataType &input)
         _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());
 
+        int maxConfidence = 0;
+        int maxX, maxY, maxW, maxH;
+        bool isFaceDetected = false;
         for(int i = 0; i < (_pFace_detection_result ? *_pFace_detection_result : 0); i++) {
 
             short * p = ((short*)(_pFace_detection_result + 1)) + 16*i;
             int confidence = p[0];
 
-            if (confidence < 90)
+            if (confidence < 30 || maxConfidence >= confidence)
                 continue;
 
-            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(x, y, w, h)).clone();
-
-                Points landmarks;
-                partial.reset();
-                getLandmarks(roiCvData, landmarks, static_cast<int>(x * _xInvRatio), static_cast<int>(y * _yInvRatio));
-                SINGLEO_LOGD("PrivateFaceService (Landmark) takes %d ms", partial.check_ms());
-                if (!_filterLandmarks->isInit()) {
-                    SINGLEO_LOGD("%zd landmarks are detected", landmarks.size());
-                    _filterLandmarks->init(landmarks);
-                    _isSkipLandmarkDetectTask = true;
-                } else {
-                    landmarks = _filterLandmarks->update(landmarks);
-                    _isSkipLandmarkDetectTask = true;
-                }
-
-                _result._landmarks.push_back(landmarks);
-            }
-
-            vector<Rect> rects = { { .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) }};
+            maxConfidence = confidence;
+            maxX = static_cast<int>(p[1]);
+            maxY = static_cast<int>(p[2]);
+            maxW = static_cast<int>(p[3]);
+            maxH = static_cast<int>(p[4]);
+            isFaceDetected = true;
+        }
+        if (!isFaceDetected)
+            return;
 
-            if (!_filterFaceRegion->isInit()) {
-                _filterFaceRegion->init(rects);
-                _isSkipDetectTask = true;
+        if (isLandmarkDetectTask) {
+            cv::Mat roiCvData = cvData(cv::Rect(maxX, maxY, maxW, maxH)).clone();
+
+            Points landmarks;
+            partial.reset();
+            getLandmarks(roiCvData, landmarks, static_cast<int>(maxX * _xInvRatio), static_cast<int>(maxY * _yInvRatio));
+            SINGLEO_LOGD("PrivateFaceService (Landmark) takes %d ms", partial.check_ms());
+            if (!_filterLandmarks->isInit()) {
+                SINGLEO_LOGD("%zd landmarks are detected", landmarks.size());
+                _filterLandmarks->init(landmarks);
+                _isSkipLandmarkDetectTask = true;
             } else {
-                rects = _filterFaceRegion->update(rects);
-                _isSkipDetectTask = true;
+                landmarks = _filterLandmarks->update(landmarks);
+                _isSkipLandmarkDetectTask = true;
             }
-            _result._rects = rects;
-            break;
+
+            _result._landmarks.push_back(landmarks);
+        }
+
+        vector<Rect> rects = { { .left = static_cast<int>( maxX * _xInvRatio ),
+                        .right = static_cast<int>( (maxX + maxW) * _xInvRatio ),
+                        .top = static_cast<int>( maxY * _yInvRatio ),
+                        .bottom = static_cast<int>( (maxY + maxH) * _yInvRatio) }};
+
+        if (!_filterFaceRegion->isInit()) {
+            _filterFaceRegion->init(rects);
+            _isSkipDetectTask = true;
+        } else {
+            rects = _filterFaceRegion->update(rects);
+            _isSkipDetectTask = true;
         }
+        _result._rects = rects;
     } else if (isLandmarkDetectTask) {
         Points landmarks;
         getLandmarks(cvData, landmarks);