|
| 1 | +wCam, hCam = 640, 480 |
| 2 | +frameR = 100 # Frame Reduction |
| 3 | +smoothening = 7 |
| 4 | + |
| 5 | +pTime = 0 |
| 6 | +plocX, plocY = 0, 0 |
| 7 | +clocX, clocY = 0, 0 |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import cv2 |
| 11 | +import os |
| 12 | +from PIL import Image, ImageDraw |
| 13 | +import math |
| 14 | +import time |
| 15 | +import handTrackingModule as htm |
| 16 | +from ctypes import cast, POINTER |
| 17 | +from comtypes import CLSCTX_ALL |
| 18 | +from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume |
| 19 | +import math |
| 20 | + |
| 21 | +# Get default audio device using PyCAW |
| 22 | +devices = AudioUtilities.GetSpeakers() |
| 23 | +interface = devices.Activate( |
| 24 | + IAudioEndpointVolume._iid_, CLSCTX_ALL, None) |
| 25 | +volume = cast(interface, POINTER(IAudioEndpointVolume)) |
| 26 | +# Get current volume |
| 27 | +currentVolumeDb = volume.GetMasterVolumeLevel() |
| 28 | + |
| 29 | + |
| 30 | +wcam,hcam=640,480 |
| 31 | +cap=cv2.VideoCapture(0) |
| 32 | +cap.set(3,wcam) |
| 33 | +cap.set(4,hcam) |
| 34 | +pTime=0 |
| 35 | +detector = htm.handDetector(detectionCon=0.75) |
| 36 | + |
| 37 | +LastPx = 800 |
| 38 | +LastPy = 0 |
| 39 | + |
| 40 | +Percent = 0 |
| 41 | + |
| 42 | +while True: |
| 43 | + # Background = Image.open('Background.jpg') |
| 44 | + success,img=cap.read() |
| 45 | + |
| 46 | + img = detector.findHands(img, draw=True ) |
| 47 | + lmList=detector.findPosition(img,draw=False) |
| 48 | + #print(lmList) |
| 49 | + tipId=[4,8,12,16,20] |
| 50 | + |
| 51 | + #Rectangle |
| 52 | + |
| 53 | + if(len(lmList)!=0): |
| 54 | + fingers=[] |
| 55 | + #thumb |
| 56 | + if(lmList[tipId[0]][1]>lmList[tipId[0]-1][1]): |
| 57 | + fingers.append(1) |
| 58 | + else : |
| 59 | + fingers.append(0) |
| 60 | + #4 fingers |
| 61 | + for id in range(1,len(tipId)): |
| 62 | + |
| 63 | + if(lmList[tipId[id]][2]<lmList[tipId[id]-2][2]): |
| 64 | + fingers.append(1) |
| 65 | + |
| 66 | + else : |
| 67 | + fingers.append(0) |
| 68 | + if len(lmList) != 0: |
| 69 | + x1, y1 = lmList[8][1:] #2nd finger |
| 70 | + # x0, y0 = lmList[12][1:] #Middle Finder |
| 71 | + x0, y0 = lmList[4][1:] #thumb |
| 72 | + |
| 73 | + # print(f'1{fingers[0]}') |
| 74 | + # print(f'2{fingers[1]}') |
| 75 | + # print(f'3{fingers[2]}') |
| 76 | + # print(f'4{fingers[3]}') |
| 77 | + # print(f'5{fingers[4]}') |
| 78 | + |
| 79 | + # if fingers[1] == 1 and fingers[2] == 0: #for 2nd finger and al finger close |
| 80 | + if fingers[1] == 1 and fingers[0] == 1: |
| 81 | + #2nd Finger |
| 82 | + x3 = np.interp(x1, (frameR, wCam - frameR), (0, 1080)) |
| 83 | + y3 = np.interp(y1, (frameR, hCam - frameR), (0, 720)) |
| 84 | + |
| 85 | + clocX = plocX + (x3 - plocX) / smoothening |
| 86 | + clocY = plocY + (y3 - plocY) / smoothening |
| 87 | + |
| 88 | + cv2.circle(img, (x1, y1), 15, (0, 0,255), cv2.FILLED) |
| 89 | + plocX, plocY = clocX, clocY |
| 90 | + |
| 91 | + #THUMB |
| 92 | + x3 = np.interp(x0, (frameR, wCam - frameR), (0, 1080)) |
| 93 | + y3 = np.interp(y0, (frameR, hCam - frameR), (0, 720)) |
| 94 | + |
| 95 | + clocX = plocX + (x3 - plocX) / smoothening |
| 96 | + clocY = plocY + (y3 - plocY) / smoothening |
| 97 | + |
| 98 | + cv2.circle(img, (x0, y0), 15, (0, 0, 255), cv2.FILLED) |
| 99 | + plocX, plocY = clocX, clocY |
| 100 | + |
| 101 | + cv2.line(img,(x1,y1),(x0,y0),(0,0,255),2) |
| 102 | + distance = math.sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)) |
| 103 | + # print(distance) |
| 104 | + |
| 105 | + |
| 106 | + if distance<50: |
| 107 | + distance = 50 |
| 108 | + elif distance>250: |
| 109 | + distance=250 |
| 110 | + Percent = round((distance-50.0)/2) |
| 111 | + # print(Percent) |
| 112 | + |
| 113 | + |
| 114 | + # Changevolume = round(0.6525*math.log(Percent/100)*100) |
| 115 | + Changevolume = round(math.log((Percent/10)+1)*50*0.54 ) |
| 116 | + print(Changevolume) |
| 117 | + volume.SetMasterVolumeLevel(-65.25+Changevolume, None) |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + height = int(340 - (Percent*2.0)) |
| 122 | + print(height) |
| 123 | + cv2.rectangle(img,(570,height),(620,340),(0,255,0),-1) |
| 124 | + cv2.rectangle(img,(570,140),(620,340),(255,0,0),2) |
| 125 | + img = cv2.flip(img, 1) |
| 126 | + |
| 127 | + font = cv2.FONT_HERSHEY_SIMPLEX |
| 128 | + bottomLeftCornerOfText = (15,130) |
| 129 | + fontScale = 1 |
| 130 | + fontColor = (0,0,0) |
| 131 | + lineType = 2 |
| 132 | + |
| 133 | + cv2.putText(img,f'{Percent}%', bottomLeftCornerOfText, font, fontScale,fontColor,lineType) |
| 134 | + |
| 135 | + cTime=time.time() |
| 136 | + fps=1.0/float(cTime-pTime) |
| 137 | + pTime=cTime |
| 138 | + |
| 139 | + cv2.imshow("image",img) |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + # Background = cv2.imread('Background.jpg') |
| 144 | + # Background = cv2.flip(Background, 1) |
| 145 | + # cv2.imshow('img',Background) |
| 146 | + |
| 147 | + |
| 148 | + if(cv2.waitKey(1) & 0xFF== ord('q')): |
| 149 | + break |
| 150 | + |
| 151 | + |
| 152 | +cap.release() |
| 153 | + |
| 154 | +cv2.destroyAllWindows() |
0 commit comments