Limit combinations of backends and targets in demos and benchmark (#145)
Browse files* limit backend and target combination in demos and benchmark
* simpler version checking
- demo.py +37 -30
- mp_palmdet.py +4 -6
demo.py
CHANGED
|
@@ -5,35 +5,40 @@ import cv2 as cv
|
|
| 5 |
|
| 6 |
from mp_palmdet import MPPalmDet
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
try:
|
| 21 |
-
backends += [cv.dnn.DNN_BACKEND_TIMVX]
|
| 22 |
-
targets += [cv.dnn.DNN_TARGET_NPU]
|
| 23 |
-
help_msg_backends += "; {:d}: TIMVX"
|
| 24 |
-
help_msg_targets += "; {:d}: NPU"
|
| 25 |
-
except:
|
| 26 |
-
print('This version of OpenCV does not support TIM-VX and NPU. Visit https://github.com/opencv/opencv/wiki/TIM-VX-Backend-For-Running-OpenCV-On-NPU for more information.')
|
| 27 |
|
| 28 |
parser = argparse.ArgumentParser(description='Hand Detector from MediaPipe')
|
| 29 |
-
parser.add_argument('--input', '-i', type=str,
|
| 30 |
-
|
| 31 |
-
parser.add_argument('--
|
| 32 |
-
|
| 33 |
-
parser.add_argument('--
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
args = parser.parse_args()
|
| 38 |
|
| 39 |
def visualize(image, results, print_results=False, fps=None):
|
|
@@ -71,12 +76,15 @@ def visualize(image, results, print_results=False, fps=None):
|
|
| 71 |
return output
|
| 72 |
|
| 73 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
| 74 |
# Instantiate MPPalmDet
|
| 75 |
model = MPPalmDet(modelPath=args.model,
|
| 76 |
nmsThreshold=args.nms_threshold,
|
| 77 |
scoreThreshold=args.score_threshold,
|
| 78 |
-
backendId=
|
| 79 |
-
targetId=
|
| 80 |
|
| 81 |
# If input is an image
|
| 82 |
if args.input is not None:
|
|
@@ -123,4 +131,3 @@ if __name__ == '__main__':
|
|
| 123 |
cv.imshow('MPPalmDet Demo', frame)
|
| 124 |
|
| 125 |
tm.reset()
|
| 126 |
-
|
|
|
|
| 5 |
|
| 6 |
from mp_palmdet import MPPalmDet
|
| 7 |
|
| 8 |
+
# Check OpenCV version
|
| 9 |
+
assert cv.__version__ >= "4.7.0", \
|
| 10 |
+
"Please install latest opencv-python to try this demo: python3 -m pip install --upgrade opencv-python"
|
| 11 |
+
|
| 12 |
+
# Valid combinations of backends and targets
|
| 13 |
+
backend_target_pairs = [
|
| 14 |
+
[cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU],
|
| 15 |
+
[cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA],
|
| 16 |
+
[cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16],
|
| 17 |
+
[cv.dnn.DNN_BACKEND_TIMVX, cv.dnn.DNN_TARGET_NPU],
|
| 18 |
+
[cv.dnn.DNN_BACKEND_CANN, cv.dnn.DNN_TARGET_NPU]
|
| 19 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
parser = argparse.ArgumentParser(description='Hand Detector from MediaPipe')
|
| 22 |
+
parser.add_argument('--input', '-i', type=str,
|
| 23 |
+
help='Usage: Set path to the input image. Omit for using default camera.')
|
| 24 |
+
parser.add_argument('--model', '-m', type=str, default='./palm_detection_mediapipe_2023feb.onnx',
|
| 25 |
+
help='Usage: Set model path, defaults to palm_detection_mediapipe_2023feb.onnx.')
|
| 26 |
+
parser.add_argument('--backend_target', '-bt', type=int, default=0,
|
| 27 |
+
help='''Choose one of the backend-target pair to run this demo:
|
| 28 |
+
{:d}: (default) OpenCV implementation + CPU,
|
| 29 |
+
{:d}: CUDA + GPU (CUDA),
|
| 30 |
+
{:d}: CUDA + GPU (CUDA FP16),
|
| 31 |
+
{:d}: TIM-VX + NPU,
|
| 32 |
+
{:d}: CANN + NPU
|
| 33 |
+
'''.format(*[x for x in range(len(backend_target_pairs))]))
|
| 34 |
+
parser.add_argument('--score_threshold', type=float, default=0.8,
|
| 35 |
+
help='Usage: Set the minimum needed confidence for the model to identify a palm, defaults to 0.8. Smaller values may result in faster detection, but will limit accuracy. Filter out faces of confidence < conf_threshold. An empirical score threshold for the quantized model is 0.49.')
|
| 36 |
+
parser.add_argument('--nms_threshold', type=float, default=0.3,
|
| 37 |
+
help='Usage: Suppress bounding boxes of iou >= nms_threshold. Default = 0.3.')
|
| 38 |
+
parser.add_argument('--save', '-s', action='store_true',
|
| 39 |
+
help='Usage: Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.')
|
| 40 |
+
parser.add_argument('--vis', '-v', action='store_true',
|
| 41 |
+
help='Usage: Specify to open a new window to show results. Invalid in case of camera input.')
|
| 42 |
args = parser.parse_args()
|
| 43 |
|
| 44 |
def visualize(image, results, print_results=False, fps=None):
|
|
|
|
| 76 |
return output
|
| 77 |
|
| 78 |
if __name__ == '__main__':
|
| 79 |
+
backend_id = backend_target_pairs[args.backend_target][0]
|
| 80 |
+
target_id = backend_target_pairs[args.backend_target][1]
|
| 81 |
+
|
| 82 |
# Instantiate MPPalmDet
|
| 83 |
model = MPPalmDet(modelPath=args.model,
|
| 84 |
nmsThreshold=args.nms_threshold,
|
| 85 |
scoreThreshold=args.score_threshold,
|
| 86 |
+
backendId=backend_id,
|
| 87 |
+
targetId=target_id)
|
| 88 |
|
| 89 |
# If input is an image
|
| 90 |
if args.input is not None:
|
|
|
|
| 131 |
cv.imshow('MPPalmDet Demo', frame)
|
| 132 |
|
| 133 |
tm.reset()
|
|
|
mp_palmdet.py
CHANGED
|
@@ -22,12 +22,10 @@ class MPPalmDet:
|
|
| 22 |
def name(self):
|
| 23 |
return self.__class__.__name__
|
| 24 |
|
| 25 |
-
def
|
| 26 |
self.backend_id = backendId
|
| 27 |
-
self.model.setPreferableBackend(self.backend_id)
|
| 28 |
-
|
| 29 |
-
def setTarget(self, targetId):
|
| 30 |
self.target_id = targetId
|
|
|
|
| 31 |
self.model.setPreferableTarget(self.target_id)
|
| 32 |
|
| 33 |
def _preprocess(self, image):
|
|
@@ -35,7 +33,7 @@ class MPPalmDet:
|
|
| 35 |
ratio = min(self.input_size / image.shape[:2])
|
| 36 |
if image.shape[0] != self.input_size[0] or image.shape[1] != self.input_size[1]:
|
| 37 |
# keep aspect ratio when resize
|
| 38 |
-
ratio_size = (np.array(image.shape[:2]) * ratio).astype(np.
|
| 39 |
image = cv.resize(image, (ratio_size[1], ratio_size[0]))
|
| 40 |
pad_h = self.input_size[0] - ratio_size[0]
|
| 41 |
pad_w = self.input_size[1] - ratio_size[1]
|
|
@@ -46,7 +44,7 @@ class MPPalmDet:
|
|
| 46 |
image = cv.copyMakeBorder(image, top, bottom, left, right, cv.BORDER_CONSTANT, None, (0, 0, 0))
|
| 47 |
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
|
| 48 |
image = image.astype(np.float32) / 255.0 # norm
|
| 49 |
-
pad_bias = (pad_bias / ratio).astype(np.
|
| 50 |
return image[np.newaxis, :, :, :], pad_bias # hwc -> nhwc
|
| 51 |
|
| 52 |
def infer(self, image):
|
|
|
|
| 22 |
def name(self):
|
| 23 |
return self.__class__.__name__
|
| 24 |
|
| 25 |
+
def setBackendAndTarget(self, backendId, targetId):
|
| 26 |
self.backend_id = backendId
|
|
|
|
|
|
|
|
|
|
| 27 |
self.target_id = targetId
|
| 28 |
+
self.model.setPreferableBackend(self.backend_id)
|
| 29 |
self.model.setPreferableTarget(self.target_id)
|
| 30 |
|
| 31 |
def _preprocess(self, image):
|
|
|
|
| 33 |
ratio = min(self.input_size / image.shape[:2])
|
| 34 |
if image.shape[0] != self.input_size[0] or image.shape[1] != self.input_size[1]:
|
| 35 |
# keep aspect ratio when resize
|
| 36 |
+
ratio_size = (np.array(image.shape[:2]) * ratio).astype(np.int32)
|
| 37 |
image = cv.resize(image, (ratio_size[1], ratio_size[0]))
|
| 38 |
pad_h = self.input_size[0] - ratio_size[0]
|
| 39 |
pad_w = self.input_size[1] - ratio_size[1]
|
|
|
|
| 44 |
image = cv.copyMakeBorder(image, top, bottom, left, right, cv.BORDER_CONSTANT, None, (0, 0, 0))
|
| 45 |
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
|
| 46 |
image = image.astype(np.float32) / 255.0 # norm
|
| 47 |
+
pad_bias = (pad_bias / ratio).astype(np.int32)
|
| 48 |
return image[np.newaxis, :, :, :], pad_bias # hwc -> nhwc
|
| 49 |
|
| 50 |
def infer(self, image):
|