static PyObject* create_module (void) { //makes sure that PyBobIpFlandmark_Type has a dictionary on tp_dict PyBobIpFlandmark_Type.tp_dict = PyDict_New(); if (!PyBobIpFlandmark_Type.tp_dict) return 0; PyBobIpFlandmark_Type.tp_new = PyType_GenericNew; if (PyType_Ready(&PyBobIpFlandmark_Type) < 0) return 0; # if PY_VERSION_HEX >= 0x03000000 PyObject* module = PyModule_Create(&module_definition); auto module_ = make_xsafe(module); const char* ret = "O"; # else PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr); const char* ret = "N"; # endif if (!module) return 0; /* register the types to python */ Py_INCREF(&PyBobIpFlandmark_Type); if (PyModule_AddObject(module, "Flandmark", (PyObject *)&PyBobIpFlandmark_Type) < 0) return 0; /* imports dependencies */ if (import_bob_blitz() < 0) return 0; if (import_bob_core_logging() < 0) return 0; if (import_bob_io_base() < 0) return 0; return Py_BuildValue(ret, module); }
PyObject* PyBobIpBase_block(PyObject*, PyObject* args, PyObject* kwds) { BOB_TRY /* Parses input arguments in a single shot */ char** kwlist = s_block.kwlist(); PyBlitzArrayObject* input = 0,* output = 0; blitz::TinyVector<int,2> size, overlap(0,0); PyObject* flat_ = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&(ii)|(ii)O&O!", kwlist, &PyBlitzArray_Converter, &input, &size[0], &size[1], &overlap[0], &overlap[1], &PyBlitzArray_OutputConverter, &output, &PyBool_Type, &flat_)) return 0; auto input_ = make_safe(input), output_ = make_xsafe(output); bool flat = f(flat_); if (input->ndim != 2) { PyErr_Format(PyExc_TypeError, "blocks can only be extracted from and to 2D arrays"); return 0; } bool return_out = false; if (output){ if (output->type_num != input->type_num){ PyErr_Format(PyExc_TypeError, "``input`` and ``output`` must have the same data type"); return 0; } if (output->ndim != 3 && output->ndim != 4){ PyErr_Format(PyExc_TypeError, "``output`` must have either three or four dimensions, not %" PY_FORMAT_SIZE_T "d", output->ndim); return 0; } flat = output->ndim == 3; } else { return_out = true; // generate output in the desired shape if (flat){ auto res = block_shape3(input, size, overlap); Py_ssize_t osize[] = {res[0], res[1], res[2]}; output = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(input->type_num, 3, osize); } else { auto res = block_shape4(input, size, overlap); Py_ssize_t osize[] = {res[0], res[1], res[2], res[3]}; output = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(input->type_num, 4, osize); } output_ = make_safe(output); } switch (input->type_num){ case NPY_UINT8: if (flat) block_inner<uint8_t,3>(input, size, overlap, output); else block_inner<uint8_t,4>(input, size, overlap, output); break; case NPY_UINT16: if (flat) block_inner<uint16_t,3>(input, size, overlap, output); else block_inner<uint16_t,4>(input, size, overlap, output); break; case NPY_FLOAT64: if (flat) block_inner<double,3>(input, size, overlap, output); else block_inner<double,4>(input, size, overlap, output); break; default: PyErr_Format(PyExc_TypeError, "block does not work on 'input' images of type %s", PyBlitzArray_TypenumAsString(input->type_num)); } if (return_out){ return PyBlitzArray_AsNumpyArray(output, 0); } else Py_RETURN_NONE; BOB_CATCH_FUNCTION("in block", 0) }
static PyObject* create_module (void) { # if PY_VERSION_HEX >= 0x03000000 PyObject* module = PyModule_Create(&module_definition); auto module_ = make_xsafe(module); const char* ret = "O"; # else PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr); const char* ret = "N"; # endif if (!module) return 0; /* register version numbers and constants */ if (PyModule_AddStringConstant(module, "module", BOB_EXT_MODULE_VERSION) < 0) return 0; if (PyModule_AddObject(module, "externals", build_version_dictionary()) < 0) return 0; return Py_BuildValue(ret, module); }
static PyObject* create_module (void) { # if PY_VERSION_HEX >= 0x03000000 PyObject* module = PyModule_Create(&module_definition); auto module_ = make_xsafe(module); const char* ret = "O"; # else PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr); const char* ret = "N"; # endif if (!module) return 0; /* imports dependencies */ if (import_bob_blitz() < 0) return 0; if (import_bob_core_logging() < 0) return 0; if (import_bob_io_base() < 0) return 0; return Py_BuildValue(ret, module); }
PyObject* PyBobIpBase_integral(PyObject*, PyObject* args, PyObject* kwds) { BOB_TRY /* Parses input arguments in a single shot */ char** kwlist = s_integral.kwlist(); PyBlitzArrayObject* src = 0,* dst = 0,* sqr = 0; PyObject* azb = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&|O&O!", kwlist, &PyBlitzArray_Converter, &src, &PyBlitzArray_OutputConverter, &dst, &PyBlitzArray_OutputConverter, &sqr, &PyBool_Type, &azb)) return 0; auto src_ = make_safe(src), dst_ = make_safe(dst), sqr_ = make_xsafe(sqr); bool b = azb && PyObject_IsTrue(azb); if (src->ndim != 2 || dst->ndim != 2 || (sqr && sqr->ndim != 2)) { PyErr_Format(PyExc_TypeError, "integral images can only be computed from and to 2D arrays"); return 0; } if (sqr && dst->type_num != sqr->type_num) { PyErr_Format(PyExc_TypeError, "'dst' and 'sqr' arrays must have the same data types (dst: `%s' != sqr: `%s')", PyBlitzArray_TypenumAsString(dst->type_num), PyBlitzArray_TypenumAsString(sqr->type_num)); return 0; } switch (src->type_num){ case NPY_INT8: return integral_middle<int8_t>(src, dst, sqr, b); case NPY_INT16: return integral_middle<int16_t>(src, dst, sqr, b); case NPY_INT32: return integral_middle<int32_t>(src, dst, sqr, b); case NPY_INT64: return integral_middle<int64_t>(src, dst, sqr, b); case NPY_UINT8: return integral_middle<uint8_t>(src, dst, sqr, b); case NPY_UINT16: return integral_middle<uint16_t>(src, dst, sqr, b); case NPY_UINT32: return integral_middle<uint32_t>(src, dst, sqr, b); case NPY_UINT64: return integral_middle<uint64_t>(src, dst, sqr, b); case NPY_FLOAT32: return integral_middle<float>(src, dst, sqr, b); case NPY_FLOAT64: return integral_middle<double>(src, dst, sqr, b); default: PyErr_Format(PyExc_TypeError, "integral does not work on 'src' images of type %s", PyBlitzArray_TypenumAsString(src->type_num)); } return 0; BOB_CATCH_FUNCTION("in integral", 0) }
PyObject* PyBobIpBase_lbphs(PyObject*, PyObject* args, PyObject* kwds) { BOB_TRY /* Parses input arguments in a single shot */ char** kwlist = s_lbphs.kwlist(); PyBlitzArrayObject* input = 0,* output = 0; PyBobIpBaseLBPObject* lbp; blitz::TinyVector<int,2> size, overlap(0,0); if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O!(ii)|(ii)O&", kwlist, &PyBlitzArray_Converter, &input, &PyBobIpBaseLBP_Type, &lbp, &size[0], &size[1], &overlap[0], &overlap[1], &PyBlitzArray_OutputConverter, &output)) return 0; auto input_ = make_safe(input), output_ = make_xsafe(output); if (input->ndim != 2 || (output && output->ndim != 2)) { PyErr_Format(PyExc_TypeError, "lbphs images can only be computed from and to 2D arrays"); return 0; } if (output && output->type_num != NPY_UINT64){ PyErr_Format(PyExc_TypeError, "lbphs datatype must be uint64"); } if (!output){ // generate output in the desired shape auto res = lbphs_shape(input, lbp, size, overlap); Py_ssize_t osize[] = {res[0], res[1]}; output = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(NPY_UINT64, 2, osize); output_ = make_safe(output); } switch (input->type_num){ case NPY_UINT8: return lbphs_inner<uint8_t>(input, lbp, size, overlap, output); case NPY_UINT16: return lbphs_inner<uint16_t>(input, lbp, size, overlap, output); case NPY_FLOAT64: return lbphs_inner<double>(input, lbp, size, overlap, output); default: PyErr_Format(PyExc_TypeError, "lbphs does not work on 'input' images of type %s", PyBlitzArray_TypenumAsString(input->type_num)); } return 0; BOB_CATCH_FUNCTION("in lbphs", 0) }
PyObject* PyBobIpBase_gammaCorrection(PyObject*, PyObject* args, PyObject* kwargs) { BOB_TRY char** kwlist = s_gammaCorrection.kwlist(); PyBlitzArrayObject* src = 0,* dst = 0; double gamma; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&d|O&", kwlist, &PyBlitzArray_Converter, &src, &gamma, &PyBlitzArray_OutputConverter, &dst)) return 0; auto src_ = make_safe(src), dst_ = make_xsafe(dst); // perform some checks if (src->ndim != 2 || (dst && dst->ndim != 2)){ PyErr_Format(PyExc_ValueError, "'gamma_correction' can be performed on 2D arrays only"); return 0; } if (dst){ if (dst->ndim != 2 || dst->type_num != NPY_FLOAT64){ PyErr_Format(PyExc_TypeError, "'gamma_correction': ``dst`` must be a 2D array of type float"); return 0; } } else { dst = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(NPY_FLOAT64, 2, src->shape); dst_ = make_safe(dst); } switch (src->type_num){ case NPY_UINT8: return inner_gammaCorrection<uint8_t>(src, dst, gamma); case NPY_UINT16: return inner_gammaCorrection<uint16_t>(src, dst, gamma); case NPY_FLOAT64: return inner_gammaCorrection<double>(src, dst, gamma); default: PyErr_Format(PyExc_ValueError, "'gamma_correction' of %s arrays is currently not supported, only uint8, uint16 or float64 arrays are", PyBlitzArray_TypenumAsString(dst->type_num)); return 0; } BOB_CATCH_FUNCTION("in gamma_correction", 0) }
static PyObject* PyBobSpIDCT1D_Call (PyBobSpIDCT1DObject* self, PyObject* args, PyObject* kwds) { static const char* const_kwlist[] = {"input", "output", 0}; static char** kwlist = const_cast<char**>(const_kwlist); PyBlitzArrayObject* input = 0; PyBlitzArrayObject* output = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&", kwlist, &PyBlitzArray_Converter, &input, &PyBlitzArray_OutputConverter, &output )) return 0; //protects acquired resources through this scope auto input_ = make_safe(input); auto output_ = make_xsafe(output); if (input->type_num != NPY_FLOAT64) { PyErr_Format(PyExc_TypeError, "`%s' only supports 64-bit float arrays for input array `input'", Py_TYPE(self)->tp_name); return 0; } if (output && output->type_num != NPY_FLOAT64) { PyErr_Format(PyExc_TypeError, "`%s' only supports 64-bit float arrays for output array `output'", Py_TYPE(self)->tp_name); return 0; } if (input->ndim != 1) { PyErr_Format(PyExc_TypeError, "`%s' only accepts 1-dimensional arrays (not %" PY_FORMAT_SIZE_T "dD arrays)", Py_TYPE(self)->tp_name, input->ndim); return 0; } if (output && input->ndim != output->ndim) { PyErr_Format(PyExc_RuntimeError, "Input and output arrays should have matching number of dimensions, but input array `input' has %" PY_FORMAT_SIZE_T "d dimensions while output array `output' has %" PY_FORMAT_SIZE_T "d dimensions", input->ndim, output->ndim); return 0; } if (output && output->shape[0] != (Py_ssize_t)self->cxx->getLength()) { PyErr_Format(PyExc_RuntimeError, "1D `output' array should have %" PY_FORMAT_SIZE_T "d elements matching `%s' output size, not %" PY_FORMAT_SIZE_T "d elements", self->cxx->getLength(), Py_TYPE(self)->tp_name, output->shape[0]); return 0; } /** if ``output`` was not pre-allocated, do it now **/ if (!output) { Py_ssize_t length = self->cxx->getLength(); output = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(NPY_FLOAT64, 1, &length); output_ = make_safe(output); } /** all basic checks are done, can call the operator now **/ try { self->cxx->operator()(*PyBlitzArrayCxx_AsBlitz<double,1>(input), *PyBlitzArrayCxx_AsBlitz<double,1>(output)); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return 0; } catch (...) { PyErr_Format(PyExc_RuntimeError, "%s cannot operate on data: unknown exception caught", Py_TYPE(self)->tp_name); return 0; } return PyBlitzArray_NUMPY_WRAP(Py_BuildValue("O", output)); }
static PyObject* boostedMachine_forward( BoostedMachineObject* self, PyObject* args, PyObject* kwargs ) { // get list of arguments char* kwlist[] = {c("features"), c("predictions"), c("labels"), NULL}; PyBlitzArrayObject* p_features = 0,* p_predictions = 0,* p_labels = 0; if (!PyArg_ParseTupleAndKeywords( args, kwargs, "O&|O&O&", kwlist, &PyBlitzArray_Converter, &p_features, &PyBlitzArray_Converter, &p_predictions, &PyBlitzArray_Converter, &p_labels ) ) return NULL; auto _1 = make_safe(p_features), _2 = make_xsafe(p_predictions); try{ if (!p_predictions){ // uni-variate, single feature const auto features = PyBlitzArrayCxx_AsBlitz<uint16_t,1>(p_features, kwlist[0]); if (!features){ boostedMachine_forward_doc.print_usage(); PyErr_SetString(PyExc_TypeError, "When a single parameter is specified, only 1D arrays of type uint16 are supported."); return NULL; } return Py_BuildValue("d", self->base->forward(*features)); } if (p_features->type_num != NPY_UINT16){ boostedMachine_forward_doc.print_usage(); PyErr_SetString(PyExc_TypeError, "The parameter 'features' only supports 1D or 2D arrays of type uint16"); return NULL; } if (p_predictions->type_num != NPY_FLOAT64){ boostedMachine_forward_doc.print_usage(); PyErr_SetString(PyExc_TypeError, "The parameter 'predictions' only supports 1D or 2D arrays of type float"); return NULL; } if (p_labels && (p_labels->type_num != NPY_FLOAT64 || p_labels->ndim != p_predictions->ndim)){ boostedMachine_forward_doc.print_usage(); PyErr_SetString(PyExc_TypeError, "The parameter 'labels' only supports 1D or 2D arrays (same as 'predictions') of type float"); return NULL; } if (p_features->ndim == 1 && p_predictions->ndim == 1) _forward(self, p_features, p_predictions); else if (p_features->ndim == 2 && p_predictions->ndim == 1) _forward<2,1>(self, p_features, p_predictions, p_labels); else if (p_features->ndim == 2 && p_predictions->ndim == 2) _forward<2,2>(self, p_features, p_predictions, p_labels); else { boostedMachine_forward_doc.print_usage(); PyErr_Format(PyExc_TypeError, "The number of dimensions of %s (%d) and %s (%d) are not supported", kwlist[0], (int)p_features->ndim, kwlist[1], (int)p_predictions->ndim); return NULL; } Py_RETURN_NONE; } catch (std::exception& ex) { PyErr_SetString(PyExc_RuntimeError, ex.what()); return NULL; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot create new object of type `%s' - unknown exception thrown", Py_TYPE(self)->tp_name); return NULL; } }
PyObject* PyBobIpBase_histogram(PyObject*, PyObject* args, PyObject* kwargs) { BOB_TRY char** kwlist1 = s_histogram.kwlist(0); char** kwlist2 = s_histogram.kwlist(1); char** kwlist3 = s_histogram.kwlist(2); char** kwlist4 = s_histogram.kwlist(3); PyBlitzArrayObject* src = 0,* hist = 0; PyObject* min_max = 0; int bins = 0; auto src_ = make_xsafe(src), hist_ = make_xsafe(hist); // get the number of command line arguments Py_ssize_t nargs = (args?PyTuple_Size(args):0) + (kwargs?PyDict_Size(kwargs):0); switch (nargs){ case 1:{ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i", kwlist1, &PyBlitzArray_Converter, &src, &bins)) return 0; // get the number of bins src_ = make_safe(src); break; } case 2:{ PyObject* k = Py_BuildValue("s", kwlist1[1]); auto k_ = make_safe(k); if ((args && PyTuple_Size(args) == 2 && PyInt_Check(PyTuple_GET_ITEM(args,1))) || (kwargs && PyDict_Contains(kwargs, k))){ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i", kwlist1, &PyBlitzArray_Converter, &src, &bins)) return 0; } else { if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&", kwlist2, &PyBlitzArray_Converter, &src, &PyBlitzArray_OutputConverter, &hist)) return 0; } src_ = make_safe(src); hist_ = make_xsafe(hist); break; } case 3:{ // get values for min and max PyObject* k = Py_BuildValue("s", kwlist3[2]); auto k_ = make_safe(k); if ((args && PyTuple_Size(args) == 3 && PyInt_Check(PyTuple_GET_ITEM(args,2))) || (kwargs && PyDict_Contains(kwargs, k))){ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&Oi", kwlist3, &PyBlitzArray_Converter, &src, &min_max, &bins)) return 0; } else { if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO&", kwlist4, &PyBlitzArray_Converter, &src, &min_max, &PyBlitzArray_OutputConverter, &hist)) return 0; } src_ = make_safe(src); hist_ = make_xsafe(hist); break; } default: PyErr_Format(PyExc_ValueError, "'histogram' called with an unsupported number of arguments"); return 0; } // check input size if (src->ndim != 2){ PyErr_Format(PyExc_TypeError, "'histogram' : The input image must be 2D."); return 0; } // allocate the output, if needed bool return_out = false; if (!hist){ return_out = true; // first, get the number of bins if (!bins){ if (src->type_num == NPY_UINT8) bins = std::numeric_limits<uint8_t>::max() + 1; else if (src->type_num == NPY_UINT16) bins = std::numeric_limits<uint16_t>::max() + 1; else { PyErr_Format(PyExc_TypeError, "'histogram' : The given input data type %s is not supported, when no bin count is specified.", PyBlitzArray_TypenumAsString(src->type_num)); return 0; } } Py_ssize_t n[] = {bins}; hist = reinterpret_cast<PyBlitzArrayObject*>(PyBlitzArray_SimpleNew(NPY_UINT64, 1, n)); hist_ = make_safe(hist); } else { if (hist->type_num != NPY_UINT64){ PyErr_Format(PyExc_TypeError, "'histogram' : The given hist data type %s is not supported, only uint64 is allowed.", PyBlitzArray_TypenumAsString(src->type_num)); return 0; } } // now, get the histogram running bool res = true; if (min_max){ switch (src->type_num){ case NPY_UINT8: res = inner_histogram<uint8_t, 'B'>(src, hist, min_max); break; case NPY_UINT16: res = inner_histogram<uint16_t, 'H'>(src, hist, min_max); break; case NPY_UINT32: res = inner_histogram<uint32_t, 'I'>(src, hist, min_max); break; case NPY_UINT64: res = inner_histogram<uint64_t, 'K'>(src, hist, min_max); break; case NPY_INT8: res = inner_histogram<int8_t, 'b'>(src, hist, min_max); break; case NPY_INT16: res = inner_histogram<int16_t, 'h'>(src, hist, min_max); break; case NPY_INT32: res = inner_histogram<int32_t, 'i'>(src, hist, min_max); break; case NPY_INT64: res = inner_histogram<int64_t, 'L'>(src, hist, min_max); break; case NPY_FLOAT32: res = inner_histogram<float, 'f'>(src, hist, min_max); break; case NPY_FLOAT64: res = inner_histogram<double, 'd'>(src, hist, min_max); break; default: PyErr_Format(PyExc_TypeError, "'histogram' : The given input data type %s is not supported.", PyBlitzArray_TypenumAsString(src->type_num)); return 0; } } else { switch (src->type_num){ case NPY_UINT8: inner_histogram<uint8_t, 'B'>(src, hist); break; case NPY_UINT16: inner_histogram<uint16_t, 'H'>(src, hist); break; case NPY_UINT32: inner_histogram<uint32_t, 'I'>(src, hist); break; case NPY_UINT64: inner_histogram<uint64_t, 'K'>(src, hist); break; default: PyErr_Format(PyExc_TypeError, "'histogram' : The given input data type %s is not supported.", PyBlitzArray_TypenumAsString(src->type_num)); return 0; } } if (!res) return 0; // return the histogram, if wanted if (return_out){ return PyBlitzArray_AsNumpyArray(hist, 0); } else { Py_RETURN_NONE; } BOB_CATCH_FUNCTION("in histogram", 0) }
PyObject* PyBobIpBase_histogramEqualization(PyObject*, PyObject* args, PyObject* kwargs) { BOB_TRY char** kwlist1 = s_histogramEqualization.kwlist(0); char** kwlist2 = s_histogramEqualization.kwlist(1); PyBlitzArrayObject* src = 0,* dst = 0; // get the number of command line arguments Py_ssize_t nargs = (args?PyTuple_Size(args):0) + (kwargs?PyDict_Size(kwargs):0); switch (nargs){ case 1:{ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&", kwlist1, &PyBlitzArray_OutputConverter, &src)) return 0; break; } case 2:{ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&", kwlist2, &PyBlitzArray_Converter, &src, &PyBlitzArray_OutputConverter, &dst)) return 0; break; } default: PyErr_Format(PyExc_ValueError, "'histogram_equalization' called with an unsupported number of arguments"); return 0; } auto src_ = make_safe(src), dst_ = make_xsafe(dst); // perform some checks if (src->ndim != 2 || (dst && dst->ndim != 2)){ PyErr_Format(PyExc_ValueError, "'histogram_equalization' can be performed on 2D arrays only"); return 0; } // in-place switch (src->type_num){ case NPY_UINT8:{ if (!dst) return inner_histogramEq<uint8_t, uint8_t>(src, src); switch (dst->type_num){ case NPY_UINT8: return inner_histogramEq<uint8_t, uint8_t>(src, dst); case NPY_UINT16: return inner_histogramEq<uint8_t, uint16_t>(src, dst); case NPY_UINT32: return inner_histogramEq<uint8_t, uint32_t>(src, dst); case NPY_FLOAT32: return inner_histogramEq<uint8_t, float>(src, dst); case NPY_FLOAT64: return inner_histogramEq<uint8_t, double>(src, dst); default: PyErr_Format(PyExc_ValueError, "'histogram_equalization' can be performed to uint8, uint16, uint32, float32 or float64 arrays, but not to %s", PyBlitzArray_TypenumAsString(dst->type_num)); return 0; } } case NPY_UINT16:{ if (!dst) return inner_histogramEq<uint16_t, uint16_t>(src, src); switch (dst->type_num){ case NPY_UINT8: return inner_histogramEq<uint16_t, uint8_t>(src, dst); case NPY_UINT16: return inner_histogramEq<uint16_t, uint16_t>(src, dst); case NPY_UINT32: return inner_histogramEq<uint16_t, uint32_t>(src, dst); case NPY_FLOAT32: return inner_histogramEq<uint16_t, float>(src, dst); case NPY_FLOAT64: return inner_histogramEq<uint16_t, double>(src, dst); default: PyErr_Format(PyExc_ValueError, "'histogram_equalization' can be performed to uint8, uint16, uint32, float32 or float64 arrays, but not to %s", PyBlitzArray_TypenumAsString(dst->type_num)); return 0; } } default: PyErr_Format(PyExc_ValueError, "'histogram_equalization' can be performed on uint8 or uint16 images, but not on %s", PyBlitzArray_TypenumAsString(src->type_num)); } return 0; BOB_CATCH_FUNCTION("in histogram_equalization", 0) }
static PyObject* create_module (void) { # if PY_VERSION_HEX >= 0x03000000 PyObject* module = PyModule_Create(&module_definition); auto module_ = make_xsafe(module); const char* ret = "O"; # else PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr); const char* ret = "N"; # endif if (!module) return 0; if (PyModule_AddStringConstant(module, "__version__", BOB_EXT_MODULE_VERSION) < 0) return 0; if (!init_BobIpBaseGeomNorm(module)) return 0; if (!init_BobIpBaseFaceEyesNorm(module)) return 0; if (!init_BobIpBaseLBP(module)) return 0; if (!init_BobIpBaseLBPTop(module)) return 0; if (!init_BobIpBaseDCTFeatures(module)) return 0; if (!init_BobIpBaseTanTriggs(module)) return 0; if (!init_BobIpBaseGaussian(module)) return 0; if (!init_BobIpBaseMultiscaleRetinex(module)) return 0; if (!init_BobIpBaseWeightedGaussian(module)) return 0; if (!init_BobIpBaseSelfQuotientImage(module)) return 0; if (!init_BobIpBaseGaussianScaleSpace(module)) return 0; if (!init_BobIpBaseSIFT(module)) return 0; if (!init_BobIpBaseHOG(module)) return 0; if (!init_BobIpBaseGLCM(module)) return 0; if (!init_BobIpBaseWiener(module)) return 0; #if HAVE_VLFEAT if (!init_BobIpBaseVLFEAT(module)) return 0; #endif // HAVE_VLFEAT static void* PyBobIpBase_API[PyBobIpBase_API_pointers]; /* exhaustive list of C APIs */ /************** * Versioning * **************/ PyBobIpBase_API[PyBobIpBase_APIVersion_NUM] = (void *)&PyBobIpBase_APIVersion; /******************************** * Bindings for bob.ip.base.LBP * ********************************/ PyBobIpBase_API[PyBobIpBaseLBP_Type_NUM] = (void *)&PyBobIpBaseLBP_Type; PyBobIpBase_API[PyBobIpBaseLBP_Check_NUM] = (void *)&PyBobIpBaseLBP_Check; PyBobIpBase_API[PyBobIpBaseLBP_Converter_NUM] = (void *)&PyBobIpBaseLBP_Converter; #if PY_VERSION_HEX >= 0x02070000 /* defines the PyCapsule */ PyObject* c_api_object = PyCapsule_New((void *)PyBobIpBase_API, BOB_EXT_MODULE_PREFIX "." BOB_EXT_MODULE_NAME "._C_API", 0); #else PyObject* c_api_object = PyCObject_FromVoidPtr((void *)PyBobIpBase_API, 0); #endif if (!c_api_object) return 0; if (PyModule_AddObject(module, "_C_API", c_api_object) < 0) return 0; /* imports bob.ip.base's C-API dependencies */ if (import_bob_blitz() < 0) return 0; if (import_bob_core_random() < 0) return 0; if (import_bob_core_logging() < 0) return 0; if (import_bob_io_base() < 0) return 0; if (import_bob_sp() < 0) return 0; return Py_BuildValue(ret, module); }
static PyObject* create_module (void) { PyBobLearnActivation_Type.tp_new = PyType_GenericNew; if (PyType_Ready(&PyBobLearnActivation_Type) < 0) return 0; PyBobLearnIdentityActivation_Type.tp_base = &PyBobLearnActivation_Type; if (PyType_Ready(&PyBobLearnIdentityActivation_Type) < 0) return 0; PyBobLearnLinearActivation_Type.tp_base = &PyBobLearnActivation_Type; if (PyType_Ready(&PyBobLearnLinearActivation_Type) < 0) return 0; PyBobLearnLogisticActivation_Type.tp_base = &PyBobLearnActivation_Type; if (PyType_Ready(&PyBobLearnLogisticActivation_Type) < 0) return 0; PyBobLearnHyperbolicTangentActivation_Type.tp_base = &PyBobLearnActivation_Type; if (PyType_Ready(&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0; PyBobLearnMultipliedHyperbolicTangentActivation_Type.tp_base = &PyBobLearnActivation_Type; if (PyType_Ready(&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) return 0; # if PY_VERSION_HEX >= 0x03000000 PyObject* module = PyModule_Create(&module_definition); auto module_ = make_xsafe(module); const char* ret = "O"; # else PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr); const char* ret = "N"; # endif if (!module) return 0; /* register the types to python */ Py_INCREF(&PyBobLearnActivation_Type); if (PyModule_AddObject(module, "Activation", (PyObject *)&PyBobLearnActivation_Type) < 0) return 0; Py_INCREF(&PyBobLearnIdentityActivation_Type); if (PyModule_AddObject(module, "Identity", (PyObject *)&PyBobLearnIdentityActivation_Type) < 0) return 0; Py_INCREF(&PyBobLearnLinearActivation_Type); if (PyModule_AddObject(module, "Linear", (PyObject *)&PyBobLearnLinearActivation_Type) < 0) return 0; Py_INCREF(&PyBobLearnLogisticActivation_Type); if (PyModule_AddObject(module, "Logistic", (PyObject *)&PyBobLearnLogisticActivation_Type) < 0) return 0; Py_INCREF(&PyBobLearnHyperbolicTangentActivation_Type); if (PyModule_AddObject(module, "HyperbolicTangent", (PyObject *)&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0; Py_INCREF(&PyBobLearnMultipliedHyperbolicTangentActivation_Type); if (PyModule_AddObject(module, "MultipliedHyperbolicTangent", (PyObject *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) return 0; static void* PyBobLearnActivation_API[PyBobLearnActivation_API_pointers]; /* exhaustive list of C APIs */ /************** * Versioning * **************/ PyBobLearnActivation_API[PyBobLearnActivation_APIVersion_NUM] = (void *)&PyBobLearnActivation_APIVersion; /************************************************* * Bindings for bob.learn.activation.Activation * *************************************************/ PyBobLearnActivation_API[PyBobLearnActivation_Type_NUM] = (void *)&PyBobLearnActivation_Type; PyBobLearnActivation_API[PyBobLearnActivation_Check_NUM] = (void *)&PyBobLearnActivation_Check; PyBobLearnActivation_API[PyBobLearnActivation_NewFromActivation_NUM] = (void *)&PyBobLearnActivation_NewFromActivation; /*********************************************** * Bindings for bob.learn.activation.Identity * ***********************************************/ PyBobLearnActivation_API[PyBobLearnIdentityActivation_Type_NUM] = (void *)&PyBobLearnIdentityActivation_Type; /********************************************* * Bindings for bob.learn.activation.Linear * *********************************************/ PyBobLearnActivation_API[PyBobLearnLinearActivation_Type_NUM] = (void *)&PyBobLearnLinearActivation_Type; /*********************************************** * Bindings for bob.learn.activation.Logistic * ***********************************************/ PyBobLearnActivation_API[PyBobLearnLogisticActivation_Type_NUM] = (void *)&PyBobLearnLogisticActivation_Type; /******************************************************** * Bindings for bob.learn.activation.HyperbolicTangent * ********************************************************/ PyBobLearnActivation_API[PyBobLearnHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnHyperbolicTangentActivation_Type; /****************************************************************** * Bindings for bob.learn.activation.MultipliedHyperbolicTangent * ******************************************************************/ PyBobLearnActivation_API[PyBobLearnMultipliedHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type; #if PY_VERSION_HEX >= 0x02070000 /* defines the PyCapsule */ PyObject* c_api_object = PyCapsule_New((void *)PyBobLearnActivation_API, BOB_EXT_MODULE_PREFIX "." BOB_EXT_MODULE_NAME "._C_API", 0); #else PyObject* c_api_object = PyCObject_FromVoidPtr((void *)PyBobLearnActivation_API, 0); #endif if (c_api_object) PyModule_AddObject(module, "_C_API", c_api_object); /* imports dependencies */ if (import_bob_blitz() < 0) return 0; if (import_bob_core_logging() < 0) return 0; if (import_bob_io_base() < 0) return 0; return Py_BuildValue(ret, module); }
static PyObject* create_module (void) { # if PY_VERSION_HEX >= 0x03000000 PyObject* m = PyModule_Create(&module_definition); auto m_ = make_xsafe(m); const char* ret = "O"; # else PyObject* m = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr); const char* ret = "N"; # endif if (!m) return 0; /* register the types to python */ if (!init_BoostMt19937(m)) return 0; if (!init_BoostUniform(m)) return 0; if (!init_BoostNormal(m)) return 0; if (!init_BoostLogNormal(m)) return 0; if (!init_BoostGamma(m)) return 0; if (!init_BoostBinomial(m)) return 0; if (!init_BoostDiscrete(m)) return 0; static void* PyBobCoreRandom_API[PyBobCoreRandom_API_pointers]; /* exhaustive list of C APIs */ PyBobCoreRandom_API[PyBobCoreRandom_APIVersion_NUM] = (void *)&PyBobCoreRandom_APIVersion; /***************************************** * Bindings for bob.core.random.mt19937 * *****************************************/ PyBobCoreRandom_API[PyBoostMt19937_Type_NUM] = (void *)&PyBoostMt19937_Type; PyBobCoreRandom_API[PyBoostMt19937_Check_NUM] = (void *)PyBoostMt19937_Check; PyBobCoreRandom_API[PyBoostMt19937_Converter_NUM] = (void *)PyBoostMt19937_Converter; PyBobCoreRandom_API[PyBoostMt19937_SimpleNew_NUM] = (void *)PyBoostMt19937_SimpleNew; PyBobCoreRandom_API[PyBoostMt19937_NewWithSeed_NUM] = (void *)PyBoostMt19937_NewWithSeed; /***************************************** * Bindings for bob.core.random.uniform * *****************************************/ PyBobCoreRandom_API[PyBoostUniform_Type_NUM] = (void *)&PyBoostUniform_Type; PyBobCoreRandom_API[PyBoostUniform_Check_NUM] = (void *)PyBoostUniform_Check; PyBobCoreRandom_API[PyBoostUniform_Converter_NUM] = (void *)PyBoostUniform_Converter; PyBobCoreRandom_API[PyBoostUniform_SimpleNew_NUM] = (void *)PyBoostUniform_SimpleNew; /**************************************** * Bindings for bob.core.random.normal * ****************************************/ PyBobCoreRandom_API[PyBoostNormal_Type_NUM] = (void *)&PyBoostNormal_Type; PyBobCoreRandom_API[PyBoostNormal_Check_NUM] = (void *)PyBoostNormal_Check; PyBobCoreRandom_API[PyBoostNormal_Converter_NUM] = (void *)PyBoostNormal_Converter; PyBobCoreRandom_API[PyBoostNormal_SimpleNew_NUM] = (void *)PyBoostNormal_SimpleNew; /******************************************* * Bindings for bob.core.random.lognormal * *******************************************/ PyBobCoreRandom_API[PyBoostLogNormal_Type_NUM] = (void *)&PyBoostLogNormal_Type; PyBobCoreRandom_API[PyBoostLogNormal_Check_NUM] = (void *)PyBoostLogNormal_Check; PyBobCoreRandom_API[PyBoostLogNormal_Converter_NUM] = (void *)PyBoostLogNormal_Converter; PyBobCoreRandom_API[PyBoostLogNormal_SimpleNew_NUM] = (void *)PyBoostLogNormal_SimpleNew; /*************************************** * Bindings for bob.core.random.gamma * ***************************************/ PyBobCoreRandom_API[PyBoostGamma_Type_NUM] = (void *)&PyBoostGamma_Type; PyBobCoreRandom_API[PyBoostGamma_Check_NUM] = (void *)PyBoostGamma_Check; PyBobCoreRandom_API[PyBoostGamma_Converter_NUM] = (void *)PyBoostGamma_Converter; PyBobCoreRandom_API[PyBoostGamma_SimpleNew_NUM] = (void *)PyBoostGamma_SimpleNew; /****************************************** * Bindings for bob.core.random.binomial * ******************************************/ PyBobCoreRandom_API[PyBoostBinomial_Type_NUM] = (void *)&PyBoostBinomial_Type; PyBobCoreRandom_API[PyBoostBinomial_Check_NUM] = (void *)PyBoostBinomial_Check; PyBobCoreRandom_API[PyBoostBinomial_Converter_NUM] = (void *)PyBoostBinomial_Converter; PyBobCoreRandom_API[PyBoostBinomial_SimpleNew_NUM] = (void *)PyBoostBinomial_SimpleNew; /****************************************** * Bindings for bob.core.random.discrete * ******************************************/ PyBobCoreRandom_API[PyBoostDiscrete_Type_NUM] = (void *)&PyBoostDiscrete_Type; PyBobCoreRandom_API[PyBoostDiscrete_Check_NUM] = (void *)PyBoostDiscrete_Check; PyBobCoreRandom_API[PyBoostDiscrete_Converter_NUM] = (void *)PyBoostDiscrete_Converter; PyBobCoreRandom_API[PyBoostDiscrete_SimpleNew_NUM] = (void *)PyBoostDiscrete_SimpleNew; #if PY_VERSION_HEX >= 0x02070000 /* defines the PyCapsule */ PyObject* c_api_object = PyCapsule_New((void *)PyBobCoreRandom_API, BOB_EXT_MODULE_PREFIX "." BOB_EXT_MODULE_NAME "._C_API", 0); #else PyObject* c_api_object = PyCObject_FromVoidPtr((void *)PyBobCoreRandom_API, 0); #endif if (c_api_object) PyModule_AddObject(m, "_C_API", c_api_object); /* imports dependencies */ if (import_bob_blitz() < 0) return 0; return Py_BuildValue(ret, m); }
static PyObject* PyBobLearnMLPMachine_forward (PyBobLearnMLPMachineObject* self, PyObject* args, PyObject* kwds) { static const char* const_kwlist[] = {"input", "output", 0}; static char** kwlist = const_cast<char**>(const_kwlist); PyBlitzArrayObject* input = 0; PyBlitzArrayObject* output = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&", kwlist, &PyBlitzArray_Converter, &input, &PyBlitzArray_OutputConverter, &output )) return 0; //protects acquired resources through this scope auto input_ = make_safe(input); auto output_ = make_xsafe(output); if (input->type_num != NPY_FLOAT64) { PyErr_Format(PyExc_TypeError, "`%s' only supports 64-bit float arrays for input array `input'", Py_TYPE(self)->tp_name); return 0; } if (output && output->type_num != NPY_FLOAT64) { PyErr_Format(PyExc_TypeError, "`%s' only supports 64-bit float arrays for output array `output'", Py_TYPE(self)->tp_name); return 0; } if (input->ndim < 1 || input->ndim > 2) { PyErr_Format(PyExc_TypeError, "`%s' only accepts 1 or 2-dimensional arrays (not %" PY_FORMAT_SIZE_T "dD arrays)", Py_TYPE(self)->tp_name, input->ndim); return 0; } if (output && input->ndim != output->ndim) { PyErr_Format(PyExc_RuntimeError, "Input and output arrays should have matching number of dimensions, but input array `input' has %" PY_FORMAT_SIZE_T "d dimensions while output array `output' has %" PY_FORMAT_SIZE_T "d dimensions", input->ndim, output->ndim); return 0; } if (input->ndim == 1) { if (input->shape[0] != (Py_ssize_t)self->cxx->inputSize()) { PyErr_Format(PyExc_RuntimeError, "1D `input' array should have %" PY_FORMAT_SIZE_T "d elements matching `%s' input size, not %" PY_FORMAT_SIZE_T "d elements", self->cxx->inputSize(), Py_TYPE(self)->tp_name, input->shape[0]); return 0; } if (output && output->shape[0] != (Py_ssize_t)self->cxx->outputSize()) { PyErr_Format(PyExc_RuntimeError, "1D `output' array should have %" PY_FORMAT_SIZE_T "d elements matching `%s' output size, not %" PY_FORMAT_SIZE_T "d elements", self->cxx->outputSize(), Py_TYPE(self)->tp_name, output->shape[0]); return 0; } } else { if (input->shape[1] != (Py_ssize_t)self->cxx->inputSize()) { PyErr_Format(PyExc_RuntimeError, "2D `input' array should have %" PY_FORMAT_SIZE_T "d columns, matching `%s' input size, not %" PY_FORMAT_SIZE_T "d elements", self->cxx->inputSize(), Py_TYPE(self)->tp_name, input->shape[1]); return 0; } if (output && output->shape[1] != (Py_ssize_t)self->cxx->outputSize()) { PyErr_Format(PyExc_RuntimeError, "2D `output' array should have %" PY_FORMAT_SIZE_T "d columns matching `%s' output size, not %" PY_FORMAT_SIZE_T "d elements", self->cxx->outputSize(), Py_TYPE(self)->tp_name, output->shape[1]); return 0; } if (output && input->shape[0] != output->shape[0]) { PyErr_Format(PyExc_RuntimeError, "2D `output' array should have %" PY_FORMAT_SIZE_T "d rows matching `input' size, not %" PY_FORMAT_SIZE_T "d rows", input->shape[0], output->shape[0]); return 0; } } /** if ``output`` was not pre-allocated, do it now **/ if (!output) { Py_ssize_t osize[2]; if (input->ndim == 1) { osize[0] = self->cxx->outputSize(); } else { osize[0] = input->shape[0]; osize[1] = self->cxx->outputSize(); } output = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(NPY_FLOAT64, input->ndim, osize); if (!output) return 0; output_ = make_safe(output); } /** all basic checks are done, can call the machine now **/ try { if (input->ndim == 1) { self->cxx->forward_(*PyBlitzArrayCxx_AsBlitz<double,1>(input), *PyBlitzArrayCxx_AsBlitz<double,1>(output)); } else { auto bzin = PyBlitzArrayCxx_AsBlitz<double,2>(input); auto bzout = PyBlitzArrayCxx_AsBlitz<double,2>(output); blitz::Range all = blitz::Range::all(); for (int k=0; k<bzin->extent(0); ++k) { blitz::Array<double,1> i_ = (*bzin)(k, all); blitz::Array<double,1> o_ = (*bzout)(k, all); self->cxx->forward_(i_, o_); ///< no need to re-check } } } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return 0; } catch (...) { PyErr_Format(PyExc_RuntimeError, "%s cannot forward data: unknown exception caught", Py_TYPE(self)->tp_name); return 0; } return PyBlitzArray_NUMPY_WRAP(Py_BuildValue("O", output)); }