Spaces:
No application file
No application file
| import cv2 | |
| import mediapipe as mp | |
| class HandDetector: | |
| def __init__(self, mode=False, maxHands=2, detectionCon=0.5, minTrackCon=0.5): | |
| self.mode = mode | |
| self.maxHands = maxHands | |
| self.detectionCon = detectionCon | |
| self.minTrackCon = minTrackCon | |
| self.mpHands = mp.solutions.hands | |
| self.hands = self.mpHands.Hands(static_image_mode=self.mode, max_num_hands=self.maxHands, | |
| min_detection_confidence=self.detectionCon, | |
| min_tracking_confidence=self.minTrackCon) | |
| self.mpDraw = mp.solutions.drawing_utils | |
| self.lmList = [] | |
| def findHands(self, img, draw=True, flipType=True): | |
| imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| self.results = self.hands.process(imgRGB) | |
| Hand = [] | |
| h, w, c = img.shape | |
| if self.results.multi_hand_landmarks: | |
| for handType, handLms in zip(self.results.multi_handedness, self.results.multi_hand_landmarks): | |
| myHand = {} | |
| ## lmList | |
| mylmList = [] | |
| xList = [] | |
| yList = [] | |
| for id, lm in enumerate(handLms.landmark): | |
| px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w) | |
| # px, py, pz = lm.x, lm.y, lm.z | |
| mylmList.append([px, py, pz]) | |
| xList.append(px) | |
| yList.append(py) | |
| ## bbox | |
| xmin, xmax = min(xList), max(xList) | |
| ymin, ymax = min(yList), max(yList) | |
| boxW, boxH = xmax - xmin, ymax - ymin | |
| bbox = xmin, ymin, boxW, boxH | |
| myHand["lmList"] = mylmList | |
| myHand["bbox"] = bbox | |
| if flipType: | |
| if handType.classification[0].label == "Right": | |
| myHand["type"] = "Left" | |
| else: | |
| myHand["type"] = "Right" | |
| else: | |
| myHand["type"] = handType.classification[0].label | |
| Hand.append(myHand) | |
| if draw: | |
| return Hand, img | |
| else: | |
| return Hand |