Handle python bindings by alias to bytes type

This commit is contained in:
Dmitry Kurtaev 2025-06-02 18:27:53 +03:00
parent cb8d6c59a7
commit f4445bbe4a
6 changed files with 28 additions and 33 deletions

View File

@ -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;

View File

@ -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

View File

@ -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];
}

View File

@ -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

View File

@ -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}
};

View File

@ -265,6 +265,7 @@ _PREDEFINED_TYPES = (
export_name="ExtractMetaCallback",
required_modules=("gapi",)
),
PrimitiveTypeNode("NativeByteArray", "bytes"),
)
PREDEFINED_TYPES = dict(