nareshmlx/opencv-pr huggingface dataset:
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 60

**Code Changes:**
```diff
+    /** @brief A simple interface to detect face from given image
+     *
+     *  @param image an image to detect
+     *  @param faces detection results stored in a cv::Mat
+     */
+    CV_WRAP virtual int detect(InputArray image, OutputArray faces) = 0;
+
+    /** @brief Creates an instance of this class with given parameters
+     *
+     *  @param model the path to the requested model
+     *  @param config the path to the config file for compability, which is not requested for ONNX models
```

Review comment:
Currently use 5-landmark definition as standard, but can be extended whenever needed.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
Done for 1. at https://github.com/opencv/opencv/pull/20422/commits/dbb9ea3d04d36a501223d1c3253cc20d015ca71b.
**File:** modules/objdetect/src/face_recognize.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,182 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#include "precomp.hpp"
+
+#include "opencv2/dnn.hpp"
+
+#include <algorithm>
```

Review comment:
Sorry for the late response. I deleted the '.clone()' because I think maintaining the extracted feature is up to the user. User may allow network to overwrite the last feature to reduce excess memory usage. And I'm sorry for another mistake, this function need not return any value. Maybe, I can delete 'return', and add '.clone()' into the sample code. ```cpp faceRecognizer->facefeature(aligned_face1, feature1); feature1 = feature1.clone(); faceRecognizer->facefeature(aligned_face2, feature2); feature2 = feature2.clone(); ```
**File:** modules/objdetect/src/face_recognize.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,182 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#include "precomp.hpp"
+
+#include "opencv2/dnn.hpp"
+
+#include <algorithm>
```

Review comment:
Done at https://github.com/fengyuentau/opencv/commit/8779ccb3c0ee9bc9c5cb522150bb8cbef6801d92.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
Done at https://github.com/fengyuentau/opencv/commit/fbf259934d3e68e91af611985e97bcf04a975f36.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
Done at https://github.com/fengyuentau/opencv/commit/46c6eb5c7dfbc388d6899af906eb276c6310e5f7.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 82

**Code Changes:**
```diff
+                                              float nms_threshold = 0.3f,
+                                              int top_k = 5000,
+                                              int backend_id = 0,
+                                              int target_id = 0);
+};
+
+/** @brief DNN-based face recognizer, model download link: https://drive.google.com/file/d/1ClK9WiB492c5OZFKveF3XiHCejoOxINW/view.
+ */
+class CV_EXPORTS_W FaceRecognizerSF
+{
+public:
```

Review comment:
Done at https://github.com/fengyuentau/opencv/commit/41fc659d2afdf7707611a137a14bb900292a7b44.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
What is about reusing/adopting of "High Level DNN API"?
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
Underscore prefixes are not allowed in public headers. Issue: #7288
**File:** modules/objdetect/src/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 174

**Code Changes:**
```diff
+                }
+            }
+        }
+    }
+
+    Mat postProcess(const std::vector<Mat>& output_blobs)
+    {
+        // Extract from output_blobs
+        Mat loc = output_blobs[0];
+        Mat conf = output_blobs[1];
+        Mat iou = output_blobs[2];
```

Review comment:
Please avoid using of generic `FaceDetector` name for such specialized implementation. Add some suffix, e.g. from article or model name -`FaceDetectorXYZ`.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
The model we used can detect faces with landmarks, so existing object detection APIs do not fit our need and we create a new one.
**File:** modules/objdetect/src/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 174

**Code Changes:**
```diff
+                }
+            }
+        }
+    }
+
+    Mat postProcess(const std::vector<Mat>& output_blobs)
+    {
+        // Extract from output_blobs
+        Mat loc = output_blobs[0];
+        Mat conf = output_blobs[1];
+        Mat iou = output_blobs[2];
```

Review comment:
Our original goal was to create a DNN-based face detection API that can use our model at least, and replace the traditional Haar-based one. Maybe we can make it more universal for different models.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
Fixed at https://github.com/opencv/opencv/pull/20422/commits/8e9278ec8c19ed45635897f2f31e33e82e25d432.
**File:** doc/tutorials/dnn/dnn_face/dnn_face.markdown
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,95 @@
+# DNN-based Face Detection And Recognition {#tutorial_dnn_face}
+
+@tableofcontents
+
+@prev_tutorial{tutorial_dnn_text_spotting}
+@next_tutorial{pytorch_cls_tutorial_dnn_conversion}
+
+| | |
+| -: | :- |
```

Review comment:
two-dimensional array of type `CV_32F`?
**File:** doc/tutorials/dnn/dnn_face/dnn_face.markdown
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,95 @@
+# DNN-based Face Detection And Recognition {#tutorial_dnn_face}
+
+@tableofcontents
+
+@prev_tutorial{tutorial_dnn_text_spotting}
+@next_tutorial{pytorch_cls_tutorial_dnn_conversion}
+
+| | |
+| -: | :- |
```

Review comment:
should the method be renamed to simply `match` instead of `faceMatch`?
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
typically, enumeration constants use upper case spelling, like COSINE, NORM_L2 etc. Also, distype should probably be renamed to DisType or something like that
**File:** modules/objdetect/src/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,288 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#include "precomp.hpp"
+
+#include "opencv2/imgproc.hpp"
+#include "opencv2/core.hpp"
+#include "opencv2/dnn.hpp"
```

Review comment:
local Mat allocation means memory allocation, more efficient would be to declare it before the loop. `push_back` in the end of the loop will still copy the elements, so it should work correctly anyway
**File:** samples/dnn/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,132 @@
+#include <opencv2/dnn.hpp>
+#include <opencv2/imgproc.hpp>
+#include <opencv2/highgui.hpp>
+#include <opencv2/objdetect.hpp>
+
+#include <iostream>
+
+using namespace cv;
+using namespace std;
```

Review comment:
could you make the `<image>` argument optional? If it's missing, you could run live detection from camera (but, please, do not save result.jpg in this case)
**File:** samples/dnn/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,132 @@
+#include <opencv2/dnn.hpp>
+#include <opencv2/imgproc.hpp>
+#include <opencv2/highgui.hpp>
+#include <opencv2/objdetect.hpp>
+
+#include <iostream>
+
+using namespace cv;
+using namespace std;
```

Review comment:
could you add the optional `-save` command line option to make the sample save the results? If the flag is not set, result.jpg should not be written
**File:** samples/dnn/face_detect.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,101 @@
+import argparse
+
+import numpy as np
+import cv2 as cv
+
+def str2bool(v):
+    if v.lower() in ['on', 'yes', 'true', 'y', 't']:
+        return True
+    elif v.lower() in ['off', 'no', 'false', 'n', 'f']:
```

Review comment:
please, make it the optional as well. Camera support would also be nice to have
**File:** modules/objdetect/src/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 174

**Code Changes:**
```diff
+                }
+            }
+        }
+    }
+
+    Mat postProcess(const std::vector<Mat>& output_blobs)
+    {
+        // Extract from output_blobs
+        Mat loc = output_blobs[0];
+        Mat conf = output_blobs[1];
+        Mat iou = output_blobs[2];
```

Review comment:
Okay. So is it the same for renaming `FaceRecognizer`?
**File:** modules/objdetect/src/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 174

**Code Changes:**
```diff
+                }
+            }
+        }
+    }
+
+    Mat postProcess(const std::vector<Mat>& output_blobs)
+    {
+        // Extract from output_blobs
+        Mat loc = output_blobs[0];
+        Mat conf = output_blobs[1];
+        Mat iou = output_blobs[2];
```

Review comment:
Done renaming for `FaceDetectorYN` at https://github.com/opencv/opencv/pull/20422/commits/541ed921c55b1325ece074a2b6725ce88968c0eb
**File:** doc/tutorials/dnn/dnn_face/dnn_face.markdown
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,95 @@
+# DNN-based Face Detection And Recognition {#tutorial_dnn_face}
+
+@tableofcontents
+
+@prev_tutorial{tutorial_dnn_text_spotting}
+@next_tutorial{pytorch_cls_tutorial_dnn_conversion}
+
+| | |
+| -: | :- |
```

Review comment:
Done. We also rename the `faceFeature` to `feature`.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
"DNN High Level API" is not about object detection at all. It is mostly about configuration of underlying DNN networks execution in some generic way. At least to configure execution backend/target to inference network on GPU.
**File:** modules/objdetect/src/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 174

**Code Changes:**
```diff
+                }
+            }
+        }
+    }
+
+    Mat postProcess(const std::vector<Mat>& output_blobs)
+    {
+        // Extract from output_blobs
+        Mat loc = output_blobs[0];
+        Mat conf = output_blobs[1];
+        Mat iou = output_blobs[2];
```

Review comment:
> FaceRecognizer Yes, there is the same note about `FaceRecognizer`.
**File:** modules/objdetect/src/face_detect.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 174

**Code Changes:**
```diff
+                }
+            }
+        }
+    }
+
+    Mat postProcess(const std::vector<Mat>& output_blobs)
+    {
+        // Extract from output_blobs
+        Mat loc = output_blobs[0];
+        Mat conf = output_blobs[1];
+        Mat iou = output_blobs[2];
```

Review comment:
Done. We rename FaceRecognizer to FaceRecognizerSF, where SF is the abbreviation of SFace.
**File:** modules/objdetect/include/opencv2/objdetect/face.hpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,125 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#ifndef OPENCV_OBJDETECT_FACE_HPP
+#define OPENCV_OBJDETECT_FACE_HPP
+
+#include <opencv2/core.hpp>
+
```

Review comment:
0.9f / 0.3f should be used with `float`.
**File:** samples/dnn/face_match.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,57 @@
+import argparse
+
+import numpy as np
+import cv2 as cv
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--input1', '-i1', type=str, help='Path to the input image1.')
+parser.add_argument('--input2', '-i2', type=str, help='Path to the input image2.')
+parser.add_argument('--face_detection_model', '-fd', type=str, help='Path to the face detection model. Download the model at https://github.com/ShiqiYu/libfacedetection.train/tree/master/tasks/task1/onnx.')
```

Review comment:
FaceRecognizerSF
**File:** samples/dnn/face_match.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,57 @@
+import argparse
+
+import numpy as np
+import cv2 as cv
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--input1', '-i1', type=str, help='Path to the input image1.')
+parser.add_argument('--input2', '-i2', type=str, help='Path to the input image2.')
+parser.add_argument('--face_detection_model', '-fd', type=str, help='Path to the face detection model. Download the model at https://github.com/ShiqiYu/libfacedetection.train/tree/master/tasks/task1/onnx.')
```

Review comment:
typo: Instantiaate
**File:** samples/dnn/face_match.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 3

**Code Changes:**
```diff
@@ -0,0 +1,57 @@
+import argparse
+
+import numpy as np
+import cv2 as cv
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--input1', '-i1', type=str, help='Path to the input image1.')
+parser.add_argument('--input2', '-i2', type=str, help='Path to the input image2.')
```

Review comment:
numpy import should go before cv2
**File:** samples/dnn/face_detect.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 45

**Code Changes:**
```diff
+
+    # Instantiate FaceDetectorYN
+    detector = cv.FaceDetectorYN.create(
+        args.model,
+        "",
+        (320, 320),
+        args.score_threshold,
+        args.nms_threshold,
+        args.top_k
+    )
+
```

Review comment:
> (320, 320) How is this aligned with .setInputSize()?
**File:** samples/dnn/face_detect.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 56

**Code Changes:**
```diff
+    # If input is an image
+    if args.input is not None:
+        image = cv.imread(args.input)
+
+        # Set input size before inference
+        detector.setInputSize((image.shape[1], image.shape[0]))
+
+        # Inference
+        faces = detector.detect(image)
+
+        # Draw results on the input image
```

Review comment:
Input of what? frame? network input to be preprocessed? Class methods have zero documentation.
**File:** modules/objdetect/src/face_recognize.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,182 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#include "precomp.hpp"
+
+#include "opencv2/dnn.hpp"
+
+#include <algorithm>
```

Review comment:
Probably already included by precomp.hp
**File:** samples/dnn/face_detect.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 56

**Code Changes:**
```diff
+    # If input is an image
+    if args.input is not None:
+        image = cv.imread(args.input)
+
+        # Set input size before inference
+        detector.setInputSize((image.shape[1], image.shape[0]))
+
+        # Inference
+        faces = detector.detect(image)
+
+        # Draw results on the input image
```

Review comment:
I added the documentation to this class method in the latest commit. This class method sets the size of network input, which overwrites the input size in `create`.
**File:** samples/dnn/face_detect.py
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect
**Review Line:** 45

**Code Changes:**
```diff
+
+    # Instantiate FaceDetectorYN
+    detector = cv.FaceDetectorYN.create(
+        args.model,
+        "",
+        (320, 320),
+        args.score_threshold,
+        args.nms_threshold,
+        args.top_k
+    )
+
```

Review comment:
This algorithm requires to generate priors/anchors for different input sizes. My intention is to allow users to set the input size for the network when creating instances in case the input size is fixed, such as taking video stream as input. However, users may also want to detect on images of different sizes with one instance, so `.setInputSize()` is added to do so without distorting images.
**File:** modules/objdetect/src/face_recognize.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,182 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#include "precomp.hpp"
+
+#include "opencv2/dnn.hpp"
+
+#include <algorithm>
```

Review comment:
Line 107 - 110 is useless. Need to be removed.
**File:** samples/dnn/face_match.cpp
**Change Type:** added
**Context:** PR #20422: Add DNN-based face detection and face recognition into modules/objdetect

**Code Changes:**
```diff
@@ -0,0 +1,103 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html.
+
+#include "opencv2/dnn.hpp"
+#include "opencv2/imgproc.hpp"
+#include "opencv2/highgui.hpp"
+
+#include <iostream>
```

Review comment:
Update these two thresholds to the ones in test_face.cpp.