mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 17:44:04 +08:00
Handle python bindings by alias to bytes type
This commit is contained in:
parent
cb8d6c59a7
commit
f4445bbe4a
@ -73,6 +73,17 @@ public:
|
||||
*/
|
||||
CV_WRAP bool detectAndDecodeMulti(InputArray img, CV_OUT std::vector<std::string>& decoded_info, OutputArray points = noArray(),
|
||||
OutputArrayOfArrays straight_code = noArray()) const;
|
||||
|
||||
#ifdef OPENCV_BINDINGS_PARSER
|
||||
CV_WRAP_AS(detectAndDecodeBytes) NativeByteArray detectAndDecode(InputArray img, OutputArray points = noArray(),
|
||||
OutputArray straight_code = noArray()) const;
|
||||
CV_WRAP_AS(decodeBytes) NativeByteArray decode(InputArray img, InputArray points, OutputArray straight_code = noArray()) const;
|
||||
// CV_WRAP_AS(decodeBytesMulti) bool decodeMulti(InputArray img, InputArray points, CV_OUT std::vector<NativeByteArray>& decoded_info,
|
||||
// OutputArrayOfArrays straight_code = noArray()) const;
|
||||
// CV_WRAP_AS(detectAndDecodeBytesMulti) bool detectAndDecodeMulti(InputArray img, CV_OUT std::vector<NativeByteArray>& decoded_info, OutputArray points = noArray(),
|
||||
// OutputArrayOfArrays straight_code = noArray()) const;
|
||||
#endif
|
||||
|
||||
struct Impl;
|
||||
protected:
|
||||
Ptr<Impl> p;
|
||||
|
@ -7,32 +7,20 @@ typedef QRCodeEncoder::Params QRCodeEncoder_Params;
|
||||
typedef HOGDescriptor::HistogramNormType HOGDescriptor_HistogramNormType;
|
||||
typedef HOGDescriptor::DescriptorStorageFormat HOGDescriptor_DescriptorStorageFormat;
|
||||
|
||||
static PyObject* pyopencv_cv_GraphicalCodeDetector_detectAndDecode(PyObject* self, PyObject* py_args, PyObject* kw);
|
||||
|
||||
static PyObject* detectAndDecodeBytes(PyObject* self, PyObject* py_args, PyObject* kw) {
|
||||
// Run original method
|
||||
PyObject* retval = pyopencv_cv_GraphicalCodeDetector_detectAndDecode(self, py_args, kw);
|
||||
|
||||
if (PyErr_Occurred()) {
|
||||
PyObject *type = nullptr, *value = nullptr, *traceback = nullptr;
|
||||
PyErr_Fetch(&type, &value, &traceback);
|
||||
|
||||
PyObject* object = PyUnicodeDecodeError_GetObject(value);
|
||||
if (object && PyBytes_Check(object)) {
|
||||
PyTuple_SetItem(retval, 0, object);
|
||||
} else {
|
||||
PyErr_Restore(type, value, traceback);
|
||||
}
|
||||
} else {
|
||||
PyObject* str = PyTuple_GetItem(retval, 0);
|
||||
PyObject* bytes = PyUnicode_AsEncodedString(str, "utf-8", 0);
|
||||
PyTuple_SetItem(retval, 0, bytes);
|
||||
class NativeByteArray
|
||||
{
|
||||
public:
|
||||
inline NativeByteArray& operator=(const std::string& from) {
|
||||
val = from;
|
||||
return *this;
|
||||
}
|
||||
return retval;
|
||||
std::string val;
|
||||
};
|
||||
|
||||
template<>
|
||||
PyObject* pyopencv_from(const NativeByteArray& from)
|
||||
{
|
||||
return PyBytes_FromStringAndSize(from.val.c_str(), from.val.size());
|
||||
}
|
||||
|
||||
// TODO: copy docstring somehow
|
||||
#define PYOPENCV_EXTRA_METHODS_GraphicalCodeDetector \
|
||||
{"detectAndDecodeBytes", CV_PY_FN_WITH_KW_(detectAndDecodeBytes, 0), ""},
|
||||
|
||||
#endif
|
||||
|
@ -1000,7 +1000,7 @@ QRCodeDetector& QRCodeDetector::setEpsY(double epsY) {
|
||||
QRCodeEncoder::ECIEncodings QRCodeDetector::getEncoding(int codeIdx) {
|
||||
auto& encodings = std::dynamic_pointer_cast<ImplContour>(p)->encodings;
|
||||
CV_Assert(codeIdx >= 0);
|
||||
CV_Assert(codeIdx < encodings.size());
|
||||
CV_Assert(codeIdx < static_cast<int>(encodings.size()));
|
||||
return encodings[codeIdx];
|
||||
}
|
||||
|
||||
|
@ -739,16 +739,14 @@ bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info)
|
||||
template<>
|
||||
PyObject* pyopencv_from(const String& value)
|
||||
{
|
||||
PyObject* ret = PyString_FromString(value.empty() ? "" : value.c_str());
|
||||
return ret ? ret : Py_None;
|
||||
return PyString_FromString(value.empty() ? "" : value.c_str());
|
||||
}
|
||||
|
||||
#if CV_VERSION_MAJOR == 3
|
||||
template<>
|
||||
PyObject* pyopencv_from(const std::string& value)
|
||||
{
|
||||
PyObject* ret = PyString_FromString(value.empty() ? "" : value.c_str());
|
||||
return ret ? ret : Py_None;
|
||||
return PyString_FromString(value.empty() ? "" : value.c_str());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -133,9 +133,6 @@ static PyGetSetDef pyopencv_${name}_getseters[] =
|
||||
|
||||
static PyMethodDef pyopencv_${name}_methods[] =
|
||||
{
|
||||
#ifdef PYOPENCV_EXTRA_METHODS_${name}
|
||||
PYOPENCV_EXTRA_METHODS_${name}
|
||||
#endif
|
||||
${methods_inits}
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
@ -265,6 +265,7 @@ _PREDEFINED_TYPES = (
|
||||
export_name="ExtractMetaCallback",
|
||||
required_modules=("gapi",)
|
||||
),
|
||||
PrimitiveTypeNode("NativeByteArray", "bytes"),
|
||||
)
|
||||
|
||||
PREDEFINED_TYPES = dict(
|
||||
|
Loading…
Reference in New Issue
Block a user