Ejemplo n.º 1
0
/* Representation */
static PyObject* PyBlitzArray_repr(PyBlitzArrayObject* o) {
  switch (o->ndim) {
    case 1:
      return
        PyString_FromFormat
          ("%s(%" PY_FORMAT_SIZE_T "d,'%s')",
          Py_TYPE(o)->tp_name,
          o->shape[0],
          PyBlitzArray_TypenumAsString(o->type_num)
          );
    case 2:
      return
        PyString_FromFormat
          ("%s((%" PY_FORMAT_SIZE_T "d,%" PY_FORMAT_SIZE_T "d),'%s')",
          Py_TYPE(o)->tp_name,
          o->shape[0],
          o->shape[1],
          PyBlitzArray_TypenumAsString(o->type_num)
          );
    case 3:
      return
        PyString_FromFormat
          ("%s((%" PY_FORMAT_SIZE_T "d,%" PY_FORMAT_SIZE_T "d,%" PY_FORMAT_SIZE_T "d),'%s')",
          Py_TYPE(o)->tp_name,
          o->shape[0],
          o->shape[1],
          o->shape[2],
          PyBlitzArray_TypenumAsString(o->type_num)
          );
    case 4:
      return
        PyString_FromFormat
          ("%s((%" PY_FORMAT_SIZE_T "d,%" PY_FORMAT_SIZE_T "d,%" PY_FORMAT_SIZE_T "d,%" PY_FORMAT_SIZE_T "d),'%s')",
          Py_TYPE(o)->tp_name,
          o->shape[0],
          o->shape[1],
          o->shape[2],
          o->shape[3],
          PyBlitzArray_TypenumAsString(o->type_num)
          );
    default:
      return
        PyString_FromFormat
          ("[unsupported] %s(@%" PY_FORMAT_SIZE_T "d,'%s') %" PY_FORMAT_SIZE_T "d elements>",
          Py_TYPE(o)->tp_name,
          o->ndim,
          PyBlitzArray_TypenumAsString(o->type_num),
          PyBlitzArray_len(o)
          );
  }
}
Ejemplo n.º 2
0
/**
 * String representation and print out
 */
static PyObject* PyBoostDiscrete_Repr(PyBoostDiscreteObject* self) {

  PyObject* probabilities = PyBoostDiscrete_GetProbabilities(self);
  if (!probabilities) return 0;
  PyObject* prob_str = PYOBJECT_STR(probabilities);
  Py_DECREF(probabilities);
  if (!prob_str) return 0;

  PyObject* retval = PyUnicode_FromFormat(
      "%s(dtype='%s' , probabilities=%U)",
      Py_TYPE(self)->tp_name,
      PyBlitzArray_TypenumAsString(self->type_num),
      prob_str
      );
  Py_DECREF(prob_str);

#if PYTHON_VERSION_HEX < 0x03000000
  if (!retval) return 0;
  PyObject* tmp = PyObject_Str(retval);
  Py_DECREF(retval);
  retval = tmp;
#endif

  return retval;

}
Ejemplo n.º 3
0
PyObject* PyBobIpBase_zigzag(PyObject*, PyObject* args, PyObject* kwds) {
  BOB_TRY
  /* Parses input arguments in a single shot */
  char** kwlist = s_zigzag.kwlist();

  PyBlitzArrayObject* src = 0;
  PyBlitzArrayObject* dst = 0;
  PyObject* bf = 0;

  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&|O",
        kwlist,
        &PyBlitzArray_Converter, &src,
        &PyBlitzArray_OutputConverter, &dst,
        &bf)) return 0;
  auto src_ = make_safe(src);
  auto dst_ = make_safe(dst);

  if (src->type_num != dst->type_num) {
    PyErr_Format(PyExc_TypeError, "source and destination arrays must have the same data types (src: `%s' != dst: `%s')",
        PyBlitzArray_TypenumAsString(src->type_num),
        PyBlitzArray_TypenumAsString(dst->type_num));
    return 0;
  }

  if (src->ndim != 2) {
    PyErr_Format(PyExc_TypeError, "source array must have 2 dimensions types (src_given: `%" PY_FORMAT_SIZE_T "d' != src_expected: 2d')", src->ndim);
    return 0;
  }

  if (dst->ndim != 1) {
    PyErr_Format(PyExc_TypeError, "destination array must have 1 dimension type (dst_given: `%" PY_FORMAT_SIZE_T "d' != dst_expected: 1d')", dst->ndim);
    return 0;
  }

  switch (src->type_num) {
      return inner_zigzag<uint8_t>(src, dst, bf);
    case NPY_UINT16:
      return inner_zigzag<uint16_t>(src, dst, bf);
    case NPY_FLOAT64:
      return inner_zigzag<double>(src, dst, bf);
    default:
      PyErr_Format(PyExc_TypeError, "zigzag from `%s' (%d) is not supported", PyBlitzArray_TypenumAsString(src->type_num), src->type_num);
  }
  return 0;

  BOB_CATCH_FUNCTION("in zigzag", 0)
}
Ejemplo n.º 4
0
static int double2d_converter(PyObject *o, PyBlitzArrayObject **a) {
  if (PyBlitzArray_Converter(o, a) == 0)
    return 0;
  // in this case, *a is set to a new reference
  if ((*a)->type_num != NPY_FLOAT64 || (*a)->ndim != 2) {
    PyErr_Format(PyExc_TypeError,
                 "cannot convert blitz::Array<%s,%" PY_FORMAT_SIZE_T
                 "d> to a blitz::Array<double,2>",
                 PyBlitzArray_TypenumAsString((*a)->type_num), (*a)->ndim);
    return 0;
  }
  return 1;
}
Ejemplo n.º 5
0
static PyObject* PyBobLearnMLPTrainer_setBiasDerivativeOnLayer
(PyBobLearnMLPTrainerObject* self, PyObject* args, PyObject* kwds) {

  /* Parses input arguments in a single shot */
  static const char* const_kwlist[] = {"array", "layer", 0};
  static char** kwlist = const_cast<char**>(const_kwlist);

  PyBlitzArrayObject* array = 0;
  Py_ssize_t layer = 0;

  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&n", kwlist,
        &PyBlitzArray_Converter, &array, &layer)) return 0;

  if (array->type_num != NPY_FLOAT64 || array->ndim != 1) {
    PyErr_Format(PyExc_TypeError, "`%s.%s' only supports 1D 64-bit float arrays for argument `array' (or any other object coercible to that), but you provided an object with %" PY_FORMAT_SIZE_T "d dimensions and with type `%s' which is not compatible - check your input", Py_TYPE(self)->tp_name, s_set_bias_derivative_str, array->ndim, PyBlitzArray_TypenumAsString(array->type_num));
    return 0;
  }

  try {
    self->cxx->setBiasDerivative(*PyBlitzArrayCxx_AsBlitz<double,1>(array), layer);
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return 0;
  }
  catch (...) {
    PyErr_Format(PyExc_RuntimeError, "cannot set bias derivative at layer %" PY_FORMAT_SIZE_T "d for `%s': unknown exception caught", layer, Py_TYPE(self)->tp_name);
    return 0;
  }

  Py_RETURN_NONE;

}
Ejemplo n.º 6
0
static PyObject* PyBlitzArray_getitem(PyBlitzArrayObject* self,
    PyObject* item) {

  if (PyBob_NumberCheck(item)) {

    if (self->ndim != 1) {
      PyErr_Format(PyExc_TypeError, "expected tuple for accessing %" PY_FORMAT_SIZE_T "dD array", self->ndim);
      return 0;
    }

    // if you get to this point, the user has passed single number
    Py_ssize_t k = PyNumber_AsSsize_t(item, PyExc_IndexError);
    return PyBlitzArray_GetItem(self, &k);

  }

  if (PySequence_Check(item)) {

    if (self->ndim != PySequence_Fast_GET_SIZE(item)) {
      PyErr_Format(PyExc_TypeError, "expected tuple of size %" PY_FORMAT_SIZE_T "d for accessing %" PY_FORMAT_SIZE_T "dD array", self->ndim, self->ndim);
      return 0;
    }

    // if you get to this point, then the input tuple has the same size
    PyBlitzArrayObject shape;
    PyBlitzArrayObject* shape_p = &shape;
    if (!PyBlitzArray_IndexConverter(item, &shape_p)) return 0;
    return PyBlitzArray_GetItem(self, shape.shape);

  }

  PyErr_Format(PyExc_TypeError, "%s(@%" PY_FORMAT_SIZE_T "d,'%s') indexing requires a single integers (for 1D arrays) or sequences, for any rank size", Py_TYPE(self)->tp_name, self->ndim, PyBlitzArray_TypenumAsString(self->type_num));
  return 0;
}
Ejemplo n.º 7
0
static PyObject* PyBobIpFlandmark_locate(PyBobIpFlandmarkObject* self,  PyObject *args, PyObject* kwds) {
BOB_TRY
  char** kwlist = s_locate.kwlist();

  PyBlitzArrayObject* image;
  int bbx[4];

  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&iiii", kwlist,  &PyBlitzArray_Converter, &image, &bbx[0], &bbx[1], &bbx[2], &bbx[3])) return 0;

  // create bounding box in format (top, left, bottom, right)
  bbx[2] += bbx[0] - 1;
  bbx[3] += bbx[1] - 1;

  auto image_ = make_safe(image);

  // check
  if (image->type_num != NPY_UINT8 || image->ndim != 2) {
    PyErr_Format(PyExc_TypeError, "`%s' input `image' data must be a 2D array with dtype `uint8' (i.e. a gray-scaled image), but you passed a %" PY_FORMAT_SIZE_T "d array with data type `%s'", Py_TYPE(self)->tp_name, image->ndim, PyBlitzArray_TypenumAsString(image->type_num));
    return 0;
  }

  // detect
  std::vector<double> detected(2*self->flandmark->data.options.M);
  bob::ip::flandmark::flandmark_detect(*PyBlitzArrayCxx_AsBlitz<uint8_t, 2>(image), bbx, self->flandmark, &detected[0]);

  // extract landmarks
  blitz::Array<double, 2> landmarks(self->flandmark->data.options.M, 2);
  for (int k = 0; k < self->flandmark->data.options.M; ++k){
    landmarks(k,0) = detected[2*k];
    landmarks(k,1) = detected[2*k+1];
  }

  return PyBlitzArrayCxx_AsNumpy(landmarks);
BOB_CATCH_MEMBER("locate", 0)
};

static PyMethodDef PyBobIpFlandmark_methods[] = {
  {
    s_locate.name(),
    (PyCFunction)PyBobIpFlandmark_locate,
    METH_VARARGS|METH_KEYWORDS,
    s_locate.doc()
  },
  {0} /* Sentinel */
};

PyObject* PyBobIpFlandmark_Repr(PyBobIpFlandmarkObject* self) {

  /**
   * Expected output:
   *
   * <bob.ip.flandmark(model='...')>
   */

  PyObject* retval = PyUnicode_FromFormat("<%s(model='%s')>",  Py_TYPE(self)->tp_name, self->filename);

#if PYTHON_VERSION_HEX < 0x03000000
  if (!retval) return 0;
  PyObject* tmp = PyObject_Str(retval);
  Py_DECREF(retval);
  retval = tmp;
#endif

  return retval;

}
Ejemplo n.º 8
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)
}
Ejemplo n.º 9
0
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)
}
Ejemplo n.º 10
0
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)
}
Ejemplo n.º 11
0
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)
}
Ejemplo n.º 12
0
static inline PyObject* integral_middle(PyBlitzArrayObject* src, PyBlitzArrayObject* dst, PyBlitzArrayObject* sqr, bool b) {
  switch (dst->type_num){
    case NPY_INT8: return integral_inner<T1,int8_t>(src, dst, sqr, b);
    case NPY_INT16: return integral_inner<T1,int16_t>(src, dst, sqr, b);
    case NPY_INT32: return integral_inner<T1,int32_t>(src, dst, sqr, b);
    case NPY_INT64: return integral_inner<T1,int64_t>(src, dst, sqr, b);
    case NPY_UINT8: return integral_inner<T1,uint8_t>(src, dst, sqr, b);
    case NPY_UINT16: return integral_inner<T1,uint16_t>(src, dst, sqr, b);
    case NPY_UINT32: return integral_inner<T1,uint32_t>(src, dst, sqr, b);
    case NPY_UINT64: return integral_inner<T1,uint64_t>(src, dst, sqr, b);
    case NPY_FLOAT32: return integral_inner<T1,float>(src, dst, sqr, b);
    case NPY_FLOAT64: return integral_inner<T1,double>(src, dst, sqr, b);
    default:
      PyErr_Format(PyExc_TypeError, "integral does not work on 'dst' images of type %s", PyBlitzArray_TypenumAsString(dst->type_num));
  }
  return 0;
}
Ejemplo n.º 13
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)
}
Ejemplo n.º 14
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)
}
Ejemplo n.º 15
0
static int PyBobLearnMLPMachine_setBiases (PyBobLearnMLPMachineObject* self,
    PyObject* biases, void* /*closure*/) {

  if (PyBob_NumberCheck(biases)){
    double v = PyFloat_AsDouble(biases);
    if (PyErr_Occurred()) return -1;
    self->cxx->setBiases(v);
    return 0;
  }

  if (!PyIter_Check(biases) && !PySequence_Check(biases)) {
    PyErr_Format(PyExc_TypeError, "setting attribute `biases' of `%s' requires either a float or an iterable, but you passed `%s' which does not implement the iterator protocol", Py_TYPE(self)->tp_name, Py_TYPE(biases)->tp_name);
    return -1;
  }

  /* Checks and converts all entries */
  std::vector<blitz::Array<double,1> > biases_seq;
  std::vector<boost::shared_ptr<PyBlitzArrayObject>> biases_seq_;

  PyObject* iterator = PyObject_GetIter(biases);
  if (!iterator) return -1;
  auto iterator_ = make_safe(iterator);

  while (PyObject* item = PyIter_Next(iterator)) {
    auto item_ = make_safe(item);

    PyBlitzArrayObject* bz = 0;

    if (!PyBlitzArray_Converter(item, &bz)) {
      PyErr_Format(PyExc_TypeError, "`%s' could not convert object of type `%s' at position %" PY_FORMAT_SIZE_T "d of input sequence into an array - check your input", Py_TYPE(self)->tp_name, Py_TYPE(item)->tp_name, biases_seq.size());
      return -1;
    }

    if (bz->ndim != 1 || bz->type_num != NPY_FLOAT64) {
      PyErr_Format(PyExc_TypeError, "`%s' only supports 1D 64-bit float arrays for attribute `biases' (or any other object coercible to that), but at position %" PY_FORMAT_SIZE_T "d I have found an object with %" PY_FORMAT_SIZE_T "d dimensions and with type `%s' which is not compatible - check your input", Py_TYPE(self)->tp_name, biases_seq.size(), bz->ndim, PyBlitzArray_TypenumAsString(bz->type_num));
      Py_DECREF(bz);
      return -1;
    }

    biases_seq_.push_back(make_safe(bz)); ///< prevents data deletion
    biases_seq.push_back(*PyBlitzArrayCxx_AsBlitz<double,1>(bz)); ///< only a view!
  }

  if (PyErr_Occurred()) return -1;

  try {
    self->cxx->setBiases(biases_seq);
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
    PyErr_Format(PyExc_RuntimeError, "cannot reset `biases' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
    return -1;
  }

  return 0;

}