通过清心醉

c++调用RapidOCR-json加nlohmann/json.hpp进行本地图片的文字识别及文字所在的坐标获取

添加nlohmann/json的方法:参考https://www.qingxinzui.com/c%e6%b7%bb%e5%8a%a0nlohmann-json.html

#include <iostream>
using namespace std;
#include <string>  
#include <fstream>  
#include <sstream>  
 #include "nlohmann/json.hpp";
using json = nlohmann::json;


//执行CMD获取返回内容
string getCmdResult(const string& strCmd)
{
    char buf[1024] = { 0 };
    FILE* pf = NULL;
    string strResult;
    if ((pf = _popen(strCmd.c_str(), "r")) == NULL)
    {
        return strResult;
    }
    while (fgets(buf, sizeof buf, pf))
    {
        strResult += buf;
    }
    _pclose(pf);
    unsigned int iSize = strResult.size();
    if (iSize > 0 && strResult[iSize - 1] == '\n')  // linux
    {
        strResult = strResult.substr(0, iSize - 1);
    }
    return strResult;
}

std::string subreplace(std::string resource_str, std::string sub_str, std::string new_str)
{
    std::string dst_str = resource_str;
    std::string::size_type pos = 0;
    while ((pos = dst_str.find(sub_str)) != std::string::npos)   //替换所有指定子串
    {
        dst_str.replace(pos, sub_str.length(), new_str);
    }
    return dst_str;
}

int main() {
    string cmd = "RapidOCR-json.exe --image_path=1.png";
    string str;
    str = getCmdResult(cmd);    
    string del1 = "RapidOCR-json v1.1.0";
    string del2 = "OCR init completed.";
    string del3 = "\n";
    str = subreplace(str, del1, ""); //去除版本
    str = subreplace(str, del2, ""); //去除初始化
    str = subreplace(str, del3, ""); //去除\n换行符
    auto j = json::parse(str.c_str()); //这里注意,要创建auto的j变量
    int code = j.at("code");
    json data = j.at("data");
    if (code == 100) {
        for (auto item : data.items()) {
            auto arr = item.value();
            string text = arr.at("text"); //获取文字
            auto box = arr.at("box");
            cout << "text = " << text << endl;
            //cout << "box = " << box.dump() << endl;
            cout << "X1" << box[0] << "X2" << box[1] << "Y1" << box[2] << "Y2" <<box[3] << endl;
            //注意,X是从左往右计算的坐标,X1为文字对应左边上角开始的宽度,高度,X2为宽度结束所在的宽度
            //注意,Y是从右往左计算的坐标,Y1为文字对应右边下角开始对应宽度,高度,Y2为往左进的宽度
            //比如box是:X1[6,462]X[62,462]Yʼ[62,479]Y[6,479]
            //那么对应文字在图片上的坐标就是 x = 6 -62之间, y就是462-479
            //就是取x = box[0][0],box[1][0] ; y = box[0][1],box[2][1]
        }
    }
    else {
        cout << "识别JSON失败" << endl;
    }

    return 0;
}

关于作者

清心醉 administrator

发表评论

请输入验证码: