-
Notifications
You must be signed in to change notification settings - Fork 20
/
scanner.py
81 lines (60 loc) · 2.09 KB
/
scanner.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
from transform import four_point_transform
from skimage.filters import threshold_local
import numpy as np
import argparse
import cv2
import imutils
from ocr import preprocess,ocr
def edgeDetection(image):
image = imutils.resize(image, height = 500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)
# cv2.imshow("Image", image)
# cv2.imshow("Edged", edged)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return edged
def findContour(edged):
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
# cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
# cv2.imshow("Outline", image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return screenCnt
def scan(screenCnt, image):
ratio = image.shape[0] / 500.0
warped = four_point_transform(image, screenCnt.reshape(4, 2) * ratio)
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
T = threshold_local(warped, 11, offset = 10, method = "gaussian")
warped = (warped > T).astype("uint8") * 255
# kernel = np.ones((1,5), np.uint8) # note this is a HORIZONTAL kernel
# kernel = np.array([(0,1,0),(1,1,1),(0,1,0)])
# e_im = cv2.dilate(warped, kernel, iterations=1)
# e_im = cv2.erode(e_im, kernel, iterations=2)
# cv2.imshow("Original", imutils.resize(orig, height = 650))
# cv2.imshow("Scanned", imutils.resize(warped, height = 650))
# cv2.imshow("Scanne", imutils.resize(e_im, height = 650))
# cv2.waitKey(0)
return warped
def main():
image = cv2.imread("pic2.jpg")
edged = edgeDetection(image)
screenCnt = findContour(edged)
scannedImage = scan(screenCnt,image)
### OCR
processedImg = preprocess(scannedImage)
ocr(processedImg)
cv2.imshow("Scanned", imutils.resize(scannedImage, height = 650))
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()