Skip to content

Commit 2fc5199

Browse files
committed
Optimized sample code for better user experience
1 parent fc800fa commit 2fc5199

File tree

5 files changed

+87
-56
lines changed

5 files changed

+87
-56
lines changed

docscanner/scripts.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
g_results = None
99
g_normalized_images = []
10+
index = 0
1011

1112
def callback(results):
1213
global g_results
@@ -20,6 +21,7 @@ def showNormalizedImage(name, normalized_image):
2021
def 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

4450
def 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:

examples/camera/test.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
g_results = None
99
g_normalized_images = []
10+
index = 0
1011

1112
def callback(results):
1213
global g_results
@@ -18,6 +19,7 @@ def showNormalizedImage(name, normalized_image):
1819
return mat
1920

2021
def process_video(scanner):
22+
global g_normalized_images, index
2123
scanner.addAsyncListener(callback)
2224

2325
cap = cv2.VideoCapture(0)
@@ -29,29 +31,37 @@ def process_video(scanner):
2931
break
3032
elif ch == ord('n'): # normalize image
3133
if g_results != None:
34+
35+
if len(g_results) > 0:
36+
for result in g_results:
37+
x1 = result.x1
38+
y1 = result.y1
39+
x2 = result.x2
40+
y2 = result.y2
41+
x3 = result.x3
42+
y3 = result.y3
43+
x4 = result.x4
44+
y4 = result.y4
45+
46+
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
47+
g_normalized_images.append((str(index), normalized_image))
48+
showNormalizedImage(str(index), normalized_image)
49+
index += 1
50+
else:
51+
print('No document found')
52+
elif ch == ord('s'): # save image
53+
if len(g_normalized_images) > 0:
54+
for data in g_normalized_images:
55+
# cv2.imwrite('images/' + str(time.time()) + '.png', image)
56+
cv2.destroyWindow(data[0])
57+
data[1].save(str(time.time()) + '.png')
58+
print('Image saved')
59+
data[1].recycle()
60+
3261
g_normalized_images = []
3362
index = 0
34-
for result in g_results:
35-
x1 = result.x1
36-
y1 = result.y1
37-
x2 = result.x2
38-
y2 = result.y2
39-
x3 = result.x3
40-
y3 = result.y3
41-
x4 = result.x4
42-
y4 = result.y4
43-
44-
normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
45-
g_normalized_images.append((str(index), normalized_image))
46-
mat = showNormalizedImage(str(index), normalized_image)
47-
index += 1
48-
elif ch == ord('s'): # save image
49-
for data in g_normalized_images:
50-
# cv2.imwrite('images/' + str(time.time()) + '.png', image)
51-
cv2.destroyWindow(data[0])
52-
data[1].save(str(time.time()) + '.png')
53-
print('Image saved')
54-
data[1].recycle()
63+
else:
64+
print('No image to save')
5565

5666
if image is not None:
5767
scanner.detectMatAsync(image)
@@ -69,9 +79,9 @@ def process_video(scanner):
6979

7080
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
7181

72-
cv2.putText(image, 'Press "n" to normalize image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
73-
cv2.putText(image, 'Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
74-
cv2.putText(image, 'Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
82+
cv2.putText(image, '1. Press "n" to normalize image', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
83+
cv2.putText(image, '2. Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
84+
cv2.putText(image, '3. Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
7585
cv2.imshow('Document Scanner', image)
7686

7787
for data in g_normalized_images:

examples/file/test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def showNormalizedImage(name, normalized_image):
1313
def process_file(filename, scanner):
1414
image = cv2.imread(filename)
1515
results = scanner.detectMat(image)
16+
normalized_image = None
1617
for result in results:
1718
x1 = result.x1
1819
y1 = result.y1
@@ -27,12 +28,16 @@ def process_file(filename, scanner):
2728
showNormalizedImage("Normalized Image", normalized_image)
2829
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
2930

31+
cv2.putText(image, 'Press "ESC" to exit', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
3032
cv2.imshow('Document Image', image)
3133
cv2.waitKey(0)
3234

33-
normalized_image.save(str(time.time()) + '.png')
34-
print('Image saved')
35-
normalized_image.recycle()
35+
if normalized_image is not None:
36+
normalized_image.save(str(time.time()) + '.png')
37+
print('Image saved')
38+
normalized_image.recycle()
39+
else:
40+
print('No document found')
3641

3742
def scandocument():
3843
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def run(self):
8383

8484

8585
setup(name='document-scanner-sdk',
86-
version='1.0.0',
86+
version='1.0.1',
8787
description='Document Scanner SDK for document edge detection, border cropping, perspective correction and brightness adjustment',
8888
long_description=long_description,
8989
long_description_content_type="text/markdown",

src/document_scanner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int DynamsoftDocumentScanner_clear(DynamsoftDocumentScanner *self)
5151
self->worker->t.join();
5252
delete self->worker;
5353
self->worker = NULL;
54-
printf("Quit native thread.\n");
54+
// printf("Quit native thread.\n");
5555
}
5656

5757
return 0;

0 commit comments

Comments
 (0)