-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_api.py
115 lines (90 loc) · 2.95 KB
/
test_api.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
from time import sleep
import docscanner
import numpy as np
import cv2
import time
# set license
docscanner.initLicense(
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
scanner = docscanner.createInstance()
ret = scanner.setParameters(docscanner.Templates.binary)
print(ret)
def showNormalizedImage(name, normalized_image):
mat = docscanner.convertNormalizedImage2Mat(normalized_image)
cv2.imshow(name, mat)
return mat
# detectFile()
def test_detectFile():
print('')
print('Test detectFile()')
results = scanner.detectFile("images/1.png")
assert len(results) > 0
image = cv2.imread("images/1.png")
for result in results:
x1 = result.x1
y1 = result.y1
x2 = result.x2
y2 = result.y2
x3 = result.x3
y3 = result.y3
x4 = result.x4
y4 = result.y4
print(x1, y1, x2, y2, x3, y3, x4, y4)
normalized_image = scanner.normalizeFile(
"images/1.png", x1, y1, x2, y2, x3, y3, x4, y4)
normalized_image.recycle()
# assert len(results) > 0
# showNormalizedImage("Normalized Image", normalized_image)
# cv2.drawContours(image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
# cv2.imshow('Document Image', image)
# cv2.waitKey(0)
# detectMat()
def test_detectMat():
print('')
print('Test detectMat()')
image = cv2.imread("images/1.png")
results = scanner.detectMat(image)
assert len(results) > 0
for result in results:
x1 = result.x1
y1 = result.y1
x2 = result.x2
y2 = result.y2
x3 = result.x3
y3 = result.y3
x4 = result.x4
y4 = result.y4
normalized_image = scanner.normalizeBuffer(
image, x1, y1, x2, y2, x3, y3, x4, y4)
# showNormalizedImage("Normalized Image", normalized_image)
normalized_image.recycle()
cv2.drawContours(
image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
# cv2.imshow('Document Image', image)
# cv2.waitKey(0)
# detectMatAsync()
def test_detectMatAsync():
print('')
print('Test detectMatAsync()')
def callback(results):
assert len(results) > 0
for result in results:
x1 = result.x1
y1 = result.y1
x2 = result.x2
y2 = result.y2
x3 = result.x3
y3 = result.y3
x4 = result.x4
y4 = result.y4
print(x1, y1, x2, y2, x3, y3, x4, y4)
cv2.drawContours(
image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
# cv2.imshow('Document Image', image)
# cv2.waitKey(0)
image = cv2.imread("images/1.png")
scanner.addAsyncListener(callback)
scanner.detectMatAsync(image)
test_detectFile()
test_detectMatAsync()
sleep(3)