9
9
g_normalized_images = []
10
10
index = 0
11
11
12
+
12
13
def callback (results ):
13
14
global g_results
14
15
g_results = results
15
16
17
+
16
18
def showNormalizedImage (name , normalized_image ):
17
19
mat = docscanner .convertNormalizedImage2Mat (normalized_image )
18
20
cv2 .imshow (name , mat )
19
21
return mat
20
22
23
+
21
24
def process_file (filename , scanner ):
22
25
image = cv2 .imread (filename )
23
26
results = scanner .detectMat (image )
@@ -31,36 +34,40 @@ def process_file(filename, scanner):
31
34
y3 = result .y3
32
35
x4 = result .x4
33
36
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 )
36
40
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 )
40
46
cv2 .imshow ('Document Image' , image )
41
47
cv2 .waitKey (0 )
42
-
48
+
43
49
if normalized_image is not None :
44
50
normalized_image .save (str (time .time ()) + '.png' )
45
51
print ('Image saved' )
46
52
normalized_image .recycle ()
47
53
else :
48
54
print ('No document found' )
49
-
55
+
56
+
50
57
def process_video (scanner ):
51
58
global g_normalized_images , index
52
59
scanner .addAsyncListener (callback )
53
-
60
+
54
61
cap = cv2 .VideoCapture (0 )
55
62
while True :
56
63
ret , image = cap .read ()
57
-
64
+
58
65
ch = cv2 .waitKey (1 )
59
66
if ch == 27 :
60
67
break
61
- elif ch == ord ('n' ): # normalize image
68
+ elif ch == ord ('n' ): # normalize image
62
69
if g_results != None :
63
-
70
+
64
71
if len (g_results ) > 0 :
65
72
for result in g_results :
66
73
x1 = result .x1
@@ -71,31 +78,32 @@ def process_video(scanner):
71
78
y3 = result .y3
72
79
x4 = result .x4
73
80
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 ))
77
86
showNormalizedImage (str (index ), normalized_image )
78
87
index += 1
79
88
else :
80
89
print ('No document found' )
81
- elif ch == ord ('s' ): # save image
90
+ elif ch == ord ('s' ): # save image
82
91
if len (g_normalized_images ) > 0 :
83
92
for data in g_normalized_images :
84
93
# cv2.imwrite('images/' + str(time.time()) + '.png', image)
85
94
cv2 .destroyWindow (data [0 ])
86
95
data [1 ].save (str (time .time ()) + '.png' )
87
96
print ('Image saved' )
88
97
data [1 ].recycle ()
89
-
98
+
90
99
g_normalized_images = []
91
100
index = 0
92
101
else :
93
102
print ('No image to save' )
94
-
95
-
103
+
96
104
if image is not None :
97
105
scanner .detectMatAsync (image )
98
-
106
+
99
107
if g_results != None :
100
108
for result in g_results :
101
109
x1 = result .x1
@@ -106,12 +114,16 @@ def process_video(scanner):
106
114
y3 = result .y3
107
115
x4 = result .x4
108
116
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 )
115
127
cv2 .imshow ('Document Scanner' , image )
116
128
117
129
for data in g_normalized_images :
@@ -122,27 +134,31 @@ def scandocument():
122
134
"""
123
135
Command-line script for scanning documents from a given image or camera video stream.
124
136
"""
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' )
126
139
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' )
129
144
args = parser .parse_args ()
130
145
# print(args)
131
146
try :
132
147
filename = args .file
133
148
license = args .license
134
149
camera = args .camera
135
-
150
+
136
151
if filename is None and camera is False :
137
152
parser .print_help ()
138
153
return
139
-
154
+
140
155
# set license
141
- if license == '' :
142
- docscanner .initLicense ("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==" )
156
+ if license == '' :
157
+ docscanner .initLicense (
158
+ "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==" )
143
159
else :
144
160
docscanner .initLicense (license )
145
-
161
+
146
162
# initialize mrz scanner
147
163
scanner = docscanner .createInstance ()
148
164
ret = scanner .setParameters (docscanner .Templates .color )
@@ -151,7 +167,7 @@ def scandocument():
151
167
process_file (filename , scanner )
152
168
elif camera is True :
153
169
process_video (scanner )
154
-
170
+
155
171
except Exception as err :
156
172
print (err )
157
173
sys .exit (1 )
0 commit comments