11# Python Document Scanner SDK
2- The project is a Python binding to [ Dynamsoft C/C++ Document Scanner SDK] ( https://www.dynamsoft.com/document-normalizer/docs/introduction/?ver=latest ) . It aims to help developers quickly build desktop document scanner applications in Python on Windows and Linux.
2+ This project provides Python bindings for the [ Dynamsoft C/C++ Document Scanner SDK v1.x ] ( https://www.dynamsoft.com/document-normalizer/docs/core/ introduction/?ver=latest&ver=latest ) , enabling developers to quickly create document scanner applications for Windows and Linux desktop environments .
33
44## About Dynamsoft Document Scanner
5- Get a [ 30-day FREE trial license] ( https://www.dynamsoft.com/customer/license/trialLicense?product=ddn ) to activate the SDK .
5+ - Activate the SDK with a [ 30-day FREE trial license] ( https://www.dynamsoft.com/customer/license/trialLicense?product=ddn ) .
66
77
8- ## Supported Python Edition
8+ ## Supported Python Versions
99* Python 3.x
1010
1111## Dependencies
12+ Install the required dependencies using pip:
1213
1314``` bash
1415pip install opencv-python
1516```
1617
1718## Command-line Usage
18- ``` bash
19- # Scan documents from images
20- $ scandocument -f < file-name> -l < license-key>
19+ - Scan documents from images:
20+
21+ ``` bash
22+ scandocument -f < file-name> -l < license-key>
23+ ```
2124
22- # Scan documents from camera video stream
23- $ scandocument -c 1 -l < license-key>
24- ```
25+ - Scan documents from a camera video stream:
26+
27+ ` ` ` bash
28+ scandocument -c 1 -l < license-key>
29+ ` ` `
2530
2631# # Quick Start
2732- Scan documents from an image file:
@@ -80,7 +85,7 @@ $ scandocument -c 1 -l <license-key>
8085
8186 # set license
8287 if license == ' ' :
83- docscanner.initLicense(" DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ== " )
88+ docscanner.initLicense(" LICENSE-KEY " )
8489 else:
8590 docscanner.initLicense(license)
8691
@@ -112,26 +117,29 @@ $ scandocument -c 1 -l <license-key>
112117 g_results = None
113118 g_normalized_images = []
114119
120+
115121 def callback(results):
116122 global g_results
117123 g_results = results
118124
125+
119126 def showNormalizedImage(name, normalized_image):
120127 mat = docscanner.convertNormalizedImage2Mat(normalized_image)
121128 cv2.imshow(name, mat)
122129 return mat
123-
130+
131+
124132 def process_video(scanner):
125133 scanner.addAsyncListener(callback)
126-
134+
127135 cap = cv2.VideoCapture(0)
128136 while True:
129137 ret, image = cap.read ()
130-
138+
131139 ch = cv2.waitKey(1)
132140 if ch == 27:
133141 break
134- elif ch == ord (' n' ): # normalize image
142+ elif ch == ord(' n' ): # normalize image
135143 if g_results ! = None:
136144 g_normalized_images = []
137145 index = 0
@@ -144,23 +152,25 @@ $ scandocument -c 1 -l <license-key>
144152 y3 = result.y3
145153 x4 = result.x4
146154 y4 = result.y4
147-
148- normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
149- g_normalized_images.append((str (index), normalized_image))
155+
156+ normalized_image = scanner.normalizeBuffer(
157+ image, x1, y1, x2, y2, x3, y3, x4, y4)
158+ g_normalized_images.append(
159+ (str(index), normalized_image))
150160 mat = showNormalizedImage(str(index), normalized_image)
151161 index += 1
152- elif ch == ord (' s' ): # save image
162+ elif ch == ord(' s' ): # save image
153163 for data in g_normalized_images:
154164 # cv2.imwrite('images/' + str(time.time()) + '.png', image)
155165 cv2.destroyWindow(data[0])
156166 data[1].save(str(time.time ()) + ' .png' )
157167 print(' Image saved' )
158-
168+
159169 g_normalized_images = []
160-
170+
161171 if image is not None:
162172 scanner.detectMatAsync(image)
163-
173+
164174 if g_results ! = None:
165175 for result in g_results:
166176 x1 = result.x1
@@ -171,71 +181,48 @@ $ scandocument -c 1 -l <license-key>
171181 y3 = result.y3
172182 x4 = result.x4
173183 y4 = result.y4
174-
175- cv2.drawContours(image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0 , (0 , 255 , 0 ), 2 )
176-
177- cv2.putText(image, ' Press "n" to normalize image' , (10 , 30 ), cv2.FONT_HERSHEY_SIMPLEX , 0.8 , (0 , 0 , 255 ), 2 )
178- cv2.putText(image, ' Press "s" to save image' , (10 , 60 ), cv2.FONT_HERSHEY_SIMPLEX , 0.8 , (0 , 0 , 255 ), 2 )
179- cv2.putText(image, ' Press "ESC" to exit' , (10 , 90 ), cv2.FONT_HERSHEY_SIMPLEX , 0.8 , (0 , 0 , 255 ), 2 )
184+
185+ cv2.drawContours(
186+ image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
187+
188+ cv2.putText(image, ' Press "n" to normalize image' ,
189+ (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
190+ cv2.putText(image, ' Press "s" to save image' , (10, 60),
191+ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
192+ cv2.putText(image, ' Press "ESC" to exit' , (10, 90),
193+ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
180194 cv2.imshow(' Document Scanner' , image)
181195
182- def scandocument ():
183- """
184- Command-line script for scanning documents from camera video stream.
185- """
186- parser = argparse.ArgumentParser(description = ' Scan documents from camera' )
187- parser.add_argument(' -c' , ' --camera' , default = False , type = bool , help = ' Whether to show the image' )
188- parser.add_argument(' -l' , ' --license' , default = ' ' , type = str , help = ' Set a valid license key' )
189- args = parser.parse_args()
190- # print(args)
191- try :
192- license = args.license
193- camera = args.camera
194-
195- if camera is False :
196- parser.print_help()
197- return
198-
199- # set license
200- if license == ' ' :
201- docscanner.initLicense(" DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==" )
202- else :
203- docscanner.initLicense(license )
204-
205- # initialize mrz scanner
206- scanner = docscanner.createInstance()
207- ret = scanner.setParameters(docscanner.Templates.color)
208196
209- if camera is True :
210- process_video(scanner )
211-
212- except Exception as err:
213- print (err )
214- sys.exit( 1 )
197+ docscanner.initLicense(
198+ " LICENSE-KEY " )
199+
200+ scanner = docscanner.createInstance ()
201+ ret = scanner.setParameters(docscanner.Templates.color )
202+ process_video(scanner )
215203
216- scandocument()
217204 ` ` `
218205
219206 ! [python document scanner from camera](https://www.dynamsoft.com/codepool/img/2022/09/python-document-scanner.png)
220207
221- # # Methods
222- - `docscanner.initLicense(' YOUR-LICENSE-KEY' )` # set the license key
208+ # # API Methods
209+ - ` docscanner.initLicense(' YOUR-LICENSE-KEY' )` : Set the license key.
223210
224211 ` ` ` python
225- docscanner.initLicense(" DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ== " )
212+ docscanner.initLicense(" LICENSE-KEY " )
226213 ` ` `
227214
228- - `docscanner.createInstance()` # create a Document Scanner instance
215+ - ` docscanner.createInstance()` : Create a Document Scanner instance.
229216
230217 ` ` ` python
231218 scanner = docscanner.createInstance ()
232219 ` ` `
233- - `detectFile(filename)` # do edge detection from an image file
220+ - ` detectFile(filename)` : Perform edge detection from an image file.
234221
235222 ` ` ` python
236223 results = scanner.detectFile(< filename> )
237224 ` ` `
238- - `detectMat(Mat image)` # do edge detection from Mat
225+ - ` detectMat(Mat image)` : Perform edge detection from an OpenCV Mat.
239226 ` ` ` python
240227 image = cv2.imread(< filename> )
241228 results = scanner.detectMat(image)
@@ -250,14 +237,14 @@ $ scandocument -c 1 -l <license-key>
250237 y4 = result.y4
251238 ` ` `
252239
253- - `setParameters(Template)` # Select color, binary or grayscale template
240+ - ` setParameters(Template)` : Select color, binary, or grayscale template.
254241
255242 ` ` ` python
256243 scanner.setParameters(docscanner.Templates.color)
257244 ` ` `
258245
259- - `addAsyncListener(callback function)` # start a native thread to run document scanning tasks
260- - `detectMatAsync(< opencv mat data> )` # put a document scanning task into the native queue
246+ - ` addAsyncListener(callback function)` : Start a native thread to run document scanning tasks asynchronously.
247+ - ` detectMatAsync(< opencv mat data> )` : Queue a document scanning task into the native thread.
261248 ` ` ` python
262249 def callback(results):
263250 for result in results:
@@ -277,23 +264,23 @@ $ scandocument -c 1 -l <license-key>
277264 sleep(5)
278265 ` ` `
279266
280- - `normalizeBuffer(mat, x1, y1, x2, y2, x3, y3, x4, y4)` # do perspective correction from Mat
267+ - ` normalizeBuffer(mat, x1, y1, x2, y2, x3, y3, x4, y4)` : Perform perspective correction from an OpenCV Mat.
268+
281269 ` ` ` python
282270 normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
283271 ` ` `
284- - `normalizeFile(filename, x1, y1, x2, y2, x3, y3, x4, y4)` # do perspective correction from a file
272+ - ` normalizeFile(filename, x1, y1, x2, y2, x3, y3, x4, y4)` : Perform perspective correction from an image file.
273+
285274 ` ` ` python
286275 normalized_image = scanner.normalizeFile(< filename> , x1, y1, x2, y2, x3, y3, x4, y4)
287276 ` ` `
288- - `normalized_image.save(filename)` # save the normalized image to a file
277+ - ` normalized_image.save(filename)` : Save the normalized image to a file.
289278 ` ` ` python
290279 normalized_image.save(< filename> )
291280 ` ` `
292- - `normalized_image.recycle()` # release the memory of the normalized image
293- - `clearAsyncListener()` # stop the native thread and clear the registered Python function
294- # # C/C++ API
295- To customize Python API based on C/ C++ , please refer to the
296- [online documentation](https:// www.dynamsoft.com/ document- normalizer/ docs/ programming/ c/ api- reference/ ? ver = latest).
281+ - ` normalized_image.recycle()` : Release the memory of the normalized image.
282+ - ` clearAsyncListener()` : Stop the native thread and clear the registered Python function.
283+
297284
298285# # How to Build the Python Document Scanner Extension
299286- Create a source distribution:
0 commit comments