-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollTracker.py
More file actions
71 lines (49 loc) · 1.92 KB
/
scrollTracker.py
File metadata and controls
71 lines (49 loc) · 1.92 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
import cv2
import numpy as np
import pyautogui
time = 0
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
low_red = np.array([0, 140, 172])
high_red = np.array([9, 255, 255])
low_green = np.array([44, 168, 45])
high_green = np.array([140,255,158])
low_yellow = np.array([16, 139, 16])
high_yellow = np.array([36, 209, 255])
low_pink = np.array([160, 141, 161])
high_pink = np.array([179, 255, 255])
x, y, w, h = 600, 400, 100, 50 # simply hardcoded the values
track_window = (x, y, w, h)
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
while True:
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame, 1)
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#pink Tracking
mask_red = cv2.inRange(hsv_frame, low_pink, high_pink)
roi_hist_red = cv2.calcHist([hsv_frame],[0], mask_red,[180],[0,180])
cv2.normalize(roi_hist_red, roi_hist_red, 0, 255, cv2.NORM_MINMAX)
hsv_red = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst_red = cv2.calcBackProject([hsv_red], [0],roi_hist_red,[0,180],1)
ret, track_window = cv2.CamShift(dst_red, track_window, term_crit)
pts_r = cv2.boxPoints(ret)
pts_r = np.int0(pts_r)
img3 = cv2.polylines(frame, [pts_r], True, (145, 80, 175), 2)
if pts_r[0][1] > 300:
#print("baixo")
pyautogui.scroll(-15)
elif pts_r[0][1] < 100:
#print("alto")
pyautogui.scroll(15)
#else:
#print("medio")
coloredMask = cv2.bitwise_and(frame, frame, mask = mask_red)
stacked = np.hstack((img3, coloredMask))
cv2.imshow("Tracking", cv2.resize(stacked, None, fx = 0.8, fy = 0.8))
key = cv2.waitKey(30) & 0xff
time = (time + 1) % 10
if key == 27:
break
cap.release()
cv2.destroyAllWindows()