| 5 | |
| 6 | |
| 7 | class HandDetector: |
| 8 | def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5): |
| 9 | self.mode = mode |
| 10 | self.maxHands = maxHands |
| 11 | self.detectionCon = detectionCon |
| 12 | self.trackCon = trackCon |
| 13 | |
| 14 | self.mpHands = mp.solutions.hands |
| 15 | self.hands = self.mpHands.Hands( |
| 16 | self.mode, self.maxHands, self.detectionCon, self.trackCon |
| 17 | ) |
| 18 | self.mpDraw = mp.solutions.drawing_utils |
| 19 | self.tipIds = [4, 8, 12, 16, 20] |
| 20 | |
| 21 | def findHands(self, img, draw=True): |
| 22 | imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 23 | self.results = self.hands.process(imgRGB) |
| 24 | # print(results.multi_hand_landmarks) |
| 25 | |
| 26 | if self.results.multi_hand_landmarks: |
| 27 | for handLms in self.results.multi_hand_landmarks: |
| 28 | if draw: |
| 29 | self.mpDraw.draw_landmarks( |
| 30 | img, handLms, self.mpHands.HAND_CONNECTIONS |
| 31 | ) |
| 32 | |
| 33 | return img |
| 34 | |
| 35 | def findPosition(self, img, handNo=0, draw=True): |
| 36 | xList = [] |
| 37 | yList = [] |
| 38 | bbox = [] |
| 39 | self.lmList = [] |
| 40 | if self.results.multi_hand_landmarks: |
| 41 | myHand = self.results.multi_hand_landmarks[handNo] |
| 42 | for id, lm in enumerate(myHand.landmark): |
| 43 | # print(id, lm) |
| 44 | h, w, c = img.shape |
| 45 | cx, cy = int(lm.x * w), int(lm.y * h) |
| 46 | xList.append(cx) |
| 47 | yList.append(cy) |
| 48 | # print(id, cx, cy) |
| 49 | self.lmList.append([id, cx, cy]) |
| 50 | if draw: |
| 51 | cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED) |
| 52 | |
| 53 | xmin, xmax = min(xList), max(xList) |
| 54 | ymin, ymax = min(yList), max(yList) |
| 55 | bbox = xmin, ymin, xmax, ymax |
| 56 | |
| 57 | if draw: |
| 58 | cv2.rectangle( |
| 59 | img, (xmin - 20, ymin - 20), (xmax + 20, ymax + 20), (0, 255, 0), 2 |
| 60 | ) |
| 61 | |
| 62 | return self.lmList, bbox |
| 63 | |
| 64 | def fingersUp(self): |
no outgoing calls
no test coverage detected