-
-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathmouse_prediction.py
executable file
·104 lines (81 loc) · 2.74 KB
/
mouse_prediction.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
#!/usr/bin/env python
import numpy
import numpy as np
import sys
import matplotlib.pyplot as plt
import pyscreenshot
from text_recognizer import predict_tensor
try:
import Tkinter as tkinter
except Exception as ex:
import tkinter
if sys.platform == 'Windows':
import win32api # GetCursorPos
app = tkinter.Tk() # must be declared before Mat
plt.matshow([[1, 0], [0, 1]], fignum=1)
plt.draw()
# if mac
# system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
i = 0
def get_mouse_position():
if sys.platform == 'Windows':
x, y = win32api.GetCursorPos()
else:
x, y = app.winfo_pointerxy()
return x, y
if __name__ == "__main__":
while 1:
x, y = get_mouse_position()
# im = ImageGrab.grab() fullscreen
# image = ImageGrab.grab([x - 60, y - 20, x + 40, y + 20])
# image = ImageGrab.grab([x - 14, y - 14, x + 14, y + 14])
# image = ImageGrab.grab([x - 10, y - 10, x + 10, y + 10])
w = 512
h = 64
# image = pyscreenshot.grab([x - w/2, y - h / 2, x + w / 2, y + h / 2])
# image = pyscreenshot.grab([x - 10, y - 10, x + w - 10, y + h - 10]) # pointer
image = pyscreenshot.grab([x - 10, y , x + w - 10, y + h] ) # cursor
# image = pyscreenshot.grab([x, y, x + w, y + h])
mat = np.array(image) / 255.0 # RGBA: h*w*4
lines=numpy.average(mat, axis=1)
# todo make model robust to extra text
argmax = numpy.argmax(lines) # most white
argmin = numpy.argmin(lines) # most black
# if(argmax<argmin):
# mat[:,:argmax,:]=1. # fill white above
# if(argmin<argmax):
# mat[:,argmax:,:]=1. # fill white below
# todo: what if invert image!?
tensor = mat
print(tensor.shape)
# tensor=cv2.resize(tensor,(64,512))
if len(tensor.shape) == 2:
tensor = tensor.transpose((1, 0))
tensor = tensor[np.newaxis, :, :, np.newaxis]
elif len(tensor.shape) == 3:
mat = numpy.average(tensor, axis=2) # R+G+B
tensor = tensor.transpose((2, 1, 0)) # 4*w*h
tensor = tensor[:, :, :, np.newaxis]
# mat = 1 - 2 * mat / 255. # norm [-1,1] !
# mat = 1 - mat / 255. # norm [0,1]! black=1
# mat = mat / 255. # norm [0,1]! black=0 (default)
"""
TEST Text 01234 Hello <- point your mouse here
"""
plt.matshow(mat, fignum=1)
# plt.imshow(image)
histogram = numpy.histogram(mat, bins=10, range=None, normed=False, weights=None, density=None)
print(argmax)
words = predict_tensor(tensor)
if len(words) > 0:
best = words[0]
else:
best = "???"
print("interpreted as: %s" % (best))
plt.title("predicted: " + best)
plt.draw()
plt.pause(0.01)
del image
del mat
# k = cv2.waitKey(0) & 0xFF # 0xFF To get the lowest byte.
# if k == 27: exit(0)