ytfeng commited on
Commit
a4924f7
·
1 Parent(s): 576ce22

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

Files changed (2) hide show
  1. demo.py +36 -30
  2. mp_handpose.py +3 -6
demo.py CHANGED
@@ -9,34 +9,38 @@ from mp_handpose import MPHandPose
9
  sys.path.append('../palm_detection_mediapipe')
10
  from mp_palmdet import MPPalmDet
11
 
12
- def str2bool(v):
13
- if v.lower() in ['on', 'yes', 'true', 'y', 't']:
14
- return True
15
- elif v.lower() in ['off', 'no', 'false', 'n', 'f']:
16
- return False
17
- else:
18
- raise NotImplementedError
19
-
20
- backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
21
- targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
22
- help_msg_backends = "Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
23
- help_msg_targets = "Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
24
- try:
25
- backends += [cv.dnn.DNN_BACKEND_TIMVX]
26
- targets += [cv.dnn.DNN_TARGET_NPU]
27
- help_msg_backends += "; {:d}: TIMVX"
28
- help_msg_targets += "; {:d}: NPU"
29
- except:
30
- 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.')
31
 
32
  parser = argparse.ArgumentParser(description='Hand Pose Estimation from MediaPipe')
33
- parser.add_argument('--input', '-i', type=str, help='Path to the input image. Omit for using default camera.')
34
- parser.add_argument('--model', '-m', type=str, default='./handpose_estimation_mediapipe_2023feb.onnx', help='Path to the model.')
35
- parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
36
- parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
37
- parser.add_argument('--conf_threshold', type=float, default=0.9, help='Filter out hands of confidence < conf_threshold.')
38
- parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.')
39
- parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.')
 
 
 
 
 
 
 
 
 
 
 
40
  args = parser.parse_args()
41
 
42
 
@@ -147,17 +151,19 @@ def visualize(image, hands, print_result=False):
147
 
148
 
149
  if __name__ == '__main__':
 
 
150
  # palm detector
151
  palm_detector = MPPalmDet(modelPath='../palm_detection_mediapipe/palm_detection_mediapipe_2023feb.onnx',
152
  nmsThreshold=0.3,
153
  scoreThreshold=0.6,
154
- backendId=args.backend,
155
- targetId=args.target)
156
  # handpose detector
157
  handpose_detector = MPHandPose(modelPath=args.model,
158
  confThreshold=args.conf_threshold,
159
- backendId=args.backend,
160
- targetId=args.target)
161
 
162
  # If input is an image
163
  if args.input is not None:
 
9
  sys.path.append('../palm_detection_mediapipe')
10
  from mp_palmdet import MPPalmDet
11
 
12
+ # Check OpenCV version
13
+ assert cv.__version__ >= "4.7.0", \
14
+ "Please install latest opencv-python to try this demo: python3 -m pip install --upgrade opencv-python"
15
+
16
+ # Valid combinations of backends and targets
17
+ backend_target_pairs = [
18
+ [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU],
19
+ [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA],
20
+ [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16],
21
+ [cv.dnn.DNN_BACKEND_TIMVX, cv.dnn.DNN_TARGET_NPU],
22
+ [cv.dnn.DNN_BACKEND_CANN, cv.dnn.DNN_TARGET_NPU]
23
+ ]
 
 
 
 
 
 
 
24
 
25
  parser = argparse.ArgumentParser(description='Hand Pose Estimation from MediaPipe')
26
+ parser.add_argument('--input', '-i', type=str,
27
+ help='Path to the input image. Omit for using default camera.')
28
+ parser.add_argument('--model', '-m', type=str, default='./handpose_estimation_mediapipe_2023feb.onnx',
29
+ help='Path to the model.')
30
+ parser.add_argument('--backend_target', '-bt', type=int, default=0,
31
+ help='''Choose one of the backend-target pair to run this demo:
32
+ {:d}: (default) OpenCV implementation + CPU,
33
+ {:d}: CUDA + GPU (CUDA),
34
+ {:d}: CUDA + GPU (CUDA FP16),
35
+ {:d}: TIM-VX + NPU,
36
+ {:d}: CANN + NPU
37
+ '''.format(*[x for x in range(len(backend_target_pairs))]))
38
+ parser.add_argument('--conf_threshold', type=float, default=0.9,
39
+ help='Filter out hands of confidence < conf_threshold.')
40
+ parser.add_argument('--save', '-s', action='store_true',
41
+ help='Specify to save results. This flag is invalid when using camera.')
42
+ parser.add_argument('--vis', '-v', action='store_true',
43
+ help='Specify to open a window for result visualization. This flag is invalid when using camera.')
44
  args = parser.parse_args()
45
 
46
 
 
151
 
152
 
153
  if __name__ == '__main__':
154
+ backend_id = backend_target_pairs[args.backend_target][0]
155
+ target_id = backend_target_pairs[args.backend_target][1]
156
  # palm detector
157
  palm_detector = MPPalmDet(modelPath='../palm_detection_mediapipe/palm_detection_mediapipe_2023feb.onnx',
158
  nmsThreshold=0.3,
159
  scoreThreshold=0.6,
160
+ backendId=backend_id,
161
+ targetId=target_id)
162
  # handpose detector
163
  handpose_detector = MPHandPose(modelPath=args.model,
164
  confThreshold=args.conf_threshold,
165
+ backendId=backend_id,
166
+ targetId=target_id)
167
 
168
  # If input is an image
169
  if args.input is not None:
mp_handpose.py CHANGED
@@ -1,7 +1,6 @@
1
  import numpy as np
2
  import cv2 as cv
3
 
4
-
5
  class MPHandPose:
6
  def __init__(self, modelPath, confThreshold=0.8, backendId=0, targetId=0):
7
  self.model_path = modelPath
@@ -28,12 +27,10 @@ class MPHandPose:
28
  def name(self):
29
  return self.__class__.__name__
30
 
31
- def setBackend(self, backendId):
32
- self.backend_id = backendId
 
33
  self.model.setPreferableBackend(self.backend_id)
34
-
35
- def setTarget(self, targetId):
36
- self.target_id = targetId
37
  self.model.setPreferableTarget(self.target_id)
38
 
39
  def _cropAndPadFromPalm(self, image, palm_bbox, for_rotation = False):
 
1
  import numpy as np
2
  import cv2 as cv
3
 
 
4
  class MPHandPose:
5
  def __init__(self, modelPath, confThreshold=0.8, backendId=0, targetId=0):
6
  self.model_path = modelPath
 
27
  def name(self):
28
  return self.__class__.__name__
29
 
30
+ def setBackendAndTarget(self, backendId, targetId):
31
+ self._backendId = backendId
32
+ self._targetId = targetId
33
  self.model.setPreferableBackend(self.backend_id)
 
 
 
34
  self.model.setPreferableTarget(self.target_id)
35
 
36
  def _cropAndPadFromPalm(self, image, palm_bbox, for_rotation = False):