-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.py
62 lines (49 loc) · 1.85 KB
/
scroll.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
# importing libraries
import cv2
import pyautogui as pag
import numpy as np
# changing these parameters will affect the triggers
# more MAGNITUDE, more SWIFT movement of hand
Y_DOWN_DIFF = 20
Y_UP_DIFF = -20
# selecting the webcam
# 0 traditionally, 1 if 0 is not working for you
capture = cv2.VideoCapture(0)
# selecting the YELLOW color range for detecting in the video
# this color will act as a trigger, you can set your own color by changing these values
yellowLower = np.array([22, 93, 0])
yellowUpper = np.array([45, 255, 255])
# helping variable, for storing the previous location of the image
prev_y = 0
# for streaming webcam
while True:
ret, frame = capture.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, yellowLower, yellowUpper)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for item in contours:
area = cv2.contourArea(item)
# checking only if area is bigger than 300 units, to remove noise
if area > 300:
x, y, w, h = cv2.boundingRect(item)
# cv2.drawContours(frame, contours, -1, (0, 255, 0), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if (y - prev_y) > Y_DOWN_DIFF:
pag.press('pagedown')
print('pressing pageDown key')
if (y - prev_y) < Y_UP_DIFF:
pag.press('pageup')
print('pressing pageUp key')
# setting up current location to previous location
prev_y = y
# display the webcam frame
cv2.imshow('frame', frame)
# display the masked frame
cv2.imshow('mask', mask)
# exit the stream, program when q is pressed
if cv2.waitKey(10) == ord('q'):
break
# release the webcam
capture.release()
# destroy all the windows
cv2.destroyAllWindows()