-
Notifications
You must be signed in to change notification settings - Fork 0
/
markup_server.py
125 lines (105 loc) · 4.22 KB
/
markup_server.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from __future__ import division
import argparse
import math
from SimpleCV import Image, Color, Features
from collections import namedtuple
import os
Rectangle = namedtuple('rectangle', ['x', 'y', 'w', 'h', 'r', 'g', 'b'])
WebRectangle = namedtuple('webrectangle', ['x', 'y', 'w', 'h', 'type','inside'])
Grid = namedtuple('grid', ['x','y', 'origin', 'end'])
Point = namedtuple('point', ['x','y'])
def find_color(x,y, image):
corner = image.crop(x-2, y-2, 4, 4)
return corner.meanColor()
def find_shapes(img):
markupImage = Image(img)
bwImage = markupImage.binarize()
blobs = bwImage.findBlobs()
rectangles = []
for b in blobs:
info = b.boundingBox()
x = info[0]
y = info[1]
w = info[2]
h = info[3]
c = find_color(x, y, markupImage)
markupImage.drawRectangle(x, y, w, h)
rectangles.append(Rectangle(*((x, y, w, h) + c)))
max_rectangle = max(rectangles, key=lambda rect: rect.w * rect.h)
#markupImage.drawRectangle(max_rectangle.x, max_rectangle.y, max_rectangle.w, max_rectangle.h, Color.ORANGE)
return rectangles, markupImage
def grid_transform(info):
max_rectangle = max(info[0], key=lambda rect: rect.w * rect.h)
info[0].remove(max_rectangle)
img = info[1]
origin = Point(max_rectangle.x, max_rectangle.y)
end = Point(max_rectangle.x + max_rectangle.w, max_rectangle.y + max_rectangle.h)
gridx = int(math.ceil(max_rectangle.w/14))
gridy = int(math.ceil(max_rectangle.h/9))
grid = Grid(gridx, gridy, origin, end)
#img = draw_grid(img, grid)
web_rects= classify_rect(info[0], grid, img)
return web_rects, img
def classify_rect(rects, grid, img):
web_rects = []
for rect in rects:
coords = transform_rect(rect, grid)
x = coords[0].x
y = coords[0].y
w = abs(coords[0].x - coords[1].x)
h = abs(coords[0].y - coords[1].y)
web_rects.append(WebRectangle(x,y,w,h,"text",[]))
for wrect in web_rects:
subshapes = find_subshapes(web_rects, wrect)
wrect = WebRectangle(wrect.x, wrect.y, wrect.w, wrect.h, "text", subshapes)
return web_rects
def find_subshapes(rectangles, rect):
subshapes = []
for r in rectangles:
if ((~(rect == r)) and (includes(r,rect))):
subshapes.append(r)
return subshapes
def includes(shape, rectangle):
if ((shape.x > rectangle.x) and (shape.y > rectangle.y) and (shape.w < rectangle.w) and (shape.h < rectangle.h)):
return True
else:
return False
def transform_rect(rect, grid):
'''Takes rectangle on paper and transforms it into a web rectangle with certain
properties'''
#_type = "text"
upper_corner = transform_point(Point(rect.x, rect.y), grid)
lower_corner = transform_point(Point(rect.x + rect.w, rect.y + rect.h), grid)
return upper_corner, lower_corner
#return WebRectangle(upper_corner.x, upper_corner.y, abs(upper_corner.x - lower_corner.x),
#abs(upper_corner.y - lower_corner.y), _type , [])
def transform_point(point, grid):
up_x = int(math.ceil((point.x + grid.origin.x)/grid.x))
down_x = int((point.x + grid.origin.x)/grid.x)
if abs(point.x - up_x) >= abs(point.x - down_x):
new_x = down_x
else:
new_x = up_x
up_y = int(math.ceil((point.y + grid.origin.y)/grid.y))
down_y = int((point.y + grid.origin.y)/grid.y)
if abs(point.y - up_y) >= abs(point.y - down_y):
new_y = down_y
else:
new_y = up_y
return Point(new_x, new_y)
def analyze(image):
return grid_transform(find_shapes(image))
def draw_grid(image, grid):
for i, x in enumerate(range(grid.origin.x, grid.end.x, grid.x)):
for j, y in enumerate(range(grid.origin.y, grid.end.y, grid.y)):
image.drawRectangle(x, y, grid.x, grid.y, Color.GREEN)
return image
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Please enter an image to be analyzed")
parser.add_argument('image', type=str,
help='path to image to be analyzed')
args = parser.parse_args()
shapes = find_shapes(os.path.abspath(args.image))
image = grid_transform(shapes)
image[1].save("squares.jpg")
print(analyze(args.image))