Project import generated by Copybara.
GitOrigin-RevId: bbbbcb4f5174dea33525729ede47c770069157cd
This commit is contained in:
@@ -89,6 +89,7 @@ class Hands(SolutionBase):
|
||||
def __init__(self,
|
||||
static_image_mode=False,
|
||||
max_num_hands=2,
|
||||
model_complexity=1,
|
||||
min_detection_confidence=0.5,
|
||||
min_tracking_confidence=0.5):
|
||||
"""Initializes a MediaPipe Hand object.
|
||||
@@ -99,6 +100,10 @@ class Hands(SolutionBase):
|
||||
https://solutions.mediapipe.dev/hands#static_image_mode.
|
||||
max_num_hands: Maximum number of hands to detect. See details in
|
||||
https://solutions.mediapipe.dev/hands#max_num_hands.
|
||||
model_complexity: Complexity of the hand landmark model: 0 or 1.
|
||||
Landmark accuracy as well as inference latency generally go up with the
|
||||
model complexity. See details in
|
||||
https://solutions.mediapipe.dev/hands#model_complexity.
|
||||
min_detection_confidence: Minimum confidence value ([0.0, 1.0]) for hand
|
||||
detection to be considered successful. See details in
|
||||
https://solutions.mediapipe.dev/hands#min_detection_confidence.
|
||||
@@ -109,6 +114,7 @@ class Hands(SolutionBase):
|
||||
super().__init__(
|
||||
binary_graph_path=_BINARYPB_FILE_PATH,
|
||||
side_inputs={
|
||||
'model_complexity': model_complexity,
|
||||
'num_hands': max_num_hands,
|
||||
'use_prev_landmarks': not static_image_mode,
|
||||
},
|
||||
|
||||
@@ -32,7 +32,8 @@ from mediapipe.python.solutions import hands as mp_hands
|
||||
|
||||
|
||||
TEST_IMAGE_PATH = 'mediapipe/python/solutions/testdata'
|
||||
DIFF_THRESHOLD = 20 # pixels
|
||||
LITE_MODEL_DIFF_THRESHOLD = 25 # pixels
|
||||
FULL_MODEL_DIFF_THRESHOLD = 20 # pixels
|
||||
EXPECTED_HAND_COORDINATES_PREDICTION = [[[138, 343], [211, 330], [257, 286],
|
||||
[289, 237], [322, 203], [219, 216],
|
||||
[238, 138], [249, 90], [253, 51],
|
||||
@@ -40,7 +41,7 @@ EXPECTED_HAND_COORDINATES_PREDICTION = [[[138, 343], [211, 330], [257, 286],
|
||||
[185, 19], [138, 208], [131, 127],
|
||||
[124, 77], [117, 36], [106, 222],
|
||||
[92, 159], [79, 124], [68, 93]],
|
||||
[[580, 36], [504, 50], [459, 94],
|
||||
[[580, 34], [504, 50], [459, 94],
|
||||
[429, 146], [397, 182], [507, 167],
|
||||
[479, 245], [469, 292], [464, 330],
|
||||
[545, 180], [534, 265], [533, 319],
|
||||
@@ -75,14 +76,18 @@ class HandsTest(parameterized.TestCase):
|
||||
self.assertIsNone(results.multi_hand_landmarks)
|
||||
self.assertIsNone(results.multi_handedness)
|
||||
|
||||
@parameterized.named_parameters(('static_image_mode', True, 1),
|
||||
('video_mode', False, 5))
|
||||
def test_multi_hands(self, static_image_mode, num_frames):
|
||||
@parameterized.named_parameters(
|
||||
('static_image_mode_with_lite_model', True, 0, 5),
|
||||
('video_mode_with_lite_model', False, 0, 10),
|
||||
('static_image_mode_with_full_model', True, 1, 5),
|
||||
('video_mode_with_full_model', False, 1, 10))
|
||||
def test_multi_hands(self, static_image_mode, model_complexity, num_frames):
|
||||
image_path = os.path.join(os.path.dirname(__file__), 'testdata/hands.jpg')
|
||||
image = cv2.imread(image_path)
|
||||
with mp_hands.Hands(
|
||||
static_image_mode=static_image_mode,
|
||||
max_num_hands=2,
|
||||
model_complexity=model_complexity,
|
||||
min_detection_confidence=0.5) as hands:
|
||||
for idx in range(num_frames):
|
||||
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
||||
@@ -104,7 +109,8 @@ class HandsTest(parameterized.TestCase):
|
||||
prediction_error = np.abs(
|
||||
np.asarray(multi_hand_coordinates) -
|
||||
np.asarray(EXPECTED_HAND_COORDINATES_PREDICTION))
|
||||
npt.assert_array_less(prediction_error, DIFF_THRESHOLD)
|
||||
diff_threshold = LITE_MODEL_DIFF_THRESHOLD if model_complexity == 0 else FULL_MODEL_DIFF_THRESHOLD
|
||||
npt.assert_array_less(prediction_error, diff_threshold)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user