forked from Shakleen/Python-Document-Detector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage_extractor.py
More file actions
147 lines (120 loc) · 5.16 KB
/
page_extractor.py
File metadata and controls
147 lines (120 loc) · 5.16 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
import cv2
import numpy as np
from skimage.filters import threshold_otsu
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
from itertools import combinations
from collections import defaultdict
class PageExtractor:
def __init__(self, preprocessors, corner_detector, output_process = False):
assert isinstance(preprocessors, list), "List of processors expected"
self._preprocessors = preprocessors
self._corner_detector = corner_detector
self.output_process = output_process
def __call__(self, image_path):
# Step 1: Read image from file
self._image = cv2.imread(image_path)
# Step 2: Preprocess image
self._processed = self._image
for preprocessor in self._preprocessors:
self._processed = preprocessor(self._processed)
self._intersections = self._corner_detector(self._processed)
# Step 3: Deskew and extract page
return self._extract_page()
def _extract_page(self):
# obtain a consistent order of the points and unpack them
# individually
pts = np.array([
(x, y)
for intersection in self._intersections
for x, y in intersection
])
rect = self._order_points(pts)
(tl, tr, br, bl) = rect
# compute the width of the new image, which will be the
# maximum distance between bottom-right and bottom-left
# x-coordiates or the top-right and top-left x-coordinates
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
# compute the height of the new image, which will be the
# maximum distance between the top-right and bottom-right
# y-coordinates or the top-left and bottom-left y-coordinates
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# now that we have the dimensions of the new image, construct
# the set of destination points to obtain a "birds eye view",
# (i.e. top-down view) of the image, again specifying points
# in the top-left, top-right, bottom-right, and bottom-left
# order
dst = np.array([
[0, 0], # Top left point
[maxWidth - 1, 0], # Top right point
[maxWidth - 1, maxHeight - 1], # Bottom right point
[0, maxHeight - 1]], # Bottom left point
dtype = "float32" # Date type
)
# compute the perspective transform matrix and then apply it
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(self._processed, M, (maxWidth, maxHeight))
if self.output_process: cv2.imwrite('output/deskewed.jpg', warped)
# return the warped image
return warped
def _order_points(self, pts):
"""
Function for getting the bounding box points in the correct
order
Params
pts The points in the bounding box. Usually (x, y) coordinates
Returns
rect The ordered set of points
"""
# initialzie a list of coordinates that will be ordered such that
# 1st point -> Top left
# 2nd point -> Top right
# 3rd point -> Bottom right
# 4th point -> Bottom left
rect = np.zeros((4, 2), dtype = "float32")
# the top-left point will have the smallest sum, whereas
# the bottom-right point will have the largest sum
s = pts.sum(axis = 1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# now, compute the difference between the points, the
# top-right point will have the smallest difference,
# whereas the bottom-left will have the largest difference
diff = np.diff(pts, axis = 1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# return the ordered coordinates
return rect
if __name__ == "__main__":
import argparse
from hough_line_corner_detector import HoughLineCornerDetector
from processors import Resizer, OtsuThresholder, FastDenoiser
parser = argparse.ArgumentParser(description="Python script to detect and extract documents.")
parser.add_argument(
'-i',
'--input-image',
help = "Image containing the document",
required = True,
dest = 'input_image'
)
page_extractor = PageExtractor(
preprocessors = [
Resizer(height = 1280, output_process = True),
FastDenoiser(strength = 9, output_process = True),
OtsuThresholder(output_process = True)
],
corner_detector = HoughLineCornerDetector(
rho_acc = 1,
theta_acc = 180,
thresh = 100,
output_process = True
)
)
args = parser.parse_args()
extracted = page_extractor(args.input_image)
cv2.imshow("Extracted page", extracted)
cv2.waitKey(0)