-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_parse.py
72 lines (56 loc) · 1.99 KB
/
image_parse.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
import collections
import numpy
import pytesseract
import scipy.ndimage
import util
class Box(collections.namedtuple('Box', 'left top right bottom')):
@classmethod
def from_slice(cls, slice_):
ys, xs = slice_
return cls(xs.start, ys.start, xs.stop, ys.stop)
@property
def width(self):
return self.right - self.left
@property
def height(self):
return self.bottom - self.top
@property
def center(self):
return self.left + self.width // 2, self.top + self.height // 2
@property
def slice(self):
return slice(self.top, self.bottom), slice(self.left, self.right)
@property
def text_box(self):
# we chop off the corners of the hex to avoid getting the OCR confused about
# the shape of the hex vs numbers.
fourth_width = self.width // 4
fourth_height = self.height // 4
return Box(
left=self.left + fourth_width,
top=self.top + fourth_height,
right=self.right - fourth_width,
bottom=self.bottom - fourth_height,
)
@util.timeit("Labeling.")
def label(im):
array = numpy.array(im)
# Do not group white pixels.
mask = (array[:, :, 0] < 150) | (array[:, :, 1] < 150) | (array[:, :, 2] < 150)
label_array, numobjects = scipy.ndimage.label(mask)
objects = map(Box.from_slice, scipy.ndimage.find_objects(label_array))
return array, label_array, objects
def debug_labels(label_array):
import matplotlib.pyplot as plt
plt.imshow(label_array)
plt.show()
def get_text_from_image(im):
"""
Given an image, return the text from it.
See https://github.com/tesseract-ocr/tesseract/wiki/Command-Line-Usage
for options + explanations.
"""
# TODO: read from a numpy array instead of a PIL image.
im = im.convert('L') # convert to grayscale for better readability
config = '-psm 8 -c tessedit_char_whitelist=0123456789-?{}'
return pytesseract.image_to_string(im, config=config)