diff --git a/python_nbis/bozorth.i b/python_nbis/bozorth.i index e381248..e23cee8 100644 --- a/python_nbis/bozorth.i +++ b/python_nbis/bozorth.i @@ -1,5 +1,8 @@ /* bozorth.i */ %module bozorth +%begin %{ +#define SWIG_PYTHON_STRICT_BYTE_CHAR +%} %include "typemaps.i" %include "bz_comon.i" @@ -21,9 +24,9 @@ int i; for(i = 0; i < xyt_s.nrows; i++){ dict = PyList_GetItem($input, (Py_ssize_t)i); - xyt_s.xcol[i] = PyDict_Contains(dict, llaveX) ? (int)PyInt_AS_LONG(PyDict_GetItem(dict, llaveX)) : 0; - xyt_s.ycol[i] = PyDict_Contains(dict, llaveX) ? (int)PyInt_AS_LONG(PyDict_GetItem(dict, llaveY)) : 0; - xyt_s.thetacol[i] = PyDict_Contains(dict, llaveX) ? (int)PyInt_AS_LONG(PyDict_GetItem(dict, llaveT)) : 0; + xyt_s.xcol[i] = PyDict_Contains(dict, llaveX) ? (int)PyInt_AsLong(PyDict_GetItem(dict, llaveX)) : 0; + xyt_s.ycol[i] = PyDict_Contains(dict, llaveX) ? (int)PyInt_AsLong(PyDict_GetItem(dict, llaveY)) : 0; + xyt_s.thetacol[i] = PyDict_Contains(dict, llaveX) ? (int)PyInt_AsLong(PyDict_GetItem(dict, llaveT)) : 0; } }else{ printf("Bozorth: Received invalid object to build xyt_struct"); diff --git a/python_nbis/lfs.i b/python_nbis/lfs.i index d39d852..ca00dcb 100644 --- a/python_nbis/lfs.i +++ b/python_nbis/lfs.i @@ -1,5 +1,8 @@ /* lfs.i */ %module lfs +%begin %{ +#define SWIG_PYTHON_STRICT_BYTE_CHAR +%} %include "bz_comon.i" %apply int *OUTPUT { int * }; @@ -37,7 +40,7 @@ } %typemap(argout) MINUTIAE ** { printf("\nReturning %d minutia using %d adress space...\n%ld\n", (**$1).num, (**$1).alloc, (**$1).num*sizeof(MINUTIAE)); - $result = SWIG_Python_AppendOutput(resultobj, PyString_FromStringAndSize((const char*)(*$1), (**$1).alloc)); + $result = SWIG_Python_AppendOutput(resultobj, PyBytes_FromStringAndSize((const char*)(*$1), (**$1).alloc)); FILE *fp; if((fp = fopen("out/minutiaeBefore.bin", "wb")) == (FILE *)NULL){ @@ -78,7 +81,7 @@ $1 = &oIntP; } %typemap(argout) (int **){ -// $result = SWIG_Python_AppendOutput(resultobj, PyString_FromStringAndSize((const char*)(*$1), *$2)); +// $result = SWIG_Python_AppendOutput(resultobj, PyBytes_FromStringAndSize((const char*)(*$1), *$2)); } %typemap(in,numinputs=0) int * (int oInt){ $1 = &oInt; @@ -91,7 +94,7 @@ } %typemap(argout) (unsigned char **){ printf("Returning char **"); -// $result = SWIG_Python_AppendOutput(resultobj, PyString_FromStringAndSize((const char*)(*$1), *$2)); +// $result = SWIG_Python_AppendOutput(resultobj, PyBytes_FromStringAndSize((const char*)(*$1), *$2)); } %typemap(in,numinputs=0) LFSPARMS *{ @@ -99,7 +102,7 @@ } %typemap(argout) (LFSPARMS *){ printf("Returning LFSPARMS\n"); -// $result = SWIG_Python_AppendOutput(resultobj, PyString_FromStringAndSize((const char*)(*$1), *$2)); +// $result = SWIG_Python_AppendOutput(resultobj, PyBytes_FromStringAndSize((const char*)(*$1), *$2)); } %{ diff --git a/python_nbis/nfiq.i b/python_nbis/nfiq.i index ed0feed..960b2e1 100644 --- a/python_nbis/nfiq.i +++ b/python_nbis/nfiq.i @@ -1,5 +1,8 @@ /* nfiq.i */ %module nfiq +%begin %{ +#define SWIG_PYTHON_STRICT_BYTE_CHAR +%} %include "typemaps.i" %apply int *OUTPUT { int * }; diff --git a/python_nbis/test-pyNBIS.py b/python_nbis/test-pyNBIS.py index a79c341..56a4897 100644 --- a/python_nbis/test-pyNBIS.py +++ b/python_nbis/test-pyNBIS.py @@ -1,5 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import unicode_literals import array import wsq @@ -34,41 +35,41 @@ depth = 8 ppi = 500 - print "\n\nTesting NFIQ:" + print("\n\nTesting NFIQ:") result = nfiq.comp_nfiq(huella, w, h, depth, depth) - print "Scan quality from pyNBIS: ", result, " vs quality from nfiq: " + print("Scan quality from pyNBIS: ", result, " vs quality from nfiq: ") call(["../install_dir/bin/nfiq", "-d", huella_raw, "-raw", "%d,%d,%d"%(w, h,depth)]) if debug: - print "Debug -> executing NFIQ" - print "../install_dir/bin/nfiq", "-d", huella_raw, "-raw", "%d,%d,%d"%(w, h,depth) + print("Debug -> executing NFIQ") + print("../install_dir/bin/nfiq", "-d", huella_raw, "-raw", "%d,%d,%d"%(w, h,depth)) - print "\n\nTesting WSQ codification:" - result = wsq.wsq_encode_mem(bitrate, huella, w, h, depth, ppi, "") - print result[:2] + print("\n\nTesting WSQ codification:") + result = wsq.wsq_encode_mem(bitrate, huella, w, h, depth, ppi, b"") + print(result[:2]) with open(huella_raw.split(".")[0]+"_py.wsq", "wb") as raw_file: raw_file.write(result[2]) call(["../install_dir/bin/cwsq", "%f"%bitrate, "wsq", huella_raw, "-raw_in", "%d,%d,%d,%d"%(w, h, depth, ppi), "/dev/null"]) #TODO: Compare MD5 Sum and output https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file if debug: - print "Debug -> executing WSQ" - print "../install_dir/bin/cwsq", "%f"%bitrate, "wsq", huella_raw, "-raw_in", "%d,%d,%d,%d"%(w, h, depth, ppi), "/dev/null" + print("Debug -> executing WSQ") + print("../install_dir/bin/cwsq", "%f"%bitrate, "wsq", huella_raw, "-raw_in", "%d,%d,%d,%d"%(w, h, depth, ppi), "/dev/null") - print "\n\nTesting MINDTCT" + print("\n\nTesting MINDTCT") #Note: We don't need to use WSQ to extract minutiae!! salida = lfs.get_minutiae(huella, w, h, depth, ppi) - lfs.write_minutiae_XYTQ("out/testMINDTCTpy.xyt", lfs.NIST_INTERNAL_XYT_REP, salida[1], w, h) + lfs.write_minutiae_XYTQ("out/testMINDTCTpy.xyt".encode('utf-8'), lfs.NIST_INTERNAL_XYT_REP, salida[1], w, h) # lfs.free_minutiae(salida[1]) #TODO: Run OS mindtct and compare output - print "\n\nTesting BOZORTH" + print("\n\nTesting BOZORTH") xyt1 = lfs.get_XYT(lfs.NIST_INTERNAL_XYT_REP, huella, w, h, depth, ppi) xyt2 = lfs.get_XYT(lfs.NIST_INTERNAL_XYT_REP, huella2, w, h, depth, ppi) [match, xyt1, xyt2] = bozorth.bozorth_main(xyt1, xyt2) #TODO: Run OS bozorth and compare output - print "Match:", match + print("Match:", match) diff --git a/python_nbis/test-wsq_decode_mem.py b/python_nbis/test-wsq_decode_mem.py new file mode 100644 index 0000000..1ffc248 --- /dev/null +++ b/python_nbis/test-wsq_decode_mem.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import os +import wsq + +from PIL import Image + +from subprocess import call + + +huella = "dedo9" + +with open(huella + '.wsq', "rb") as wsq_file: + print("\n\nTesting WSQ decodification:") + + wsq_file.seek(0, os.SEEK_END) + size = wsq_file.tell() + wsq_file.seek(0, os.SEEK_SET) + result = wsq.wsq_decode_mem(wsq_file.read(), size) + wsq = { + 'odata': result[6], + 'ow': result[5], + 'oh': result[4], + 'od': result[3], + 'oppi': result[2], + 'loosyflag': result[1], + 'wsq_decode_mem': result[0], + } + # print(wsq) + + with open(huella + ".dwsq_py.raw", "wb") as raw_file: + raw_file.write(wsq['odata']) + + # call(["../install_dir/bin/cwsq", "2.25", "wsq", huella + ".dwsq_py.raw", "-raw_in", "%d,%d,%d,%d"%(wsq['ow'], wsq['oh'], wsq['od'], wsq['oppi']), "/dev/null"]) + + call(["../install_dir/bin/dwsq", "raw", huella + '.wsq', "-raw_out"]) + img = Image.frombuffer('L', [wsq['ow'], wsq['oh']], wsq['odata'], "raw", 'L', 0, 1) + img.show() + + # TODO: Compare MD5 Sum and output https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file diff --git a/python_nbis/wsq.i b/python_nbis/wsq.i index de07882..b46a7b9 100644 --- a/python_nbis/wsq.i +++ b/python_nbis/wsq.i @@ -1,5 +1,8 @@ /* wsq.i */ %module wsq +%begin %{ +#define SWIG_PYTHON_STRICT_BYTE_CHAR +%} %include "typemaps.i" %apply char* {unsigned char*}; @@ -7,12 +10,29 @@ %typemap(in) unsigned char* = char*; %typemap(in,numinputs=0) (unsigned char **, int *)(unsigned char *odata, int * olen){ - $1 = &odata; - $2 = &olen; + $1 = (unsigned char **)&odata; + $2 = (int *)&olen; } %typemap(argout) (unsigned char **, int *){ $result = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*$2))); - $result = SWIG_Python_AppendOutput(resultobj, PyString_FromStringAndSize((const char*)(*$1), *$2)); + $result = SWIG_Python_AppendOutput(resultobj, PyBytes_FromStringAndSize((const char*)(*$1), *$2)); +} + +%typemap(in,numinputs=0) (unsigned char **, int *, int *, int *, int *, int *)(unsigned char *odata, int *ow, int *oh, int *od, int *oppi, int *loosyflag){ + $1 = (unsigned char **)&odata; + $2 = (int *)&ow; + $3 = (int *)&oh; + $4 = (int *)&od; + $5 = (int *)&oppi; + $6 = (int *)&loosyflag; +} +%typemap(argout) (unsigned char **, int *, int *, int *, int *, int *){ + $result = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*$6))); + $result = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*$5))); + $result = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*$4))); + $result = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*$3))); + $result = SWIG_Python_AppendOutput(resultobj, SWIG_From_int((*$2))); + $result = SWIG_Python_AppendOutput(resultobj, PyBytes_FromStringAndSize((const char*)(*$1), ((*$2)*(*$3)))); } %{ @@ -23,3 +43,6 @@ int debug=0; %include "wsq.h" //extern int wsq_encode_mem(unsigned char **, int *, const float, unsigned char *, // const int, const int, const int, const int, char *); +//extern int wsq_decode_mem(unsigned char **, int *, int *, int *, int *, int *, +// unsigned char *, const int); +