-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_api.py
More file actions
125 lines (98 loc) · 3.48 KB
/
test_api.py
File metadata and controls
125 lines (98 loc) · 3.48 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
from time import sleep
import docscanner
from docscanner import *
import numpy as np
import cv2
import time
print(docscanner.__version__)
# set license
docscanner.initLicense(
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
scanner = docscanner.createInstance()
def showNormalizedImage(name, mat):
cv2.imshow(name, mat)
return mat
# detectFile()
def test_detectFile():
print('')
print('Test detectFile()')
results = scanner.detect("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)
scanner.normalize(result, EnumImageColourMode.ICM_BINARY)
if result.normalized_image is not None:
cv2.imwrite(str(time.time()) + '.png', result.normalized_image)
# if result.normalized_image is not None:
# showNormalizedImage("Normalized Image", result.normalized_image)
# cv2.drawContours(
# image, [np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], dtype=np.int32)], 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.detect(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
print(x1, y1, x2, y2, x3, y3, x4, y4)
scanner.normalize(result, EnumImageColourMode.ICM_GRAYSCALE)
if result.normalized_image is not None:
cv2.imwrite(str(time.time()) + '.png', result.normalized_image)
# showNormalizedImage("Normalized Image", result.normalized_image)
# cv2.drawContours(
# image, [np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], dtype=np.int32)], 0, (0, 255, 0), 2)
# cv2.imshow('Document Image', image)
# cv2.waitKey(0)
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)
if result.normalized_image is not None:
cv2.imwrite(str(time.time()) + '.png', result.normalized_image)
# cv2.drawContours(
# image, [np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], dtype=np.int32)], 0, (0, 255, 0), 2)
# if result.normalized_image is not None:
# showNormalizedImage("Normalized Image", result.normalized_image)
# cv2.imshow('Document Image', image)
# cv2.waitKey(0)
image = cv2.imread("images/1.png")
scanner.addAsyncListener(callback)
for i in range(2):
print('detectMatAsync: {}'.format(i))
scanner.detectMatAsync(image)
sleep(1)
scanner.clearAsyncListener()
test_detectFile()
test_detectMat()
test_detectMatAsync()