Skip to content

Commit 1e63fbb

Browse files
committed
Update examples
1 parent 2353110 commit 1e63fbb

File tree

6 files changed

+297
-81
lines changed

6 files changed

+297
-81
lines changed

docscanner/scripts.py

Lines changed: 104 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,120 @@
22
import docscanner
33
import sys
44
import numpy as np
5+
import cv2
6+
import time
57

6-
from mrz.checker.td1 import TD1CodeChecker
7-
from mrz.checker.td2 import TD2CodeChecker
8-
from mrz.checker.td3 import TD3CodeChecker
9-
from mrz.checker.mrva import MRVACodeChecker
10-
from mrz.checker.mrvb import MRVBCodeChecker
8+
g_results = None
9+
g_normalized_images = []
1110

12-
def check(lines):
13-
try:
14-
td1_check = TD1CodeChecker(lines)
15-
if bool(td1_check):
16-
return "TD1", td1_check.fields()
17-
except Exception as err:
18-
pass
11+
def callback(results):
12+
global g_results
13+
g_results = results
14+
15+
def showNormalizedImage(name, normalized_image):
16+
mat = docscanner.convertNormalizedImage2Mat(normalized_image)
17+
cv2.imshow(name, mat)
18+
return mat
19+
20+
def process_file(filename, scanner):
21+
image = cv2.imread(filename)
22+
results = scanner.decodeMat(image)
23+
for result in results:
24+
x1 = result.x1
25+
y1 = result.y1
26+
x2 = result.x2
27+
y2 = result.y2
28+
x3 = result.x3
29+
y3 = result.y3
30+
x4 = result.x4
31+
y4 = result.y4
32+
33+
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
34+
showNormalizedImage("Normalized Image", normalized_image)
35+
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
1936

20-
try:
21-
td2_check = TD2CodeChecker(lines)
22-
if bool(td2_check):
23-
return "TD2", td2_check.fields()
24-
except Exception as err:
25-
pass
37+
cv2.imshow('Document Image', image)
38+
cv2.waitKey(0)
2639

27-
try:
28-
td3_check = TD3CodeChecker(lines)
29-
if bool(td3_check):
30-
return "TD3", td3_check.fields()
31-
except Exception as err:
32-
pass
40+
normalized_image.save(str(time.time()) + '.png')
41+
print('Image saved')
3342

34-
try:
35-
mrva_check = MRVACodeChecker(lines)
36-
if bool(mrva_check):
37-
return "MRVA", mrva_check.fields()
38-
except Exception as err:
39-
pass
43+
def process_video(scanner):
44+
scanner.addAsyncListener(callback)
4045

41-
try:
42-
mrvb_check = MRVBCodeChecker(lines)
43-
if bool(mrvb_check):
44-
return "MRVB", mrvb_check.fields()
45-
except Exception as err:
46-
pass
47-
48-
return 'No valid MRZ information found'
46+
cap = cv2.VideoCapture(0)
47+
while True:
48+
ret, image = cap.read()
49+
50+
ch = cv2.waitKey(1)
51+
if ch == 27:
52+
break
53+
elif ch == ord('n'): # normalize image
54+
if g_results != None:
55+
g_normalized_images = []
56+
index = 0
57+
for result in g_results:
58+
x1 = result.x1
59+
y1 = result.y1
60+
x2 = result.x2
61+
y2 = result.y2
62+
x3 = result.x3
63+
y3 = result.y3
64+
x4 = result.x4
65+
y4 = result.y4
66+
67+
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
68+
g_normalized_images.append((str(index), normalized_image))
69+
mat = showNormalizedImage(str(index), normalized_image)
70+
index += 1
71+
elif ch == ord('s'): # save image
72+
for data in g_normalized_images:
73+
# cv2.imwrite('images/' + str(time.time()) + '.png', image)
74+
cv2.destroyWindow(data[0])
75+
data[1].save(str(time.time()) + '.png')
76+
print('Image saved')
77+
78+
g_normalized_images = []
79+
80+
if image is not None:
81+
scanner.decodeMatAsync(image)
82+
83+
if g_results != None:
84+
for result in g_results:
85+
x1 = result.x1
86+
y1 = result.y1
87+
x2 = result.x2
88+
y2 = result.y2
89+
x3 = result.x3
90+
y3 = result.y3
91+
x4 = result.x4
92+
y4 = result.y4
93+
94+
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
95+
96+
cv2.putText(image, 'Press "n" to normalize image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
97+
cv2.putText(image, 'Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
98+
cv2.putText(image, 'Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
99+
cv2.imshow('Document Scanner', image)
49100

50101
def scandocument():
51102
"""
52-
Command-line script for recognize MRZ info from a given image
103+
Command-line script for scanning documents from a given image or camera video stream.
53104
"""
54-
parser = argparse.ArgumentParser(description='Scan MRZ info from a given image')
55-
parser.add_argument('filename')
56-
parser.add_argument('-u', '--ui', default=False, type=bool, help='Whether to show the image')
105+
parser = argparse.ArgumentParser(description='Scan documents from an image file or camera')
106+
parser.add_argument('-f', '--file', help='Path to the image file')
107+
parser.add_argument('-c', '--camera', default=False, type=bool, help='Whether to show the image')
57108
parser.add_argument('-l', '--license', default='', type=str, help='Set a valid license key')
58109
args = parser.parse_args()
59110
# print(args)
60111
try:
61-
filename = args.filename
112+
filename = args.file
62113
license = args.license
63-
ui = args.ui
114+
camera = args.camera
115+
116+
if filename is None and camera is False:
117+
parser.print_help()
118+
return
64119

65120
# set license
66121
if license == '':
@@ -70,42 +125,12 @@ def scandocument():
70125

71126
# initialize mrz scanner
72127
scanner = docscanner.createInstance()
128+
ret = scanner.setParameters(docscanner.Templates.color)
73129

74-
# load MRZ model
75-
scanner.loadModel(docscanner.get_model_path())
76-
77-
s = ""
78-
if ui:
79-
import cv2
80-
image = cv2.imread(filename)
81-
results = scanner.decodeMat(image)
82-
for result in results:
83-
print(result.text)
84-
s += result.text + '\n'
85-
x1 = result.x1
86-
y1 = result.y1
87-
x2 = result.x2
88-
y2 = result.y2
89-
x3 = result.x3
90-
y3 = result.y3
91-
x4 = result.x4
92-
y4 = result.y4
93-
94-
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
95-
# cv2.putText(image, result.text, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
96-
97-
print(check(s[:-1]))
98-
99-
cv2.imshow("image", image)
100-
cv2.waitKey(0)
101-
else:
102-
results = scanner.decodeFile(filename)
103-
for result in results:
104-
print(result.text)
105-
s += result.text + '\n'
106-
107-
print(check(s[:-1]))
108-
130+
if filename is not None:
131+
process_file(filename, scanner)
132+
elif camera is True:
133+
process_video(scanner)
109134

110135
except Exception as err:
111136
print(err)

examples/camera/test.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import argparse
2+
import docscanner
3+
import sys
4+
import numpy as np
5+
import cv2
6+
import time
7+
8+
g_results = None
9+
g_normalized_images = []
10+
11+
def callback(results):
12+
global g_results
13+
g_results = results
14+
15+
def showNormalizedImage(name, normalized_image):
16+
mat = docscanner.convertNormalizedImage2Mat(normalized_image)
17+
cv2.imshow(name, mat)
18+
return mat
19+
20+
def process_video(scanner):
21+
scanner.addAsyncListener(callback)
22+
23+
cap = cv2.VideoCapture(0)
24+
while True:
25+
ret, image = cap.read()
26+
27+
ch = cv2.waitKey(1)
28+
if ch == 27:
29+
break
30+
elif ch == ord('n'): # normalize image
31+
if g_results != None:
32+
g_normalized_images = []
33+
index = 0
34+
for result in g_results:
35+
x1 = result.x1
36+
y1 = result.y1
37+
x2 = result.x2
38+
y2 = result.y2
39+
x3 = result.x3
40+
y3 = result.y3
41+
x4 = result.x4
42+
y4 = result.y4
43+
44+
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
45+
g_normalized_images.append((str(index), normalized_image))
46+
mat = showNormalizedImage(str(index), normalized_image)
47+
index += 1
48+
elif ch == ord('s'): # save image
49+
for data in g_normalized_images:
50+
# cv2.imwrite('images/' + str(time.time()) + '.png', image)
51+
cv2.destroyWindow(data[0])
52+
data[1].save(str(time.time()) + '.png')
53+
print('Image saved')
54+
55+
g_normalized_images = []
56+
57+
if image is not None:
58+
scanner.decodeMatAsync(image)
59+
60+
if g_results != None:
61+
for result in g_results:
62+
x1 = result.x1
63+
y1 = result.y1
64+
x2 = result.x2
65+
y2 = result.y2
66+
x3 = result.x3
67+
y3 = result.y3
68+
x4 = result.x4
69+
y4 = result.y4
70+
71+
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
72+
73+
cv2.putText(image, 'Press "n" to normalize image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
74+
cv2.putText(image, 'Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
75+
cv2.putText(image, 'Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
76+
cv2.imshow('Document Scanner', image)
77+
78+
def scandocument():
79+
"""
80+
Command-line script for scanning documents from camera video stream.
81+
"""
82+
parser = argparse.ArgumentParser(description='Scan documents from camera')
83+
parser.add_argument('-c', '--camera', default=False, type=bool, help='Whether to show the image')
84+
parser.add_argument('-l', '--license', default='', type=str, help='Set a valid license key')
85+
args = parser.parse_args()
86+
# print(args)
87+
try:
88+
license = args.license
89+
camera = args.camera
90+
91+
if camera is False:
92+
parser.print_help()
93+
return
94+
95+
# set license
96+
if license == '':
97+
docscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
98+
else:
99+
docscanner.initLicense(license)
100+
101+
# initialize mrz scanner
102+
scanner = docscanner.createInstance()
103+
ret = scanner.setParameters(docscanner.Templates.color)
104+
105+
if camera is True:
106+
process_video(scanner)
107+
108+
except Exception as err:
109+
print(err)
110+
sys.exit(1)
111+
112+
scandocument()

0 commit comments

Comments
 (0)