forked from Kaustav-04/Hack-The-6ix-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapPrev.py
113 lines (86 loc) · 4.29 KB
/
SwapPrev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import cv2
import mediapipe
import numpy as np
import HandTrackingModule as ht
import math
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import pyautogui as pa
import keyboard
from pynput.keyboard import Key, Controller
keyboard = Controller()
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(3, 1900)
cap.set(4, 1950)
# 3 and 4 are for setting the Width and height of the camera window respectively
detector = ht.HandDetector()
# Object for the HandTrackingModule we created
# devices = AudioUtilities.GetSpeakers()
# interface = devices.Activate(
# IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
# volume = cast(interface, POINTER(IAudioEndpointVolume))
# # Above things are all initialisation
# # volume.GetMute()
# # We do not need this statement
# # volume.GetMasterVolumeLevel()
# volRange = volume.GetVolumeRange()
# '''We see that the minimum range is -65 and the maximum is 0'''
# # volume.SetMasterVolumeLevel(-20.0, None)
# # When it is set to -20 then our volume is 26 and when it is 0 then it actually our volume is 100
# '''volume.GetVolumeRange() will give the value in the form of a tuple with the minimum value , maximum value and another parameter'''
# minVol = volRange[0]
# maxVol = volRange[1]
while (True):
_, img = cap.read()
img = cv2.flip(img, 1)
# Lateral shift
img = detector.findHands(img)
# Sending to the method created in the HandTrackingModule
idList = detector.findPosition(img, False)
if (len(idList) != 0):
x1, y1 = idList[4][1], idList[4][2]
# Finding the x and y coordinate for the index fingure
x2, y2 = idList[8][1], idList[8][2]
# Same as the above but this is for the thumb
x3, y3 = idList[12][1], idList[12][2]
x6, y6 = idList[12][1], idList[12][2]
x4, y4 = idList[16][1], idList[16][2]
x5, y5 = idList[20][1], idList[20][2]
cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x2, y2), 15, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x3, y3), 15, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x4, y4), 15, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x5, y5), 15, (255, 0, 255), cv2.FILLED)
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
cv2.line(img, (x2, y2), (x3, y3), (255, 0, 255), 3)
cv2.line(img, (x3, y3), (x4, y4), (255, 0, 255), 3)
cv2.line(img, (x4, y4), (x5, y5), (255, 0, 255), 3)
# Now we will calculate the length on the basis of which the volume will be generated
length = math.hypot(x6 - 256, y6 - 249)
print(length)
if(length > 750):
# with pa.hold('shift'):
keyboard.press(Key.media_previous)
# # Now hereafter for the volume functions we have to take in a library
# '''Here we see the maximum length coming to be almost 400 and the minimum length to be 15'''
# if length <= 50:
# cv2.circle(img, (x3, y3), 15, (0, 255, 0), cv2.FILLED)
# # This will turn the middle point to be a kind of button when the
# '''Converting length to volume range'''
# # Volume range is from -65 to 0
# vol = np.interp(length, [50, 400], [minVol, maxVol])
# # This method will change it proportionately
# # [50 , 400] are the minimum and the maximum lengths of the line
# # Corresponding to which the min and the max volumes are given in the list beside
# print(int(length), vol)
# volume.SetMasterVolumeLevel(vol, None)
# volBar = np.interp(length, [50, 400], [400, 150])
# # Here 400 is minimum because the coordinates are inverted here and origin is in the top left corner of the screen
# cv2.rectangle(img, (50, 150), (85, 400), (0, 255, 0), 3)
# # 3 is the synonym for not filling but just the thickness of the outline
# cv2.rectangle(img, (50, int(volBar)), (85, 400), (0, 255, 0), cv2.FILLED)
# volPercent = np.interp(length, [50, 400], [0, 100])
# cv2.putText(img, f'{int(volPercent)} %', (40, 100), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 1, (255, 0, 0), 3)
cv2.imshow("Result", img)
cv2.waitKey(1)