Skip to content

Commit 9f40c66

Browse files
committed
Update
1 parent e5723d0 commit 9f40c66

File tree

7 files changed

+270
-196
lines changed

7 files changed

+270
-196
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $ scandocument -c 1 -l <license-key>
5353

5454
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
5555
showNormalizedImage("Normalized Image", normalized_image)
56-
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
56+
cv2.drawContours(image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
5757

5858
cv2.imshow('Document Image', image)
5959
cv2.waitKey(0)
@@ -172,7 +172,7 @@ $ scandocument -c 1 -l <license-key>
172172
x4 = result.x4
173173
y4 = result.y4
174174

175-
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
175+
cv2.drawContours(image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
176176

177177
cv2.putText(image, 'Press "n" to normalize image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
178178
cv2.putText(image, 'Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)

docscanner/scripts.py

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
g_normalized_images = []
1010
index = 0
1111

12+
1213
def callback(results):
1314
global g_results
1415
g_results = results
1516

17+
1618
def showNormalizedImage(name, normalized_image):
1719
mat = docscanner.convertNormalizedImage2Mat(normalized_image)
1820
cv2.imshow(name, mat)
1921
return mat
2022

23+
2124
def process_file(filename, scanner):
2225
image = cv2.imread(filename)
2326
results = scanner.detectMat(image)
@@ -31,36 +34,40 @@ def process_file(filename, scanner):
3134
y3 = result.y3
3235
x4 = result.x4
3336
y4 = result.y4
34-
35-
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
37+
38+
normalized_image = scanner.normalizeBuffer(
39+
image, x1, y1, x2, y2, x3, y3, x4, y4)
3640
showNormalizedImage("Normalized Image", normalized_image)
37-
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
38-
39-
cv2.putText(image, 'Press "ESC" to exit', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
41+
cv2.drawContours(
42+
image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
43+
44+
cv2.putText(image, 'Press "ESC" to exit', (10, 30),
45+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
4046
cv2.imshow('Document Image', image)
4147
cv2.waitKey(0)
42-
48+
4349
if normalized_image is not None:
4450
normalized_image.save(str(time.time()) + '.png')
4551
print('Image saved')
4652
normalized_image.recycle()
4753
else:
4854
print('No document found')
49-
55+
56+
5057
def process_video(scanner):
5158
global g_normalized_images, index
5259
scanner.addAsyncListener(callback)
53-
60+
5461
cap = cv2.VideoCapture(0)
5562
while True:
5663
ret, image = cap.read()
57-
64+
5865
ch = cv2.waitKey(1)
5966
if ch == 27:
6067
break
61-
elif ch == ord('n'): # normalize image
68+
elif ch == ord('n'): # normalize image
6269
if g_results != None:
63-
70+
6471
if len(g_results) > 0:
6572
for result in g_results:
6673
x1 = result.x1
@@ -71,31 +78,32 @@ def process_video(scanner):
7178
y3 = result.y3
7279
x4 = result.x4
7380
y4 = result.y4
74-
75-
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
76-
g_normalized_images.append((str(index), normalized_image))
81+
82+
normalized_image = scanner.normalizeBuffer(
83+
image, x1, y1, x2, y2, x3, y3, x4, y4)
84+
g_normalized_images.append(
85+
(str(index), normalized_image))
7786
showNormalizedImage(str(index), normalized_image)
7887
index += 1
7988
else:
8089
print('No document found')
81-
elif ch == ord('s'): # save image
90+
elif ch == ord('s'): # save image
8291
if len(g_normalized_images) > 0:
8392
for data in g_normalized_images:
8493
# cv2.imwrite('images/' + str(time.time()) + '.png', image)
8594
cv2.destroyWindow(data[0])
8695
data[1].save(str(time.time()) + '.png')
8796
print('Image saved')
8897
data[1].recycle()
89-
98+
9099
g_normalized_images = []
91100
index = 0
92101
else:
93102
print('No image to save')
94-
95-
103+
96104
if image is not None:
97105
scanner.detectMatAsync(image)
98-
106+
99107
if g_results != None:
100108
for result in g_results:
101109
x1 = result.x1
@@ -106,12 +114,16 @@ def process_video(scanner):
106114
y3 = result.y3
107115
x4 = result.x4
108116
y4 = result.y4
109-
110-
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
111-
112-
cv2.putText(image, '1. Press "n" to normalize image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
113-
cv2.putText(image, '2. Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
114-
cv2.putText(image, '3. Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
117+
118+
cv2.drawContours(
119+
image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
120+
121+
cv2.putText(image, '1. Press "n" to normalize image',
122+
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
123+
cv2.putText(image, '2. Press "s" to save image', (10, 60),
124+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
125+
cv2.putText(image, '3. Press "ESC" to exit', (10, 90),
126+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
115127
cv2.imshow('Document Scanner', image)
116128

117129
for data in g_normalized_images:
@@ -122,27 +134,31 @@ def scandocument():
122134
"""
123135
Command-line script for scanning documents from a given image or camera video stream.
124136
"""
125-
parser = argparse.ArgumentParser(description='Scan documents from an image file or camera')
137+
parser = argparse.ArgumentParser(
138+
description='Scan documents from an image file or camera')
126139
parser.add_argument('-f', '--file', help='Path to the image file')
127-
parser.add_argument('-c', '--camera', default=False, type=bool, help='Whether to show the image')
128-
parser.add_argument('-l', '--license', default='', type=str, help='Set a valid license key')
140+
parser.add_argument('-c', '--camera', default=False,
141+
type=bool, help='Whether to show the image')
142+
parser.add_argument('-l', '--license', default='',
143+
type=str, help='Set a valid license key')
129144
args = parser.parse_args()
130145
# print(args)
131146
try:
132147
filename = args.file
133148
license = args.license
134149
camera = args.camera
135-
150+
136151
if filename is None and camera is False:
137152
parser.print_help()
138153
return
139-
154+
140155
# set license
141-
if license == '':
142-
docscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
156+
if license == '':
157+
docscanner.initLicense(
158+
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
143159
else:
144160
docscanner.initLicense(license)
145-
161+
146162
# initialize mrz scanner
147163
scanner = docscanner.createInstance()
148164
ret = scanner.setParameters(docscanner.Templates.color)
@@ -151,7 +167,7 @@ def scandocument():
151167
process_file(filename, scanner)
152168
elif camera is True:
153169
process_video(scanner)
154-
170+
155171
except Exception as err:
156172
print(err)
157173
sys.exit(1)

examples/camera/test.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,32 @@
99
g_normalized_images = []
1010
index = 0
1111

12+
1213
def callback(results):
1314
global g_results
1415
g_results = results
1516

17+
1618
def showNormalizedImage(name, normalized_image):
1719
mat = docscanner.convertNormalizedImage2Mat(normalized_image)
1820
cv2.imshow(name, mat)
1921
return mat
20-
22+
23+
2124
def process_video(scanner):
2225
global g_normalized_images, index
2326
scanner.addAsyncListener(callback)
24-
27+
2528
cap = cv2.VideoCapture(0)
2629
while True:
2730
ret, image = cap.read()
28-
31+
2932
ch = cv2.waitKey(1)
3033
if ch == 27:
3134
break
32-
elif ch == ord('n'): # normalize image
35+
elif ch == ord('n'): # normalize image
3336
if g_results != None:
34-
37+
3538
if len(g_results) > 0:
3639
for result in g_results:
3740
x1 = result.x1
@@ -42,30 +45,32 @@ def process_video(scanner):
4245
y3 = result.y3
4346
x4 = result.x4
4447
y4 = result.y4
45-
46-
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
47-
g_normalized_images.append((str(index), normalized_image))
48+
49+
normalized_image = scanner.normalizeBuffer(
50+
image, x1, y1, x2, y2, x3, y3, x4, y4)
51+
g_normalized_images.append(
52+
(str(index), normalized_image))
4853
showNormalizedImage(str(index), normalized_image)
4954
index += 1
5055
else:
5156
print('No document found')
52-
elif ch == ord('s'): # save image
57+
elif ch == ord('s'): # save image
5358
if len(g_normalized_images) > 0:
5459
for data in g_normalized_images:
5560
# cv2.imwrite('images/' + str(time.time()) + '.png', image)
5661
cv2.destroyWindow(data[0])
5762
data[1].save(str(time.time()) + '.png')
5863
print('Image saved')
5964
data[1].recycle()
60-
65+
6166
g_normalized_images = []
6267
index = 0
6368
else:
6469
print('No image to save')
65-
70+
6671
if image is not None:
6772
scanner.detectMatAsync(image)
68-
73+
6974
if g_results != None:
7075
for result in g_results:
7176
x1 = result.x1
@@ -76,49 +81,58 @@ def process_video(scanner):
7681
y3 = result.y3
7782
x4 = result.x4
7883
y4 = result.y4
79-
80-
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
81-
82-
cv2.putText(image, '1. Press "n" to normalize image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
83-
cv2.putText(image, '2. Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
84-
cv2.putText(image, '3. Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
84+
85+
cv2.drawContours(
86+
image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
87+
88+
cv2.putText(image, '1. Press "n" to normalize image',
89+
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
90+
cv2.putText(image, '2. Press "s" to save image', (10, 60),
91+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
92+
cv2.putText(image, '3. Press "ESC" to exit', (10, 90),
93+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
8594
cv2.imshow('Document Scanner', image)
86-
95+
8796
for data in g_normalized_images:
8897
data[1].recycle()
8998

99+
90100
def scandocument():
91101
"""
92102
Command-line script for scanning documents from camera video stream.
93103
"""
94104
parser = argparse.ArgumentParser(description='Scan documents from camera')
95-
parser.add_argument('-c', '--camera', default=False, type=bool, help='Whether to show the image')
96-
parser.add_argument('-l', '--license', default='', type=str, help='Set a valid license key')
105+
parser.add_argument('-c', '--camera', default=False,
106+
type=bool, help='Whether to show the image')
107+
parser.add_argument('-l', '--license', default='',
108+
type=str, help='Set a valid license key')
97109
args = parser.parse_args()
98110
# print(args)
99111
try:
100112
license = args.license
101113
camera = args.camera
102-
114+
103115
if camera is False:
104116
parser.print_help()
105117
return
106-
118+
107119
# set license
108-
if license == '':
109-
docscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
120+
if license == '':
121+
docscanner.initLicense(
122+
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
110123
else:
111124
docscanner.initLicense(license)
112-
125+
113126
# initialize mrz scanner
114127
scanner = docscanner.createInstance()
115128
ret = scanner.setParameters(docscanner.Templates.color)
116129

117130
if camera is True:
118131
process_video(scanner)
119-
132+
120133
except Exception as err:
121134
print(err)
122135
sys.exit(1)
123136

124-
scandocument()
137+
138+
scandocument()

0 commit comments

Comments
 (0)