-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
236 lines (187 loc) · 7.3 KB
/
run.py
File metadata and controls
236 lines (187 loc) · 7.3 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import argparse
import cv2
import sys
import numpy as np
from google.cloud import vision
import io
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="./config.json"
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required = True, help = "Path to the input image")
ap.add_argument("-o", "--output", required = True, help = "Path to the output image")
ap.add_argument("-d", "--debug", required = False, help = "Debug status")
args = vars(ap.parse_args())
DEBUG = args["debug"] == 'True'
cv_version = cv2.__version__
rectangle_epsilon = 0.5
position_epsilon = 0.25
canny_threshold = 100
stdW = 25; stdH = 25
padding = 25
def distance(v1, v2):
return np.sqrt(np.sum((v1 - v2) ** 2))
# Find contours
def findContours(image):
# Convert image to GrayScale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Blur image
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# using the Canny edge detector
edge = cv2.Canny(blurred, canny_threshold, canny_threshold * 2)
# apply a dilation
dilated = cv2.dilate(edge, None, iterations=1)
if DEBUG:
pass
# cv2.imshow("Blurred", blurred)
# cv2.imshow("Dilation", dilated)
# cv2.imshow("Canny edge detector", edge)
# Find contours
if (cv_version[0] == '4'):
contours, hierarchy = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
else:
_, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
return [contours, hierarchy]
def getTextBoudingBox(path):
"""Get bounding box of all text in image"""
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
if (len(texts) > 0):
minLenTopLeft = 100000
maxLenBottomRight = 0
return [(vertex.x, vertex.y) for vertex in texts[0].bounding_poly.vertices]
else:
return [(0,0), (0,0), (0,0), (0,0)]
if response.error.message:
return [(0,0), (0,0), (0,0), (0,0)]
def filterBoundingBox(contours):
boundingBoxes = [list(cv2.boundingRect(cnt)) for cnt in contours]
return list(filter(lambda box: (box[2] > stdW and box[3] > stdH), boundingBoxes))
# Find the Largest Rectangle
def findTheLargestRect(contours, imageW, imageH):
xMin = 0; yMin = 0; xMax = 0; yMax = 0; resW = 0; resH = 0
points = getTextBoudingBox(args['input'])
xA_ = points[0][0]; yA_ = points[0][1]; xC_ = points[2][0]; yC_ = points[2][1]
# print(points)
if (points == [(0,0), (0,0), (0,0), (0,0)]):
# Can't find text
return [0, 0, imageW, imageH]
else:
# Found text
xA = 0; yA = 0; xC = 0; yC = 0;
wMax = 0; hMax = 0; sMax = 0
# finding the largest rectangle
boundingBoxes = filterBoundingBox(contours)
# print(boundingBoxes)
for [x, y, w, h] in boundingBoxes:
if (w * h >= sMax): #and h/w >= rectangle_epsilon
sMax = w * h
wMax = w; hMax = h
xA = x; yA = y
xC = xA + wMax; yC = yA + hMax
# finding the union rectangle
xMin = xA if xA < xA_ else xA_
yMin = yA if yA < yA_ else yA_
xMax = xC if xC > xC_ else xC_
yMax = yC if yC > yC_ else yC_
resW = xMax - xMin
resH = yMax - yMin
# refine rectangle with rectangle_epsilon
if (resH / resW < rectangle_epsilon):
yMin = (yMin - int((resW/2 - resH)/2)) if (yMin - int((resW/2 - resH)/2) > 0) else yMin
resH = (resH + int((resW/2 - resH))) if (resH + int((resW/2 - resH)) < imageH) else imageH
# add padding around
xMin = (xMin - padding) if (xMin - padding > 0) else 0
yMin = (yMin - padding) if (yMin - padding > 0) else 0
resW = (resW + padding*2) if (resW + padding*2 < imageW) else imageW
resH = (resH + padding*2) if (resH + padding*2 < imageH) else imageH
return [xMin, yMin, resW, resH]
def findGreenRect(contours, imageW, imageH):
xMin = 0
yMin = 0
xMax = 0
yMax = 0
resW = 0
resH = 0
# Found text
xA = 0
yA = 0
xC = 0
yC = 0
wMax = 0
hMax = 0
sMax = 0
# finding the largest rectangle
boundingBoxes = filterBoundingBox(contours)
# print(boundingBoxes)
for [x, y, w, h] in boundingBoxes:
if (w * h >= sMax ): #and h/w >= rectangle_epsilon
sMax = w * h
wMax = w
hMax = h
xA = x
yA = y
xC = xA + wMax
yC = yA + hMax
return [xA, yA, wMax, hMax]
#================================================================================================
# MAIN PROGRAM
#================================================================================================
# Read image file
print('=======DEBUG = {}======='.format(DEBUG))
print("##########################################################")
print('process file {}'.format(args["input"]))
image = cv2.imread(args["input"])
if image is None:
sys.exit("File not found!")
contours, hierarchy = findContours(image)
points = getTextBoudingBox(args["input"])
[x, y, w, h] = findTheLargestRect(contours, image.shape[1], image.shape[0])
# =====Crop by the largest contours=====
cropImgage = image[y:y+h, x:x+w]
cv2.imwrite(args["output"], cropImgage)
print("##########################################################")
if DEBUG:
# =====Final rect=====
print('Final rect: {}'.format([x, y, w, h]))
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 5)
# =====Show original image and its size=====
# cv2.imshow("Original", image)
# print((image.shape[0], image.shape[1]))
# =====contours info=====
# print(len(contours))
# =====the largest contours params=====
# print([x, y, w, h])
# =====Draw contours=====
# cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.imshow('Final edge', image)
# =====Draw all contours=====
# drawing = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
# for i in range(len(contours)):
# if i == 0:
# # print(contours[i])
# cv2.drawContours(drawing, contours, i, (0,0,255), 2, cv2.LINE_8, hierarchy, 0)
# else:
# cv2.drawContours(drawing, contours, i, (0,255,0), 2, cv2.LINE_8, hierarchy, 0)
# cv2.imshow('all contours', drawing)
# =====Draw contours bounding boxes=====
# cv2.drawContours(image, contours, -1, (0,0,255), 3, cv2.LINE_8, hierarchy, 0)
bounding_boxes = filterBoundingBox(contours)
for bbox in bounding_boxes:
[x , y, w, h] = bbox
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 255), 2)
# cv2.imshow('bounding boxes', image)
# =====Green rect=====
[xx, yy, ww, hh] = findGreenRect(contours, image.shape[1], image.shape[0])
print('Green rect: {}'.format([xx, yy, ww, hh]))
cv2.rectangle(image, (xx, yy), (xx + ww, yy + hh), (0, 255, 0), 5)
# =====Draw bounding boxes by GG Vison=====
cv2.rectangle(image, points[0], points[2], (255, 0, 0), 2)
# cv2.imshow('GG Vison', image)
# =====Crop final image=====
# cv2.imshow('Crop image', cropImgage)
cv2.imwrite('./debug/' + args["input"], image)
cv2.waitKey(0)