77
88g_results = None
99g_normalized_images = []
10+ index = 0
1011
1112def callback (results ):
1213 global g_results
@@ -20,6 +21,7 @@ def showNormalizedImage(name, normalized_image):
2021def process_file (filename , scanner ):
2122 image = cv2 .imread (filename )
2223 results = scanner .detectMat (image )
24+ normalized_image = None
2325 for result in results :
2426 x1 = result .x1
2527 y1 = result .y1
@@ -34,14 +36,19 @@ def process_file(filename, scanner):
3436 showNormalizedImage ("Normalized Image" , normalized_image )
3537 cv2 .drawContours (image , [np .int0 ([(x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y4 )])], 0 , (0 , 255 , 0 ), 2 )
3638
39+ cv2 .putText (image , 'Press "ESC" to exit' , (10 , 30 ), cv2 .FONT_HERSHEY_SIMPLEX , 0.8 , (0 , 0 , 255 ), 2 )
3740 cv2 .imshow ('Document Image' , image )
3841 cv2 .waitKey (0 )
3942
40- normalized_image .save (str (time .time ()) + '.png' )
41- print ('Image saved' )
42- normalized_image .recycle ()
43+ if normalized_image is not None :
44+ normalized_image .save (str (time .time ()) + '.png' )
45+ print ('Image saved' )
46+ normalized_image .recycle ()
47+ else :
48+ print ('No document found' )
4349
4450def process_video (scanner ):
51+ global g_normalized_images , index
4552 scanner .addAsyncListener (callback )
4653
4754 cap = cv2 .VideoCapture (0 )
@@ -53,29 +60,38 @@ def process_video(scanner):
5360 break
5461 elif ch == ord ('n' ): # normalize image
5562 if g_results != None :
63+
64+ if len (g_results ) > 0 :
65+ for result in g_results :
66+ x1 = result .x1
67+ y1 = result .y1
68+ x2 = result .x2
69+ y2 = result .y2
70+ x3 = result .x3
71+ y3 = result .y3
72+ x4 = result .x4
73+ 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 ))
77+ showNormalizedImage (str (index ), normalized_image )
78+ index += 1
79+ else :
80+ print ('No document found' )
81+ elif ch == ord ('s' ): # save image
82+ if len (g_normalized_images ) > 0 :
83+ for data in g_normalized_images :
84+ # cv2.imwrite('images/' + str(time.time()) + '.png', image)
85+ cv2 .destroyWindow (data [0 ])
86+ data [1 ].save (str (time .time ()) + '.png' )
87+ print ('Image saved' )
88+ data [1 ].recycle ()
89+
5690 g_normalized_images = []
5791 index = 0
58- for result in g_results :
59- x1 = result .x1
60- y1 = result .y1
61- x2 = result .x2
62- y2 = result .y2
63- x3 = result .x3
64- y3 = result .y3
65- x4 = result .x4
66- y4 = result .y4
67-
68- normalized_image = scanner .normalizeBuffer (image , x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 )
69- g_normalized_images .append ((str (index ), normalized_image ))
70- mat = showNormalizedImage (str (index ), normalized_image )
71- index += 1
72- elif ch == ord ('s' ): # save image
73- for data in g_normalized_images :
74- # cv2.imwrite('images/' + str(time.time()) + '.png', image)
75- cv2 .destroyWindow (data [0 ])
76- data [1 ].save (str (time .time ()) + '.png' )
77- print ('Image saved' )
78- data [1 ].recycle ()
92+ else :
93+ print ('No image to save' )
94+
7995
8096 if image is not None :
8197 scanner .detectMatAsync (image )
@@ -93,9 +109,9 @@ def process_video(scanner):
93109
94110 cv2 .drawContours (image , [np .int0 ([(x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y4 )])], 0 , (0 , 255 , 0 ), 2 )
95111
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 )
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 )
99115 cv2 .imshow ('Document Scanner' , image )
100116
101117 for data in g_normalized_images :
0 commit comments