Skip to content

Commit 71849c7

Browse files
committed
Update
1 parent aea2257 commit 71849c7

File tree

4 files changed

+171
-68
lines changed

4 files changed

+171
-68
lines changed

src/docscanner.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,3 @@ PyMODINIT_FUNC PyInit_docscanner(void)
102102
return module;
103103
}
104104

105-

src/document_result.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ typedef struct
99
{
1010
PyObject_HEAD
1111
PyObject *confidence;
12-
PyObject *text;
1312
PyObject *x1;
1413
PyObject *y1;
1514
PyObject *x2;
@@ -23,7 +22,6 @@ typedef struct
2322
static void DocumentResult_dealloc(DocumentResult *self)
2423
{
2524
if (self->confidence) Py_DECREF(self->confidence);
26-
if (self->text) Py_DECREF(self->text);
2725
if (self->x1) Py_DECREF(self->x1);
2826
if (self->y1) Py_DECREF(self->y1);
2927
if (self->x2) Py_DECREF(self->x2);
@@ -45,7 +43,6 @@ static PyObject *DocumentResult_new(PyTypeObject *type, PyObject *args, PyObject
4543

4644
static PyMemberDef DocumentResult_members[] = {
4745
{"confidence", T_OBJECT_EX, offsetof(DocumentResult, confidence), 0, "confidence"},
48-
{"text", T_OBJECT_EX, offsetof(DocumentResult, text), 0, "text"},
4946
{"x1", T_OBJECT_EX, offsetof(DocumentResult, x1), 0, "x1"},
5047
{"y1", T_OBJECT_EX, offsetof(DocumentResult, y1), 0, "y1"},
5148
{"x2", T_OBJECT_EX, offsetof(DocumentResult, x2), 0, "x2"},

src/document_scanner.h

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -77,50 +77,49 @@ static PyObject *DynamsoftDocumentScanner_new(PyTypeObject *type, PyObject *args
7777
return (PyObject *)self;
7878
}
7979

80-
// PyObject *createPyList(DLR_ResultArray *pResults)
81-
// {
82-
// int count = pResults->resultsCount;
83-
84-
// // Create a Python object to store results
85-
// PyObject *list = PyList_New(0);
86-
// for (int i = 0; i < count; i++)
87-
// {
88-
// DLR_Result *mrzResult = pResults->results[i];
89-
// int lCount = mrzResult->lineResultsCount;
90-
// for (int j = 0; j < lCount; j++)
91-
// {
92-
// // printf("Line result %d: %s\n", j, mrzResult->lineResults[j]->text);
93-
94-
// DM_Point *points = mrzResult->lineResults[j]->location.points;
95-
// int x1 = points[0].x;
96-
// int y1 = points[0].y;
97-
// int x2 = points[1].x;
98-
// int y2 = points[1].y;
99-
// int x3 = points[2].x;
100-
// int y3 = points[2].y;
101-
// int x4 = points[3].x;
102-
// int y4 = points[3].y;
103-
// DocumentResult *result = PyObject_New(DocumentResult, &DocumentResultType);
104-
// result->confidence = Py_BuildValue("i", mrzResult->lineResults[j]->confidence);
105-
// result->text = PyUnicode_FromString(mrzResult->lineResults[j]->text);
106-
// result->x1 = Py_BuildValue("i", x1);
107-
// result->y1 = Py_BuildValue("i", y1);
108-
// result->x2 = Py_BuildValue("i", x2);
109-
// result->y2 = Py_BuildValue("i", y2);
110-
// result->x3 = Py_BuildValue("i", x3);
111-
// result->y3 = Py_BuildValue("i", y3);
112-
// result->x4 = Py_BuildValue("i", x4);
113-
// result->y4 = Py_BuildValue("i", y4);
114-
115-
// PyList_Append(list, (PyObject *)result);
116-
// }
117-
// }
118-
119-
// return list;
120-
// }
80+
PyObject *createPyList(DetectedQuadResultArray *pResults)
81+
{
82+
// Create a Python object to store results
83+
PyObject *list = PyList_New(0);
84+
85+
if (pResults)
86+
{
87+
int count = pResults->resultsCount;
88+
89+
for (int i = 0; i < count; i++)
90+
{
91+
DetectedQuadResult *quadResult = pResults->detectedQuadResults[i];
92+
int confidence = quadResult->confidenceAsDocumentBoundary;
93+
DM_Point *points = quadResult->location->points;
94+
int x1 = points[0].coordinate[0];
95+
int y1 = points[0].coordinate[1];
96+
int x2 = points[1].coordinate[0];
97+
int y2 = points[1].coordinate[1];
98+
int x3 = points[2].coordinate[0];
99+
int y3 = points[2].coordinate[1];
100+
int x4 = points[3].coordinate[0];
101+
int y4 = points[3].coordinate[1];
102+
103+
DocumentResult *result = PyObject_New(DocumentResult, &DocumentResultType);
104+
result->confidence = Py_BuildValue("i", confidence);
105+
result->x1 = Py_BuildValue("i", x1);
106+
result->y1 = Py_BuildValue("i", y1);
107+
result->x2 = Py_BuildValue("i", x2);
108+
result->y2 = Py_BuildValue("i", y2);
109+
result->x3 = Py_BuildValue("i", x3);
110+
result->y3 = Py_BuildValue("i", y3);
111+
result->x4 = Py_BuildValue("i", x4);
112+
result->y4 = Py_BuildValue("i", y4);
113+
114+
PyList_Append(list, (PyObject *)result);
115+
}
116+
}
117+
118+
return list;
119+
}
121120

122121
/**
123-
* Recognize MRZ from image files.
122+
* Recognize document from image files.
124123
*
125124
* @param string filename
126125
*
@@ -136,24 +135,25 @@ static PyObject *decodeFile(PyObject *obj, PyObject *args)
136135
return NULL;
137136
}
138137

139-
NormalizedImageResult* normalizedResult = NULL;
140-
int ret = DDN_NormalizeFile(self->handler, pFileName, "", NULL, &normalizedResult);
138+
DetectedQuadResultArray *pResults = NULL;
139+
140+
int ret = DDN_DetectQuadFromFile(self->handler, pFileName, "", &pResults);
141141
if (ret)
142142
{
143143
printf("Detection error: %s\n", DC_GetErrorString(ret));
144144
}
145145

146-
PyObject *list = NULL;
146+
PyObject *list = createPyList(pResults);
147147

148148
// Release memory
149-
if (normalizedResult != NULL)
150-
DDN_FreeNormalizedImageResult(&normalizedResult);
149+
if (pResults != NULL)
150+
DDN_FreeDetectedQuadResultArray(&pResults);
151151

152152
return list;
153153
}
154154

155155
/**
156-
* Recognize MRZ from OpenCV Mat.
156+
* Recognize document from OpenCV Mat.
157157
*
158158
* @param Mat image
159159
*
@@ -207,29 +207,30 @@ static PyObject *decodeMat(PyObject *obj, PyObject *args)
207207
data.format = format;
208208
data.bytesLength = len;
209209

210-
NormalizedImageResult* normalizedResult = NULL;
211-
int ret = DDN_NormalizeBuffer(self->handler, &data, "", NULL, &normalizedResult);
210+
DetectedQuadResultArray *pResults = NULL;
211+
212+
int ret = DDN_DetectQuadFromBuffer(self->handler, &data, "", &pResults);
212213
if (ret)
213214
{
214215
printf("Detection error: %s\n", DC_GetErrorString(ret));
215216
}
216217

217-
PyObject *list = NULL;
218+
PyObject *list = createPyList(pResults);
218219

219220
// Release memory
220-
if (normalizedResult != NULL)
221-
DDN_FreeNormalizedImageResult(&normalizedResult);
221+
if (pResults != NULL)
222+
DDN_FreeDetectedQuadResultArray(&pResults);
222223

223224
Py_DECREF(memoryview);
224225

225226
return list;
226227
}
227228

228-
void onResultReady(DynamsoftDocumentScanner *self)
229+
void onResultReady(DynamsoftDocumentScanner *self, DetectedQuadResultArray *pResults)
229230
{
230231
PyGILState_STATE gstate;
231232
gstate = PyGILState_Ensure();
232-
PyObject *list = NULL;
233+
PyObject *list = createPyList(pResults);
233234
PyObject *result = PyObject_CallFunction(self->callback, "O", list);
234235
if (result != NULL)
235236
Py_DECREF(result);
@@ -247,23 +248,23 @@ void scan(DynamsoftDocumentScanner *self, unsigned char *buffer, int width, int
247248
data.format = format;
248249
data.bytesLength = len;
249250

250-
NormalizedImageResult* normalizedResult = NULL;
251-
int ret = DDN_NormalizeBuffer(self->handler, &data, "", NULL, &normalizedResult);
251+
DetectedQuadResultArray *pResults = NULL;
252+
int ret = DDN_DetectQuadFromBuffer(self->handler, &data, "", &pResults);
252253
if (ret)
253254
{
254255
printf("Detection error: %s\n", DC_GetErrorString(ret));
255256
}
256257

257-
// Release memory
258-
if (normalizedResult != NULL)
259-
DDN_FreeNormalizedImageResult(&normalizedResult);
260-
261258
free(buffer);
262-
onResultReady(self);
259+
onResultReady(self, pResults);
260+
261+
// Release memory
262+
if (pResults != NULL)
263+
DDN_FreeDetectedQuadResultArray(&pResults);
263264
}
264265

265266
/**
266-
* Recognize MRZ from OpenCV Mat asynchronously.
267+
* Recognize document from OpenCV Mat asynchronously.
267268
*
268269
* @param Mat image
269270
*
@@ -346,7 +347,7 @@ void run(DynamsoftDocumentScanner *self)
346347
}
347348

348349
/**
349-
* Register callback function to receive MRZ decoding result asynchronously.
350+
* Register callback function to receive document decoding result asynchronously.
350351
*/
351352
static PyObject *addAsyncListener(PyObject *obj, PyObject *args)
352353
{

test.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from time import sleep
2+
import docscanner
3+
import numpy as np
4+
import cv2
5+
# set license
6+
docscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
7+
8+
scanner = docscanner.createInstance()
9+
10+
# # decodeFile()
11+
# print('')
12+
# print('Test decodeFile()')
13+
# results = scanner.decodeFile("images/3.png")
14+
# image = cv2.imread("images/3.png")
15+
# for result in results:
16+
# x1 = result.x1
17+
# y1 = result.y1
18+
# x2 = result.x2
19+
# y2 = result.y2
20+
# x3 = result.x3
21+
# y3 = result.y3
22+
# x4 = result.x4
23+
# y4 = result.y4
24+
25+
# cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
26+
27+
# cv2.imshow("image", image)
28+
# cv2.waitKey(0)
29+
30+
# decodeMat()
31+
# print('')
32+
# print('Test decodeMat()')
33+
34+
# image = cv2.imread("images/1.png")
35+
# results = scanner.decodeMat(image)
36+
# for result in 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+
# cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
47+
48+
# cv2.imshow("image", image)
49+
# cv2.waitKey(0)
50+
51+
# decodeMatAsync()
52+
print('')
53+
print('Test decodeMatAsync()')
54+
# def callback(results):
55+
# for result in results:
56+
# x1 = result.x1
57+
# y1 = result.y1
58+
# x2 = result.x2
59+
# y2 = result.y2
60+
# x3 = result.x3
61+
# y3 = result.y3
62+
# x4 = result.x4
63+
# y4 = result.y4
64+
65+
# cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
66+
67+
# cv2.imshow("image", image)
68+
# cv2.waitKey(0)
69+
70+
# import cv2
71+
# image = cv2.imread("images/1.png")
72+
# scanner.addAsyncListener(callback)
73+
# scanner.decodeMatAsync(image)
74+
# sleep(5)
75+
76+
g_results = None
77+
78+
def callback(results):
79+
global g_results
80+
g_results = results
81+
82+
scanner.addAsyncListener(callback)
83+
84+
cap = cv2.VideoCapture(0)
85+
while True:
86+
ret, image = cap.read()
87+
if image is not None:
88+
scanner.decodeMatAsync(image)
89+
90+
if g_results != None:
91+
for result in g_results:
92+
x1 = result.x1
93+
y1 = result.y1
94+
x2 = result.x2
95+
y2 = result.y2
96+
x3 = result.x3
97+
y3 = result.y3
98+
x4 = result.x4
99+
y4 = result.y4
100+
101+
cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
102+
103+
cv2.imshow('Document Scanner', image)
104+
ch = cv2.waitKey(1)
105+
if ch == 27:
106+
break

0 commit comments

Comments
 (0)