-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse.py
45 lines (38 loc) · 1.2 KB
/
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
import sys
from skimage import io, transform, feature, img_as_ubyte
import numpy as np
def preprocess(filename):
image = img_as_ubyte(io.imread(filename, as_grey = True))
if image.shape[0] != 768:
print(image.shape)
print("WARN: Resizing image to old iPad Size. TODO> Move forward to retina images!")
return img_as_ubyte(transform.resize(image, (768, 1024)))
return image
def yield_slits(image):
ff = feature.canny(image)
pi = 0
for i in range(len(ff)):
cnt = np.count_nonzero(ff[i])
if cnt > 800:
diff = i - pi
if diff > 50 and diff < 60:
slit = image[i-52:i]
yield slit
pi = i
def parse(filename):
image = preprocess(filename)
return yield_slits(image)
def check_and_parse(filename):
"""
Check whether enemy or not, and returns slit.
filename : file object or url, which can be read from imread
"""
image = preprocess(filename)
isEnemy = (image[55][600] < 150)
for it in yield_slits(image):
yield (isEnemy, it)
if __name__ == "__main__":
io.use_plugin('pil')
check_and_parse("9.png")
for it in parse("9.png"):
print(it)