-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMouse.py
More file actions
executable file
·47 lines (41 loc) · 1.53 KB
/
Mouse.py
File metadata and controls
executable file
·47 lines (41 loc) · 1.53 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
import cv2
import numpy as np
import win32api
global cam
cam = cv2.VideoCapture(0)
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
def main():
global cam
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
while True:
ret, frame = cam.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_hsv = np.array([0, 130, 147])
higher_hsv = np.array([22, 255, 215])
mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
mask = cv2.erode(mask, None, iterations=5)
mask = cv2.dilate(mask, None, iterations=5)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnt=cnts[0]
if len(cnts)>0:
x,y,w,h = cv2.boundingRect(cnt)
xx,yy=(2*x+w)/2 , (2*y+h)/2
if xx>0 and xx<1366 and yy>0 and yy<768:
win32api.SetCursorPos((1366-(xx*3),yy*3))
(cnts) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
cv2.imshow("Mouse", frame)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
main()