Skip to content

Commit fc800fa

Browse files
committed
Add recycle() method
1 parent 2235508 commit fc800fa

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ $ scandocument -c 1 -l <license-key>
289289
```python
290290
normalized_image.save(<filename>)
291291
```
292+
- `normalized_image.recycle()` # release the memory of the normalized image
292293

293294
## C/C++ API
294295
To customize Python API based on C/C++, please refer to the

docscanner/scripts.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def process_file(filename, scanner):
3939

4040
normalized_image.save(str(time.time()) + '.png')
4141
print('Image saved')
42+
normalized_image.recycle()
4243

4344
def process_video(scanner):
4445
scanner.addAsyncListener(callback)
@@ -74,8 +75,7 @@ def process_video(scanner):
7475
cv2.destroyWindow(data[0])
7576
data[1].save(str(time.time()) + '.png')
7677
print('Image saved')
77-
78-
g_normalized_images = []
78+
data[1].recycle()
7979

8080
if image is not None:
8181
scanner.detectMatAsync(image)
@@ -98,6 +98,10 @@ def process_video(scanner):
9898
cv2.putText(image, 'Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
9999
cv2.imshow('Document Scanner', image)
100100

101+
for data in g_normalized_images:
102+
data[1].recycle()
103+
104+
101105
def scandocument():
102106
"""
103107
Command-line script for scanning documents from a given image or camera video stream.

examples/camera/test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ def process_video(scanner):
5151
cv2.destroyWindow(data[0])
5252
data[1].save(str(time.time()) + '.png')
5353
print('Image saved')
54+
data[1].recycle()
5455

55-
g_normalized_images = []
56-
5756
if image is not None:
5857
scanner.detectMatAsync(image)
5958

@@ -74,6 +73,9 @@ def process_video(scanner):
7473
cv2.putText(image, 'Press "s" to save image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
7574
cv2.putText(image, 'Press "ESC" to exit', (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
7675
cv2.imshow('Document Scanner', image)
76+
77+
for data in g_normalized_images:
78+
data[1].recycle()
7779

7880
def scandocument():
7981
"""

examples/file/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def process_file(filename, scanner):
3232

3333
normalized_image.save(str(time.time()) + '.png')
3434
print('Image saved')
35+
normalized_image.recycle()
3536

3637
def scandocument():
3738
"""

src/document_scanner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ static PyObject *normalizeFile(PyObject *obj, PyObject *args)
444444
quad.points[3].coordinate[1] = y4;
445445

446446
NormalizedImageResult* normalizedResult = NULL;
447+
447448
int errorCode = DDN_NormalizeFile(self->handler, pFileName, "", &quad, &normalizedResult);
448449
if (errorCode != DM_OK)
449450
printf("%s\r\n", DC_GetErrorString(errorCode));

src/normalized_image.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ static PyObject *save(PyObject *obj, PyObject *args)
5858
return Py_BuildValue("i", -1);
5959
}
6060

61+
static PyObject *recycle(PyObject *obj, PyObject *args)
62+
{
63+
NormalizedImage *self = (NormalizedImage *)obj;
64+
65+
if (self->normalizedResult)
66+
{
67+
DDN_FreeNormalizedImageResult(&self->normalizedResult);
68+
self->normalizedResult = NULL;
69+
}
70+
71+
return Py_BuildValue("i", 0);
72+
}
73+
6174
static PyMemberDef NormalizedImage_members[] = {
6275
{"bytearray", T_OBJECT_EX, offsetof(NormalizedImage, bytearray), 0, "bytearray"},
6376
{"length", T_OBJECT_EX, offsetof(NormalizedImage, length), 0, "length"},
@@ -70,6 +83,7 @@ static PyMemberDef NormalizedImage_members[] = {
7083

7184
static PyMethodDef ni_instance_methods[] = {
7285
{"save", save, METH_VARARGS, NULL},
86+
{"recycle", recycle, METH_VARARGS, NULL},
7387
{NULL, NULL, 0, NULL}
7488
};
7589

test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def showNormalizedImage(name, normalized_image):
3535
print(x1, y1, x2, y2, x3, y3, x4, y4)
3636

3737
normalized_image = scanner.normalizeFile("images/1.png", x1, y1, x2, y2, x3, y3, x4, y4)
38+
normalized_image.recycle()
3839
# showNormalizedImage("Normalized Image", normalized_image)
3940
# cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
4041

@@ -60,6 +61,7 @@ def showNormalizedImage(name, normalized_image):
6061

6162
# normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4)
6263
# showNormalizedImage("Normalized Image", normalized_image)
64+
# normalized_image.recycle()
6365
# cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
6466

6567
# cv2.imshow('Document Image', image)
@@ -130,6 +132,7 @@ def showNormalizedImage(name, normalized_image):
130132
# cv2.destroyWindow(data[0])
131133
# data[1].save(str(time.time()) + '.png')
132134
# print('Image saved')
135+
# data[1].recycle()
133136

134137
# g_normalized_images = []
135138

0 commit comments

Comments
 (0)