Skip to content

Commit 94e4fc6

Browse files
author
Pannous
committed
1 parent 104b476 commit 94e4fc6

File tree

5 files changed

+32
-85
lines changed

5 files changed

+32
-85
lines changed

combine.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from PIL import Image # Image.open(test_image)
66
# import cv2 # cv2.imwrite(output_path, img)
77
# import skimage #skimage.io.imread
8+
# server='0.0.0.0'
9+
server='87.118.88.144' #dev03'
810

911
class Box(object):
1012
def __init__(self, **kwargs):
@@ -14,21 +16,14 @@ def __init__(self, **kwargs):
1416
image_file='test_image.png'
1517
test_file='test_out.png'
1618
image = Image.open(image_file)
17-
1819
with open(image_file, 'rb') as f:
19-
r = requests.post('http://0.0.0.0:8769/?json=1', files={'image': f})
20-
print(r.text)
21-
ocr=json.loads(r.text.replace(""",'"'))
22-
for line in ocr['text_lines']:
20+
r = requests.post('http://'+server+':8769/?json=1', files={'image': f})
21+
raw=r.text.replace(""",'"')
22+
print(raw)
23+
boxes=json.loads(raw)
24+
for line in boxes['text_lines']:
2325
print(line)
2426
b=Box(**line)
2527
print(b.x0)
2628
word=image.crop((b.x0-5, b.y0-5, b.x2+15, b.y2+15))
2729
word.save(open(test_file, 'wb'))
28-
29-
# h = httplib.HTTPConnection('http://0.0.0.0:8769')
30-
# headers={}
31-
# data=[]
32-
# h.request('POST', '/?json=1', data, headers)
33-
# r = h.getresponse()
34-
# result=r.read()

mouse_prediction.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#!/usr/bin/env python
2+
import numpy
3+
4+
import numpy as np
25
import sys
36

47
import matplotlib.pyplot as plt
5-
import numpy
6-
import numpy as np
7-
# import gtk
8-
# gtk.set_interactive(False)
98
import pyscreenshot
10-
import tensorflow as tf
119

1210
from text_recognizer import predict_tensor
1311

@@ -16,35 +14,27 @@
1614
except Exception as ex:
1715
import tkinter
1816

17+
if sys.platform == 'Windows':
18+
import win32api # GetCursorPos
19+
1920
app = tkinter.Tk() # must be declared before Mat
2021

21-
# import cv2
2222
plt.matshow([[1, 0], [0, 1]], fignum=1)
23-
# print(dir(plt))
24-
# help(plt)
25-
# ax.patch.set_facecolor('None') or ax.patch.set_visible(False).
2623
plt.draw()
2724

2825
# if mac
2926
# system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
3027

3128
i = 0
32-
width = 256
33-
height = 256
34-
3529

3630
def get_mouse_position():
3731
if sys.platform == 'Windows':
38-
import win32api
3932
x, y = win32api.GetCursorPos()
4033
else:
4134
x, y = app.winfo_pointerxy()
4235
return x, y
4336

4437

45-
get_mouse_position()
46-
session = tf.Session()
47-
4838
if __name__ == "__main__":
4939
while 1:
5040
x, y = get_mouse_position()
@@ -60,15 +50,19 @@ def get_mouse_position():
6050
# image = pyscreenshot.grab([x, y, x + w, y + h])
6151
mat = np.array(image) / 255.0 # RGBA: h*w*4
6252

63-
6453
lines=numpy.average(mat, axis=1)
54+
# todo make model robust to extra text
6555
argmax = numpy.argmax(lines) # most white
6656
argmin = numpy.argmin(lines) # most black
6757
if(argmax<argmin):
6858
mat[:,:argmax,:]=1. # fill white above
59+
if(argmin<argmax):
60+
mat[:,argmax:,:]=1. # fill white below
61+
# todo: what if invert image!?
6962

7063
tensor = mat
7164
print(tensor.shape)
65+
# tensor=cv2.resize(tensor,(64,512))
7266
if len(tensor.shape) == 2:
7367
tensor = tensor.transpose((1, 0))
7468
tensor = tensor[np.newaxis, :, :, np.newaxis]
@@ -77,27 +71,19 @@ def get_mouse_position():
7771
tensor = tensor.transpose((2, 1, 0)) # 4*w*h
7872
tensor = tensor[:, :, :, np.newaxis]
7973

80-
# tensor=cv2.resize(tensor,(64,512))
74+
# mat = 1 - 2 * mat / 255. # norm [-1,1] !
75+
# mat = 1 - mat / 255. # norm [0,1]! black=1
76+
# mat = mat / 255. # norm [0,1]! black=0 (default)
77+
8178
"""
8279
8380
TEST Text 01234 Hello <- point your mouse here
8481
8582
"""
86-
# help(image.show) Displays this image via preview. This method is mainly intended for debugging purposes
87-
# array = numpy.array(image.getdata()) # (1, 4000, 4)
88-
#
89-
# mat = array.reshape(image.height, image.width, -1)[:, :, 0]
90-
# if size> image.height:
91-
# mat=numpy.pad(mat, (0, size- image.height), 'constant', constant_values=255) # 1==white!
92-
93-
# mat = 1 - 2 * mat / 255. # norm [-1,1] !
94-
# mat = 1 - mat / 255. # norm [0,1]! black=1
95-
# mat = mat / 255. # norm [0,1]! black=0 (default)
9683

9784
plt.matshow(mat, fignum=1)
9885
# plt.imshow(image)
9986

100-
10187
histogram = numpy.histogram(mat, bins=10, range=None, normed=False, weights=None, density=None)
10288
print(argmax)
10389

text_recognizer.py

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,24 @@
1010
from keras.layers.convolutional import Conv2D, MaxPooling2D
1111
from keras.layers.merge import add, concatenate
1212
from keras.layers.recurrent import GRU
13-
from keras.models import Model
13+
from keras.models import Model, load_model
1414

1515
# weight_file = 'best_weights.h5'
16-
weight_file = 'current_weights.h5'
16+
# weight_file = 'current_weights.h5'
17+
weight_file = 'weights_ascii.h5'
1718

18-
# alphabet = u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '
19-
alphabet = u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜß0123456789!@#$%^&*()[]{}-_=+\\|"\'`;:/.,?><~ '
19+
20+
# chars = u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '
21+
chars = u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜß0123456789!@#$%^&*()[]{}-_=+\\|"\'`;:/.,?><~ '
2022

2123

2224
global model
2325
model=None
2426

2527
def load_model():
2628
global model
27-
# Model similar to image_ocr.py
28-
rnn_size = 1024
29-
dropout = 0.2
30-
pool_size = 2
31-
kernel_size = (3, 3)
32-
time_dense_size = 32
33-
conv_filters = 16
34-
img_h = 64
35-
img_w = 512
36-
37-
act = 'relu'
38-
input_data = Input(name='the_input', shape=(img_w, img_h, 1), dtype='float32')
39-
inner = Conv2D(conv_filters, kernel_size, padding='same', activation=act, name='conv1')(input_data)
40-
inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max1')(inner)
41-
inner = Conv2D(conv_filters, kernel_size, padding='same', activation=act, name='conv2')(inner)
42-
inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max2')(inner)
43-
44-
conv_to_rnn_dims = (img_w // (pool_size ** 2), (img_h // (pool_size ** 2)) * conv_filters)
45-
inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner)
46-
47-
inner = Dropout(rate=dropout, name='dropout_dense1a')(inner)
48-
inner = Dense(time_dense_size, activation=act, name='dense1')(inner)
49-
inner = Dropout(rate=dropout, name='dropout_dense1b')(inner)
50-
51-
# Two layers of bidirectional GRUs
52-
gru_1 = GRU(rnn_size, return_sequences=True, dropout=0.3, name='gru1')(inner)
53-
gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, name='gru1_b')(inner)
54-
gru1_merged = add([gru_1, gru_1b])
55-
gru_2 = GRU(rnn_size, return_sequences=True, dropout=0.3, name='gru2')(gru1_merged)
56-
gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, name='gru2_b')(gru1_merged)
57-
58-
dense2 = Dense(len(alphabet) + 1, name='dense2')
59-
inner = dense2(concatenate([gru_2, gru_2b]))
60-
y_pred = Activation('softmax', name='softmax')(inner)
61-
model = Model(inputs=input_data, outputs=y_pred)
62-
model.summary()
63-
64-
model.load_weights(weight_file, reshape=True, by_name=True)
29+
model = load_model(weight_file)
30+
# model.load_weights(weight_file, reshape=True, by_name=True)
6531

6632
def predict_tensor(images):
6733
if not model: load_model()
@@ -74,10 +40,10 @@ def decode_labels(labels):
7440
ret = []
7541
for c in labels:
7642
# ret += chr(c)
77-
if c == len(alphabet):
43+
if c == len(chars):
7844
ret.append("")
7945
else:
80-
ret.append(alphabet[c])
46+
ret.append(chars[c])
8147
return "".join(ret)
8248

8349

weights_ascii.h5

74.5 MB
Binary file not shown.

weights_ascii2.h5

74.5 MB
Binary file not shown.

0 commit comments

Comments
 (0)