-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.VolumeHandControl.py
109 lines (98 loc) · 4.26 KB
/
05.VolumeHandControl.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
import cv2
import time
import numpy as np
import HandTrackingModule as HTM
import math
import subprocess
# from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
# call(["osascript -e 'set volume output volume 100'"], shell=True)
# same code below
# from osascript import run
from Foundation import NSAppleScript as NSA
# import applescript
#import os
#os.system('say "your program has finished"')
##### parameter
pTime = 0
#### Video Controler
cap = cv2.VideoCapture(0)
#### Object Detection Module
detector = HTM.handDetector()
s = NSA.alloc().initWithSource_("tell app \"Finder\" to activate")
s.executeAndReturnError_(None)
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img, draw=False)
cv2.rectangle(img, (50,100), (85,300), (0,255,0), 3)
if len(lmList) > 21:
x1, y1 = lmList[4][1], lmList[4][2]
x2, y2 = lmList[8][1], lmList[8][2]
cx12, cy12 = (x1 + x2) // 2, (y1 + y2) // 2
x3, y3 = lmList[25][1], lmList[25][2]
x4, y4 = lmList[29][1], lmList[29][2]
cx34, cy34 = (x3 + x4) // 2, (y3 + y4) // 2
cv2.circle(img, (x1,y1), 15, (255,0,255), cv2.FILLED)
cv2.circle(img, (x2,y2), 15, (255,0,255), cv2.FILLED)
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
cv2.circle(img, (cx12,cy12), 10, (255,0,255), cv2.FILLED)
cv2.circle(img, (x3,y3), 15, (255,255,255), cv2.FILLED)
cv2.circle(img, (x4,y4), 15, (255,255,255), cv2.FILLED)
length = math.hypot(x2-x1, y2-y1)
#if length <= 30:
# length = 30
#elif length >= 230:
# length = 230
range100 = lambda x : 30 if x <= 30 else 230 if x >= 230 else x
length = range100(length)
print('length : ',length)
s = NSA.alloc().initWithSource_('set volume output volume {}'.format(int(length-30)/2))
s.executeAndReturnError_(None)
# run('set volume output volume {}'.format(int(length-30)/2)),
# subprocess.call("osascript -e 'set volume output volume {}'".format((length-30)/2), shell=True)
# out= subprocess.check_output(["osascript -e 'output volume of (get volume settings)'"], shell=True)
#code, out, err = osascript.run("output volume of (get volume settings)")
# print('out : ', out)
print(100+length)
cv2.rectangle(img, (50, (300-int(length-30))), (85,300), (255,0,0), cv2.FILLED)
elif len(lmList) != 0:
print(lmList[4], lmList[8])
print('---')
x1, y1 = lmList[4][1], lmList[4][2]
x2, y2 = lmList[8][1], lmList[8][2]
cx12, cy12 = (x1 + x2) // 2, (y1 + y2) // 2
cv2.circle(img, (x1,y1), 15, (255,0,255), cv2.FILLED)
cv2.circle(img, (x2,y2), 15, (255,0,255), cv2.FILLED)
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
cv2.circle(img, (cx12,cy12), 10, (255,0,255), cv2.FILLED)
length = math.hypot(x2-x1, y2-y1)
#if length <= 30:
# length = 30
#elif length >= 230:
# length = 230
range100 = lambda x : 30 if x <= 30 else 230 if x >= 230 else x
length = range100(length)
print('length : ',length)
#run('volume {}'.format(int(length-30)/2))
# run('set volume output volume {}'.format(int(length-30)/2))
s = NSA.alloc().initWithSource_('set volume output volume {}'.format(int(length-30)/2))
s.executeAndReturnError_(None)
#subprocess.call("osascript -e 'set volume output volume {}'".format((length-30)/2), shell=True)
# out= subprocess.check_output(["osascript -e 'output volume of (get volume settings)'"], shell=True)
# code, out, err = osascript.run("output volume of (get volume settings)")
# print('out : ', out)
print(100+length)
cv2.rectangle(img, (50, (300-int(length-30))), (85,300), (255,0,0), cv2.FILLED)
cv2.putText(img, 'Volume: {} %'.format(int(length-30)/2), (40, 400),
cv2.FONT_HERSHEY_COMPLEX,
1, (255, 0, 0), 3)
else:
print('Hands up!')
cTime = time.time()
fps = 1/(cTime - pTime)
pTime = cTime
cv2.putText(img, 'FPS: {}'.format(int(fps)), (40, 50),
cv2.FONT_HERSHEY_COMPLEX,
1, (255, 0, 0), 3)
cv2.imshow("Img", img)
cv2.waitKey(1)