-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmediaControl.py
More file actions
executable file
·97 lines (84 loc) · 2.57 KB
/
mediaControl.py
File metadata and controls
executable file
·97 lines (84 loc) · 2.57 KB
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
import cv2
import numpy as np
import pyautogui
from time import sleep
import sys
import _thread
config = {
"capture_port": 0,
"screenWidth": 1920,
"screenHeight": 1080,
"frameWidth": 640,
"frameHeight": 480,
"button_pause_time": 1,
"button_action_key": "space"
}
try:
with open('config.json', 'r') as f:
config = json.load(f)
except:
pass
capture_port = int(sys.argv[1]) if len(sys.argv) > 1 else config['capture_port']
screenHeight = int(sys.argv[3]) if len(sys.argv) > 3 else config['screenHeight']
screenWidth = int(sys.argv[2]) if len(sys.argv) > 2 else config['screenWidth']
frameWidth = config['frameWidth']
frameHeight = config['frameHeight']
button_pause_time = config['button_pause_time']
button_action_key = config['button_action_key']
fist = cv2.CascadeClassifier('./data/fist.xml')
palm = cv2.CascadeClassifier('./data/open_palm.xml')
cap = cv2.VideoCapture(capture_port)
scaling_factor = 0.5
pressed = False
spacePressed = False
def control_by_rect(palm_rects, fist_rects):
global pressed
global spacePressed
for (x,y,w,h) in palm_rects:
centerx = 150
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,100), 5)
centerx=x+w//2
centery=y+h//2
cv2.circle(frame,(centerx,centery),4,(200,200,0),3)
if (pressed==False and centerx<120):
pyautogui.press('left')
pressed = True
sleep(1)
if (pressed==False and centerx>210):
pyautogui.press('right')
pressed = True
sleep(1)
if (pressed==False and centery<120):
pyautogui.press('up')
pressed = True
sleep(1)
if (pressed==False and centery>160):
pyautogui.press('down')
pressed = True
sleep(1)
# if (centerx>120 and centerx<210):
else:
pressed = False
print (centery) #show the x position
for (x,y,w,h) in fist_rects:
cv2.rectangle(frame, (x, y), (x+w,y+h), (0,255,100), 5)
if (spacePressed==False):
pyautogui.press(button_action_key)
spacePressed = True
print('Button action called')
sleep(1)
else:
spacePressed = False
while True:
ret, frame = cap.read()
frame = cv2.flip(frame,1)
# frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
fist_rects = fist.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=5)
palm_rects = palm.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=5)
_thread.start_new_thread(control_by_rect,(palm_rects, fist_rects))
cv2.imshow('Media dolanshyk', frame)
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
cv2.destroyAllWindows()