我在C++工程中引用了paddleocr,但是识别出来的是乱码,但是在python下识别是没有问题的

brc7rcf0  于 4个月前  发布在  Python
关注(0)|答案(1)|浏览(81)

请提出你的问题 Please ask your question

`#include <Python.h>

void call_paddleocr() {
PyObject* pName, * pModule, * pDict, * pClass, * pInstance, * pFunc;
PyObject* pArgs, * pValue;

// 初始化Python解释器
Py_Initialize();

// 导入Python模块
pName = PyUnicode_DecodeFSDefault("paddleocr");
pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL) {
    // 获取模块中的字典
    pDict = PyModule_GetDict(pModule);

    // 获取PaddleOCR类
    pClass = PyDict_GetItemString(pDict, "PaddleOCR");
    if (pClass && PyCallable_Check(pClass)) {
        // 创建PaddleOCR实例
        pInstance = PyObject_CallObject(pClass, NULL);
        if (pInstance != NULL) {
            // 调用ocr方法
            pFunc = PyObject_GetAttrString(pInstance, "ocr");
            if (pFunc && PyCallable_Check(pFunc)) {
                pArgs = PyTuple_Pack(2, PyUnicode_FromString("H:\\1.png"), Py_True);
                pValue = PyObject_CallObject(pFunc, pArgs);
                Py_DECREF(pArgs);
                if (pValue != NULL) {
                    // 处理结果
                    PyObject* repr = PyObject_Repr(pValue);
                    const char* resultStr = PyUnicode_AsUTF8(repr);
                    printf("Result: %s\n", resultStr);
                    Py_DECREF(repr);
                    Py_DECREF(pValue);
                }
                else {
                    PyErr_Print();
                }
            }
            else {
                if (PyErr_Occurred())
                    PyErr_Print();
                fprintf(stderr, "Cannot find function 'ocr'\n");
            }
            Py_XDECREF(pFunc);
            Py_DECREF(pInstance);
        }
        else {
            PyErr_Print();
        }
    }
    else {
        if (PyErr_Occurred())
            PyErr_Print();
        fprintf(stderr, "Cannot find class 'PaddleOCR'\n");
    }
    Py_DECREF(pModule);
}
else {
    PyErr_Print();
    fprintf(stderr, "Failed to load 'paddleocr'\n");
}

// 结束Python解释器
Py_Finalize();

}

int main() {
call_paddleocr();
return 0;
}
`

相关问题