Abhishek Gola
commited on
Commit
·
3d70824
1
Parent(s):
555ec94
updated the usage of yunet
Browse files
demo.py
CHANGED
|
@@ -9,6 +9,7 @@ import argparse
|
|
| 9 |
|
| 10 |
import numpy as np
|
| 11 |
import cv2 as cv
|
|
|
|
| 12 |
|
| 13 |
# Check OpenCV version
|
| 14 |
opencv_python_version = lambda str_version: tuple(map(int, (str_version.split("."))))
|
|
@@ -16,10 +17,10 @@ assert opencv_python_version(cv.__version__) >= opencv_python_version("4.10.0"),
|
|
| 16 |
"Please install latest opencv-python for benchmark: python3 -m pip install --upgrade opencv-python"
|
| 17 |
|
| 18 |
from sface import SFace
|
| 19 |
-
|
| 20 |
-
sys.path.append('../face_detection_yunet')
|
| 21 |
from yunet import YuNet
|
| 22 |
|
|
|
|
|
|
|
| 23 |
# Valid combinations of backends and targets
|
| 24 |
backend_target_pairs = [
|
| 25 |
[cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU],
|
|
@@ -114,7 +115,7 @@ if __name__ == '__main__':
|
|
| 114 |
backendId=backend_id,
|
| 115 |
targetId=target_id)
|
| 116 |
# Instantiate YuNet for face detection
|
| 117 |
-
detector = YuNet(modelPath=
|
| 118 |
inputSize=[320, 320],
|
| 119 |
confThreshold=0.9,
|
| 120 |
nmsThreshold=0.3,
|
|
|
|
| 9 |
|
| 10 |
import numpy as np
|
| 11 |
import cv2 as cv
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
|
| 14 |
# Check OpenCV version
|
| 15 |
opencv_python_version = lambda str_version: tuple(map(int, (str_version.split("."))))
|
|
|
|
| 17 |
"Please install latest opencv-python for benchmark: python3 -m pip install --upgrade opencv-python"
|
| 18 |
|
| 19 |
from sface import SFace
|
|
|
|
|
|
|
| 20 |
from yunet import YuNet
|
| 21 |
|
| 22 |
+
yunet_model_path = hf_hub_download(repo_id="opencv/face_detection_yunet", filename="face_detection_yunet_2023mar.onnx")
|
| 23 |
+
|
| 24 |
# Valid combinations of backends and targets
|
| 25 |
backend_target_pairs = [
|
| 26 |
[cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU],
|
|
|
|
| 115 |
backendId=backend_id,
|
| 116 |
targetId=target_id)
|
| 117 |
# Instantiate YuNet for face detection
|
| 118 |
+
detector = YuNet(modelPath=yunet_model_path,
|
| 119 |
inputSize=[320, 320],
|
| 120 |
confThreshold=0.9,
|
| 121 |
nmsThreshold=0.3,
|
yunet.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file is part of OpenCV Zoo project.
|
| 2 |
+
# It is subject to the license terms in the LICENSE file found in the same directory.
|
| 3 |
+
#
|
| 4 |
+
# Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
|
| 5 |
+
# Third party copyrights are property of their respective owners.
|
| 6 |
+
|
| 7 |
+
from itertools import product
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
import cv2 as cv
|
| 11 |
+
|
| 12 |
+
class YuNet:
|
| 13 |
+
def __init__(self, modelPath, inputSize=[320, 320], confThreshold=0.6, nmsThreshold=0.3, topK=5000, backendId=0, targetId=0):
|
| 14 |
+
self._modelPath = modelPath
|
| 15 |
+
self._inputSize = tuple(inputSize) # [w, h]
|
| 16 |
+
self._confThreshold = confThreshold
|
| 17 |
+
self._nmsThreshold = nmsThreshold
|
| 18 |
+
self._topK = topK
|
| 19 |
+
self._backendId = backendId
|
| 20 |
+
self._targetId = targetId
|
| 21 |
+
|
| 22 |
+
self._model = cv.FaceDetectorYN.create(
|
| 23 |
+
model=self._modelPath,
|
| 24 |
+
config="",
|
| 25 |
+
input_size=self._inputSize,
|
| 26 |
+
score_threshold=self._confThreshold,
|
| 27 |
+
nms_threshold=self._nmsThreshold,
|
| 28 |
+
top_k=self._topK,
|
| 29 |
+
backend_id=self._backendId,
|
| 30 |
+
target_id=self._targetId)
|
| 31 |
+
|
| 32 |
+
@property
|
| 33 |
+
def name(self):
|
| 34 |
+
return self.__class__.__name__
|
| 35 |
+
|
| 36 |
+
def setBackendAndTarget(self, backendId, targetId):
|
| 37 |
+
self._backendId = backendId
|
| 38 |
+
self._targetId = targetId
|
| 39 |
+
self._model = cv.FaceDetectorYN.create(
|
| 40 |
+
model=self._modelPath,
|
| 41 |
+
config="",
|
| 42 |
+
input_size=self._inputSize,
|
| 43 |
+
score_threshold=self._confThreshold,
|
| 44 |
+
nms_threshold=self._nmsThreshold,
|
| 45 |
+
top_k=self._topK,
|
| 46 |
+
backend_id=self._backendId,
|
| 47 |
+
target_id=self._targetId)
|
| 48 |
+
|
| 49 |
+
def setInputSize(self, input_size):
|
| 50 |
+
self._model.setInputSize(tuple(input_size))
|
| 51 |
+
|
| 52 |
+
def infer(self, image):
|
| 53 |
+
# Forward
|
| 54 |
+
faces = self._model.detect(image)
|
| 55 |
+
return np.empty(shape=(0, 5)) if faces[1] is None else faces[1]
|