From 2ea5184c51918bbf4617d682423dcd0e78afa457 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Thu, 10 Nov 2022 16:02:04 -0800 Subject: [PATCH] Internal change PiperOrigin-RevId: 487663454 --- .../hand_landmarker/hand_landmarker_graph.cc | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/mediapipe/tasks/cc/vision/hand_landmarker/hand_landmarker_graph.cc b/mediapipe/tasks/cc/vision/hand_landmarker/hand_landmarker_graph.cc index e610a412..2c4133eb 100644 --- a/mediapipe/tasks/cc/vision/hand_landmarker/hand_landmarker_graph.cc +++ b/mediapipe/tasks/cc/vision/hand_landmarker/hand_landmarker_graph.cc @@ -276,33 +276,44 @@ class HandLandmarkerGraph : public core::ModelTaskGraph { .set_min_size(max_num_hands); auto has_enough_hands = min_size_node.Out("").Cast(); - auto image_for_hand_detector = - DisallowIf(image_in, has_enough_hands, graph); - auto norm_rect_in_for_hand_detector = - DisallowIf(norm_rect_in, has_enough_hands, graph); - auto& hand_detector = graph.AddNode("mediapipe.tasks.vision.hand_detector.HandDetectorGraph"); hand_detector.GetOptions().CopyFrom( tasks_options.hand_detector_graph_options()); - image_for_hand_detector >> hand_detector.In("IMAGE"); - norm_rect_in_for_hand_detector >> hand_detector.In("NORM_RECT"); - auto hand_rects_from_hand_detector = hand_detector.Out("HAND_RECTS"); - - auto& hand_association = graph.AddNode("HandAssociationCalculator"); - hand_association.GetOptions() - .set_min_similarity_threshold(tasks_options.min_tracking_confidence()); - prev_hand_rects_from_landmarks >> - hand_association[Input>::Multiple("")][0]; - hand_rects_from_hand_detector >> - hand_association[Input>::Multiple("")][1]; - auto hand_rects = hand_association.Out(""); - auto& clip_hand_rects = graph.AddNode("ClipNormalizedRectVectorSizeCalculator"); clip_hand_rects.GetOptions() .set_max_vec_size(max_num_hands); - hand_rects >> clip_hand_rects.In(""); + + if (tasks_options.base_options().use_stream_mode()) { + // While in stream mode, skip hand detector graph when we successfully + // track the hands from the last frame. + auto image_for_hand_detector = + DisallowIf(image_in, has_enough_hands, graph); + auto norm_rect_in_for_hand_detector = + DisallowIf(norm_rect_in, has_enough_hands, graph); + image_for_hand_detector >> hand_detector.In("IMAGE"); + norm_rect_in_for_hand_detector >> hand_detector.In("NORM_RECT"); + auto hand_rects_from_hand_detector = hand_detector.Out("HAND_RECTS"); + auto& hand_association = graph.AddNode("HandAssociationCalculator"); + hand_association.GetOptions() + .set_min_similarity_threshold( + tasks_options.min_tracking_confidence()); + prev_hand_rects_from_landmarks >> + hand_association[Input>::Multiple("")][0]; + hand_rects_from_hand_detector >> + hand_association[Input>::Multiple("")][1]; + auto hand_rects = hand_association.Out(""); + hand_rects >> clip_hand_rects.In(""); + } else { + // While not in stream mode, the input images are not guaranteed to be in + // series, and we don't want to enable the tracking and hand associations + // between input images. Always use the hand detector graph. + image_in >> hand_detector.In("IMAGE"); + norm_rect_in >> hand_detector.In("NORM_RECT"); + auto hand_rects_from_hand_detector = hand_detector.Out("HAND_RECTS"); + hand_rects_from_hand_detector >> clip_hand_rects.In(""); + } auto clipped_hand_rects = clip_hand_rects.Out(""); auto& hand_landmarks_detector_graph = graph.AddNode(