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