-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mp Hands nodes & segmentation msg creation #2
+742
−67
Merged
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
38be7e8
New import path.
kkeroo c7a566f
Segmentation msg creation and new selfi seg. output.
kkeroo e7dc367
Nodes for MP hands.
kkeroo eac159c
More general function for depth or segmentation message creation.
kkeroo 65c8b75
Typo.
kkeroo 869b607
Adding HandLandmarksDescriptor.
kkeroo 3434ca3
Removing unnecessary variables.
kkeroo d796a21
Renaming mp hands nodes.
kkeroo 26a53e4
Rename MP selfie segmentation node.
kkeroo 5437ca8
HandKeypoints message using property.
kkeroo 9a486bc
Depth and Segmentation msg creation.
kkeroo c084392
Segmentation msg added to host node.
kkeroo b1a5ce0
Removed unnecessary prints.
kkeroo d7171e8
Renamed mediapipe.
kkeroo df7a38b
Normalize hand landmarks.
kkeroo 8d6f6b6
Anchors and decoding in utils.
kkeroo 3e90e4b
Mediapipe metadata added.
kkeroo bf463a2
general multiclass segmentation parser.
kkeroo 26381fa
Validating input in create functions.
kkeroo 2deaeee
Function for creating detection and keypoint msgs.
kkeroo 293ce23
Moved anchors function.
kkeroo 45131f7
argmax to get segmentation classes.
kkeroo 827d5c8
Updated docs.
kkeroo 25ce9cc
Keypoints message.
kkeroo 47b8dd7
Generic keypoints.
kkeroo 847165f
Correction: handdedness -> handedness.
kkeroo e2d0534
Init file changed.
kkeroo f8a48f1
1 channel in 3rd dim. validation.
kkeroo 14a9e4c
Remove unnecessary atributes.
kkeroo 7d9f6d4
Merge branch 'main' into mp_hands
kkeroo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .img_detections import ImgDetectionsWithKeypoints | ||
from .img_detections import ImgDetectionsWithKeypoints | ||
from .keypoints import HandKeypoints, Keypoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import depthai as dai | ||
from typing import List | ||
|
||
|
||
class Keypoints(dai.Buffer): | ||
def __init__(self): | ||
super().__init__() | ||
self._keypoints: List[dai.Point3f] = [] | ||
|
||
@property | ||
def keypoints(self) -> List[dai.Point3f]: | ||
return self._keypoints | ||
|
||
@keypoints.setter | ||
def keypoints(self, value: List[dai.Point3f]): | ||
if not isinstance(value, list): | ||
raise TypeError("keypoints must be a list.") | ||
for item in value: | ||
if not isinstance(item, dai.Point3f): | ||
raise TypeError("All items in keypoints must be of type dai.Point3f.") | ||
self._keypoints = value | ||
|
||
|
||
class HandKeypoints(Keypoints): | ||
def __init__(self): | ||
Keypoints.__init__(self) | ||
self._confidence: float = 0.0 | ||
self._handdedness: float = 0.0 | ||
|
||
@property | ||
def confidence(self) -> float: | ||
return self._confidence | ||
|
||
@confidence.setter | ||
def confidence(self, value: float): | ||
if not isinstance(value, float): | ||
raise TypeError("confidence must be a float.") | ||
self._confidence = value | ||
|
||
@property | ||
def handdedness(self) -> float: | ||
return self._handdedness | ||
|
||
@handdedness.setter | ||
def handdedness(self, value: float): | ||
if not isinstance(value, float): | ||
raise TypeError("handdedness must be a float.") | ||
self._handdedness = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import depthai as dai | ||
import numpy as np | ||
import cv2 | ||
|
||
from .utils.message_creation import create_detection_message | ||
from .utils.medipipe import generate_anchors_and_decode | ||
|
||
class MPHandDetectionParser(dai.node.ThreadedHostNode): | ||
def __init__( | ||
self, | ||
score_threshold=0.5, | ||
nms_threshold=0.5, | ||
top_k=100 | ||
): | ||
dai.node.ThreadedHostNode.__init__(self) | ||
self.input = dai.Node.Input(self) | ||
self.out = dai.Node.Output(self) | ||
|
||
self.score_threshold = score_threshold | ||
self.nms_threshold = nms_threshold | ||
self.top_k = top_k | ||
|
||
def setConfidenceThreshold(self, threshold): | ||
self.score_threshold = threshold | ||
|
||
def setNMSThreshold(self, threshold): | ||
self.nms_threshold = threshold | ||
|
||
def setTopK(self, top_k): | ||
self.top_k = top_k | ||
|
||
def run(self): | ||
""" | ||
Postprocessing logic for MediPipe Hand detection model. | ||
Returns: | ||
dai.ImgDetections containing bounding boxes, labels, and confidence scores of detected hands. | ||
""" | ||
|
||
while self.isRunning(): | ||
|
||
try: | ||
output: dai.NNData = self.input.get() | ||
except dai.MessageQueue.QueueException as e: | ||
break # Pipeline was stopped | ||
|
||
tensorInfo = output.getTensorInfo("Identity") | ||
bboxes = output.getTensor(f"Identity").reshape(2016, 18).astype(np.float32) | ||
bboxes = (bboxes - tensorInfo.qpZp) * tensorInfo.qpScale | ||
tensorInfo = output.getTensorInfo("Identity_1") | ||
scores = output.getTensor(f"Identity_1").reshape(2016).astype(np.float32) | ||
scores = (scores - tensorInfo.qpZp) * tensorInfo.qpScale | ||
|
||
decoded_bboxes = generate_anchors_and_decode(bboxes=bboxes, scores=scores, threshold=self.score_threshold, scale=192) | ||
|
||
bboxes = [] | ||
scores = [] | ||
|
||
for hand in decoded_bboxes: | ||
extended_points = hand.rect_points | ||
xmin = int(min(extended_points[0][0], extended_points[1][0])) | ||
ymin = int(min(extended_points[0][1], extended_points[1][1])) | ||
xmax = int(max(extended_points[2][0], extended_points[3][0])) | ||
ymax = int(max(extended_points[2][1], extended_points[3][1])) | ||
|
||
bboxes.append([xmin, ymin, xmax, ymax]) | ||
scores.append(hand.pd_score) | ||
|
||
indices = cv2.dnn.NMSBoxes(bboxes, scores, self.score_threshold, self.nms_threshold, top_k=self.top_k) | ||
bboxes = np.array(bboxes)[indices] | ||
scores = np.array(scores)[indices] | ||
|
||
detections_msg = create_detection_message(bboxes, scores, labels=None) | ||
self.out.send(detections_msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import depthai as dai | ||
import numpy as np | ||
import cv2 | ||
|
||
from .utils.message_creation import create_hand_keypoints_message | ||
|
||
class MPHandLandmarkParser(dai.node.ThreadedHostNode): | ||
def __init__( | ||
self, | ||
score_threshold=0.5, | ||
scale_factor=224 | ||
): | ||
dai.node.ThreadedHostNode.__init__(self) | ||
self.input = dai.Node.Input(self) | ||
self.out = dai.Node.Output(self) | ||
|
||
self.score_threshold = score_threshold | ||
self.scale_factor = scale_factor | ||
|
||
def setScoreThreshold(self, threshold): | ||
self.score_threshold = threshold | ||
|
||
def setScaleFactor(self, scale_factor): | ||
self.scale_factor = scale_factor | ||
|
||
def run(self): | ||
""" | ||
Postprocessing logic for MediaPipe Hand landmark model. | ||
Returns: | ||
HandLandmarks containing normalized 21 landmarks, confidence score, and handdedness score (right or left hand). | ||
""" | ||
|
||
while self.isRunning(): | ||
|
||
try: | ||
output: dai.NNData = self.input.get() | ||
except dai.MessageQueue.QueueException as e: | ||
break # Pipeline was stopped | ||
|
||
tensorInfo = output.getTensorInfo("Identity") | ||
landmarks = output.getTensor(f"Identity").reshape(21, 3).astype(np.float32) | ||
landmarks = (landmarks - tensorInfo.qpZp) * tensorInfo.qpScale | ||
tensorInfo = output.getTensorInfo("Identity_1") | ||
hand_score = output.getTensor(f"Identity_1").reshape(-1).astype(np.float32) | ||
hand_score = (hand_score - tensorInfo.qpZp) * tensorInfo.qpScale | ||
hand_score = hand_score[0] | ||
tensorInfo = output.getTensorInfo("Identity_2") | ||
handedness = output.getTensor(f"Identity_2").reshape(-1).astype(np.float32) | ||
handedness = (handedness - tensorInfo.qpZp) * tensorInfo.qpScale | ||
handedness = handedness[0] | ||
|
||
# normalize landmarks | ||
landmarks /= self.scale_factor | ||
|
||
hand_landmarks_msg = create_hand_keypoints_message(landmarks, float(handedness), float(hand_score), self.score_threshold) | ||
self.out.send(hand_landmarks_msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import depthai as dai | ||
import numpy as np | ||
import cv2 | ||
from .utils.message_creation import create_segmentation_message | ||
|
||
class SegmentationParser(dai.node.ThreadedHostNode): | ||
def __init__( | ||
self, | ||
threshold=0.5, | ||
num_classes=2, | ||
): | ||
dai.node.ThreadedHostNode.__init__(self) | ||
self.input = dai.Node.Input(self) | ||
self.out = dai.Node.Output(self) | ||
|
||
self.threshold = threshold | ||
self.num_classes = num_classes | ||
|
||
def setConfidenceThreshold(self, threshold): | ||
self.threshold = threshold | ||
|
||
def setNumClasses(self, num_classes): | ||
self.num_classes = num_classes | ||
|
||
def run(self): | ||
""" | ||
Postprocessing logic for Segmentation model with `num_classes` classes including background at index 0. | ||
|
||
Returns: | ||
Segmenation mask with `num_classes` classes, 0 - background. | ||
""" | ||
|
||
while self.isRunning(): | ||
|
||
try: | ||
output: dai.NNData = self.input.get() | ||
except dai.MessageQueue.QueueException as e: | ||
break # Pipeline was stopped | ||
|
||
segmentation_mask = output.getTensor("output") | ||
segmentation_mask = segmentation_mask[0] # num_clases x H x W | ||
segmentation_mask = np.vstack((np.zeros((1, segmentation_mask.shape[1], segmentation_mask.shape[2]), dtype=np.float32), segmentation_mask)) | ||
segmentation_mask[segmentation_mask < self.threshold] = 0 | ||
overlay_image = np.argmax(segmentation_mask, axis=0).reshape(segmentation_mask.shape[1], segmentation_mask.shape[2], 1).astype(np.uint8) | ||
|
||
imgFrame = create_segmentation_message(overlay_image) | ||
self.out.send(imgFrame) |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mediapipe_selfie_segmentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done