|
| 1 | +import cv2 |
| 2 | +import mediapipe as mp |
| 3 | +import time |
| 4 | +import math |
| 5 | +import numpy as np |
| 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(self.mode, self.maxHands, |
| 16 | + self.detectionCon, self.trackCon) |
| 17 | + self.mpDraw = mp.solutions.drawing_utils |
| 18 | + self.tipIds = [4, 8, 12, 16, 20] |
| 19 | + |
| 20 | +def findHands(self, img, draw=True): |
| 21 | + imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 22 | + self.results = self.hands.process(imgRGB) |
| 23 | + # print(results.multi_hand_landmarks) |
| 24 | + |
| 25 | + if self.results.multi_hand_landmarks: |
| 26 | + for handLms in self.results.multi_hand_landmarks: |
| 27 | + if draw: |
| 28 | + self.mpDraw.draw_landmarks(img, handLms, |
| 29 | + self.mpHands.HAND_CONNECTIONS) |
| 30 | + |
| 31 | + return img |
| 32 | + |
| 33 | +def findPosition(self, img, handNo=0, draw=True): |
| 34 | + xList = [] |
| 35 | + yList = [] |
| 36 | + bbox = [] |
| 37 | + self.lmList = [] |
| 38 | + if self.results.multi_hand_landmarks: |
| 39 | + myHand = self.results.multi_hand_landmarks[handNo] |
| 40 | + for id, lm in enumerate(myHand.landmark): |
| 41 | + # print(id, lm) |
| 42 | + h, w, c = img.shape |
| 43 | + cx, cy = int(lm.x * w), int(lm.y * h) |
| 44 | + xList.append(cx) |
| 45 | + yList.append(cy) |
| 46 | + # print(id, cx, cy) |
| 47 | + self.lmList.append([id, cx, cy]) |
| 48 | + if draw: |
| 49 | + cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED) |
| 50 | + |
| 51 | + xmin, xmax = min(xList), max(xList) |
| 52 | + ymin, ymax = min(yList), max(yList) |
| 53 | + bbox = xmin, ymin, xmax, ymax |
| 54 | + |
| 55 | + if draw: |
| 56 | + cv2.rectangle(img, (xmin - 20, ymin - 20), (xmax + 20, ymax + 20), |
| 57 | + (0, 255, 0), 2) |
| 58 | + |
| 59 | + return self.lmList, bbox |
| 60 | + |
| 61 | +def fingersUp(self): |
| 62 | + fingers = [] |
| 63 | + # Thumb |
| 64 | + if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]: |
| 65 | + fingers.append(1) |
| 66 | + else: |
| 67 | + fingers.append(0) |
| 68 | + |
| 69 | + # Fingers |
| 70 | + for id in range(1, 5): |
| 71 | + if self.lmList[self.tipIds[id]][2] < self.lmList[self.tipIds[id] - 2][2]: |
| 72 | + fingers.append(1) |
| 73 | + else: |
| 74 | + fingers.append(0) |
| 75 | + |
| 76 | + # totalFingers = fingers.count(1) |
| 77 | + |
| 78 | + return fingers |
| 79 | + |
| 80 | +def findDistance(self, p1, p2, img, draw=True,r=15, t=3): |
| 81 | + x1, y1 = self.lmList[p1][1:] |
| 82 | + x2, y2 = self.lmList[p2][1:] |
| 83 | + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 |
| 84 | + |
| 85 | + if draw: |
| 86 | + cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), t) |
| 87 | + cv2.circle(img, (x1, y1), r, (255, 0, 255), cv2.FILLED) |
| 88 | + cv2.circle(img, (x2, y2), r, (255, 0, 255), cv2.FILLED) |
| 89 | + cv2.circle(img, (cx, cy), r, (0, 0, 255), cv2.FILLED) |
| 90 | + length = math.hypot(x2 - x1, y2 - y1) |
| 91 | + |
| 92 | + return length, img, [x1, y1, x2, y2, cx, cy] |
| 93 | + |
| 94 | +def main(): |
| 95 | + pTime = 0 |
| 96 | + cTime = 0 |
| 97 | + cap = cv2.VideoCapture(1) |
| 98 | + detector = handDetector() |
| 99 | + while True: |
| 100 | + success, img = cap.read() |
| 101 | + img = detector.findHands(img) |
| 102 | + lmList, bbox = detector.findPosition(img) |
| 103 | + if len(lmList) != 0: |
| 104 | + print(lmList[4]) |
| 105 | + |
| 106 | + cTime = time.time() |
| 107 | + fps = 1 / (cTime - pTime) |
| 108 | + pTime = cTime |
| 109 | + |
| 110 | + cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, |
| 111 | + (255, 0, 255), 3) |
| 112 | + |
| 113 | + cv2.imshow("Image", img) |
| 114 | + cv2.waitKey(1) |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments