Advertisement
rhandycan1

GestureDetection_VolumeControl

Jan 18th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | Source Code | 0 0
  1. #Instructions: name this folder as "handtracingModule", it will be imported on the Main Module
  2. import cv2
  3. import mediapipe as mp
  4. import time
  5.  
  6.  
  7. class handDetector():
  8.     def __init__(self, mode=False, maxHands=2, modelComp=1, detectionCon=0.5, trackCon=0.5):
  9.         self.mode = mode
  10.         self.maxHands = maxHands
  11.         self.detectionCon = detectionCon
  12.         self.trackCon = trackCon
  13.         self.modelComp = modelComp
  14.  
  15.         self.mpHands = mp.solutions.hands
  16.         self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.modelComp, self.detectionCon, self.trackCon)
  17.         self.mpDraw = mp.solutions.drawing_utils
  18.  
  19.     def findHands(self, img, draw=True):
  20.         imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  21.         self.results = self.hands.process(imgRGB)
  22.         self.drawSpecCircle = self.mpDraw.DrawingSpec(thickness=1, circle_radius=1, color=(255, 51,51))
  23.         self.drawSpecLine = self.mpDraw.DrawingSpec(thickness=3, circle_radius=2, color=(255, 51, 153))
  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(img, handLms,
  30.                                                self.mpHands.HAND_CONNECTIONS,self.drawSpecCircle,self.drawSpecLine)
  31.         return img
  32.  
  33.     def findPosition(self, img, handNo=0, draw=True):
  34.  
  35.         lmList = []
  36.         if self.results.multi_hand_landmarks:
  37.             myHand = self.results.multi_hand_landmarks[handNo]
  38.             for id, lm in enumerate(myHand.landmark):
  39.                 # print(id, lm)
  40.                 h, w, c = img.shape
  41.                 cx, cy = int(lm.x * w), int(lm.y * h)
  42.                 # print(id, cx, cy)
  43.                 lmList.append([id, cx, cy])
  44.                 if draw:
  45.                     cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
  46.  
  47.         return lmList
  48.  
  49.  
  50. def main():
  51.     pTime = 0
  52.     cTime = 0
  53.     cap = cv2.VideoCapture(0)
  54.     detector = handDetector()
  55.     while True:
  56.         success, img = cap.read()
  57.         img = detector.findHands(img)
  58.         lmList = detector.findPosition(img)
  59.         if len(lmList) != 0:
  60.             print(lmList[4])
  61.  
  62.         cTime = time.time()
  63.         fps = 1 / (cTime - pTime)
  64.         pTime = cTime
  65.  
  66.         cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
  67.                     (255, 0, 255), 3)
  68.  
  69.         cv2.imshow("Image", img)
  70.         cv2.waitKey(1)
  71.  
  72.  
  73. if __name__ == "__main__":
  74.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement