99g_normalized_images = []
1010index = 0
1111
12+
1213def callback (results ):
1314 global g_results
1415 g_results = results
1516
17+
1618def showNormalizedImage (name , normalized_image ):
1719 mat = docscanner .convertNormalizedImage2Mat (normalized_image )
1820 cv2 .imshow (name , mat )
1921 return mat
2022
23+
2124def 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+
5057def 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 )
0 commit comments