From 3527c4d4231d74bb6b2cba9154ebafd017a345c4 Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Sun, 2 Nov 2025 12:31:01 -0800 Subject: [PATCH] scripts/examples: Simplify blazeface and palm examples. --- .../00-TensorFlow/blazeface_detector.py | 24 ++++----- .../00-TensorFlow/blazepalm_detection.py | 32 ++++++------ .../hand_landmarks_multi_hand.py | 49 +++++++++---------- .../hand_landmarks_single_hand.py | 13 ++--- 4 files changed, 52 insertions(+), 66 deletions(-) diff --git a/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_detector.py b/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_detector.py index 6efe47b9c..b6ebd7426 100644 --- a/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_detector.py +++ b/scripts/examples/03-Machine-Learning/00-TensorFlow/blazeface_detector.py @@ -26,20 +26,16 @@ while True: img = csi0.snapshot() # faces is a list of ((x, y, w, h), score, keypoints) tuples - faces = model.predict([img]) + for r, score, keypoints in model.predict([img]): + ml.utils.draw_predictions(img, [r], ("face",), ((0, 0, 255),), format=None) - # Draw bounding boxes around the detected faces and keypoints. - if faces: - for r, score, keypoints in faces[0]: - ml.utils.draw_predictions(img, [r], ("face",), ((0, 0, 255),), format=None) - - # keypoints is a ndarray of shape (6, 2) - # 0 - right eye (x, y) - # 1 - left eye (x, y) - # 2 - nose (x, y) - # 3 - mouth (x, y) - # 4 - right ear (x, y) - # 5 - left ear (x, y) - ml.utils.draw_keypoints(img, keypoints, color=(255, 0, 0)) + # keypoints is a ndarray of shape (6, 2) + # 0 - right eye (x, y) + # 1 - left eye (x, y) + # 2 - nose (x, y) + # 3 - mouth (x, y) + # 4 - right ear (x, y) + # 5 - left ear (x, y) + ml.utils.draw_keypoints(img, keypoints, color=(255, 0, 0)) print(clock.fps(), "fps") diff --git a/scripts/examples/03-Machine-Learning/00-TensorFlow/blazepalm_detection.py b/scripts/examples/03-Machine-Learning/00-TensorFlow/blazepalm_detection.py index d1dabd998..6bea64b7d 100644 --- a/scripts/examples/03-Machine-Learning/00-TensorFlow/blazepalm_detection.py +++ b/scripts/examples/03-Machine-Learning/00-TensorFlow/blazepalm_detection.py @@ -31,24 +31,20 @@ while True: img = csi0.snapshot() # palms is a list of ((x, y, w, h), score, keypoints) tuples - palms = model.predict([img]) + for r, score, keypoints in model.predict([img]): + ml.utils.draw_predictions(img, [r], ("palm",), ((0, 0, 255),), format=None) - # Draw bounding boxes around the detected palms and keypoints. - if palms: - for r, score, keypoints in palms[0]: - ml.utils.draw_predictions(img, [r], ("palm",), ((0, 0, 255),), format=None) - - # keypoints is a ndarray of shape (7, 2) - # 0 - wrist (x, y) - # 1 - index finger mcp (x, y) - # 2 - middle finger mcp (x, y) - # 3 - ring finger mcp (x, y) - # 4 - pinky mcp (x, y) - # 5 - thumb cmc (x, y) - # 6 - thumb mcp (x, y) - # - # mcp = Metacarpophalangeal Joint - the knuckle - # cmc = Carpometacarpal Joint - the base of the thumb - ml.utils.draw_skeleton(img, keypoints, palm_lines, kp_color=(255, 0, 0), line_color=(0, 255, 0)) + # keypoints is a ndarray of shape (7, 2) + # 0 - wrist (x, y) + # 1 - index finger mcp (x, y) + # 2 - middle finger mcp (x, y) + # 3 - ring finger mcp (x, y) + # 4 - pinky mcp (x, y) + # 5 - thumb cmc (x, y) + # 6 - thumb mcp (x, y) + # + # mcp = Metacarpophalangeal Joint - the knuckle + # cmc = Carpometacarpal Joint - the base of the thumb + ml.utils.draw_skeleton(img, keypoints, palm_lines, kp_color=(255, 0, 0), line_color=(0, 255, 0)) print(clock.fps(), "fps") diff --git a/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_multi_hand.py b/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_multi_hand.py index bfca10514..1e0379003 100644 --- a/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_multi_hand.py +++ b/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_multi_hand.py @@ -39,34 +39,31 @@ while True: img = csi0.snapshot() # palms is a list of ((x, y, w, h), score, keypoints) tuples - palms = palm_detection.predict([img]) + for r, score, keypoints in palm_detection.predict([img]): + # rect is (x, y, w, h) - enlarge by 3x for hand landmarks model + wider_rect = (r[0] - r[2], r[1] - r[3], r[2] * 3, r[3] * 3) + # Operate on just the ROI of the detected palm + n = Normalization(roi=wider_rect) - if palms: - for r, score, keypoints in palms[0]: - # rect is (x, y, w, h) - enlarge by 3x for hand landmarks model - wider_rect = (r[0] - r[2], r[1] - r[3], r[2] * 3, r[3] * 3) - # Operate on just the ROI of the detected palm - n = Normalization(roi=wider_rect) + # hands is a list of ((x, y, w, h), score, keypoints) tuples + # index 0 (if present) is left hand + # index 1 (if present) is right hand + hands = hand_landmarks.predict([n(img)]) - # hands is a list of ((x, y, w, h), score, keypoints) tuples - # index 0 (if present) is left hand - # index 1 (if present) is right hand - hands = hand_landmarks.predict([n(img)]) + # Draw bounding boxes around the detected hands and keypoints. + for i, detections in enumerate(hands): + for r, score, keypoints in detections: + ml.utils.draw_predictions(img, [r], ("right",) if i else ("left",), ((0, 0, 255),), format=None) - # Draw bounding boxes around the detected hands and keypoints. - for i, detections in enumerate(hands): - for r, score, keypoints in detections: - ml.utils.draw_predictions(img, [r], ("right",) if i else ("left",), ((0, 0, 255),), format=None) - - # keypoints: ndarray (21, 3) of hand joints (x, y, z) - # Indices follow MediaPipe convention: - # 0: wrist - # Thumb: 1 cmc, 2 mcp, 3 ip, 4 tip - # Index: 5 mcp, 6 pip, 7 dip, 8 tip - # Middle: 9 mcp, 10 pip, 11 dip, 12 tip - # Ring: 13 mcp, 14 pip, 15 dip, 16 tip - # Pinky: 17 mcp, 18 pip, 19 dip, 20 tip - # (cmc=base, mcp=knuckle, pip=mid, dip=distal, ip=thumb joint, tip=fingertip) - ml.utils.draw_skeleton(img, keypoints, hand_lines, kp_color=(255, 0, 0), line_color=(0, 255, 0)) + # keypoints: ndarray (21, 3) of hand joints (x, y, z) + # Indices follow MediaPipe convention: + # 0: wrist + # Thumb: 1 cmc, 2 mcp, 3 ip, 4 tip + # Index: 5 mcp, 6 pip, 7 dip, 8 tip + # Middle: 9 mcp, 10 pip, 11 dip, 12 tip + # Ring: 13 mcp, 14 pip, 15 dip, 16 tip + # Pinky: 17 mcp, 18 pip, 19 dip, 20 tip + # (cmc=base, mcp=knuckle, pip=mid, dip=distal, ip=thumb joint, tip=fingertip) + ml.utils.draw_skeleton(img, keypoints, hand_lines, kp_color=(255, 0, 0), line_color=(0, 255, 0)) print(clock.fps(), "fps") diff --git a/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_single_hand.py b/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_single_hand.py index 261f358b0..06e3dec3c 100644 --- a/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_single_hand.py +++ b/scripts/examples/03-Machine-Learning/00-TensorFlow/hand_landmarks_single_hand.py @@ -43,14 +43,11 @@ while True: if n is None: # palms is a list of ((x, y, w, h), score, keypoints) tuples - palms = palm_detection.predict([img]) - - if palms: - for r, score, keypoints in palms[0]: - # rect is (x, y, w, h) - enlarge by 3x for hand landmarks model - wider_rect = (r[0] - r[2], r[1] - r[3], r[2] * 3, r[3] * 3) - # Operate on just the ROI of the detected palm - n = Normalization(roi=wider_rect) + for r, score, keypoints in palm_detection.predict([img]): + # rect is (x, y, w, h) - enlarge by 3x for hand landmarks model + wider_rect = (r[0] - r[2], r[1] - r[3], r[2] * 3, r[3] * 3) + # Operate on just the ROI of the detected palm + n = Normalization(roi=wider_rect) else: # hands is a list of ((x, y, w, h), score, keypoints) tuples