Advertisement
Guest User

HandTrackingModule.Py

a guest
Mar 29th, 2022
29
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. def main():
  4.     cap = cv2.VideoCapture(0)
  5.     detector = HandDetector(detectionCon=0.8, maxHands=2)
  6.     while True:
  7.         # Get image frame
  8.         success, img = cap.read()
  9.         # Find the hand and its landmarks
  10.         hands, img = detector.findHands(img)  # with draw
  11.         # hands = detector.findHands(img, draw=False)  # without draw
  12.  
  13.         if hands:
  14.             # Hand 1
  15.             hand1 = hands[0]
  16.             lmList1 = hand1["lmList"]  # List of 21 Landmark points
  17.             bbox1 = hand1["bbox"]  # Bounding box info x,y,w,h
  18.             centerPoint1 = hand1['center']  # center of the hand cx,cy
  19.             handType1 = hand1["type"]  # Handtype Left or Right
  20.  
  21.             fingers1 = detector.fingersUp(hand1)
  22.  
  23.             if len(hands) == 2:
  24.                 # Hand 2
  25.                 hand2 = hands[1]
  26.                 lmList2 = hand2["lmList"]  # List of 21 Landmark points
  27.                 bbox2 = hand2["bbox"]  # Bounding box info x,y,w,h
  28.                 centerPoint2 = hand2['center']  # center of the hand cx,cy
  29.                 handType2 = hand2["type"]  # Hand Type "Left" or "Right"
  30.  
  31.                 fingers2 = detector.fingersUp(hand2)
  32.  
  33.                 # Find Distance between two Landmarks. Could be same hand or different hands
  34.                 length, info, img = detector.findDistance(lmList1[8][0:2], lmList2[8][0:2], img)  # with draw
  35.                 # length, info = detector.findDistance(lmList1[8], lmList2[8])  # with draw
  36.         # Display
  37.         cv2.imshow("Image", img)
  38.         cv2.waitKey(1)
  39.  
  40.  
  41. if __name__ == "__main__":
  42.     main()
  43.  
Advertisement
RAW Paste Data Copied
Advertisement