22import docscanner
33import sys
44import 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
50101def 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 )
0 commit comments