ocr中CTC解码相关

1.C++带有转置

cpp 复制代码
 result[id].score = ptr[id];

            //fprintf(stdout,"%d:%.2f\n",id,ptr[id]);
           // fprintf(stdout,"sigmod %d:%.2f\n",id,do_sigmod(ptr[id]));
        }
#define CTC_TIME_LENGTH 25
#define CTC_CHAR_NUMBER 82
        float plate_prob[CTC_TIME_LENGTH] = { 0.0f };
        int  plate_idx[CTC_TIME_LENGTH] = { 0 };

        for (int i = 0; i < CTC_TIME_LENGTH; i++)
        {
            for (int j = 0; j < CTC_CHAR_NUMBER; j++)
            {
                if (plate_prob[i] < *(ptr + j ))
                {
                    plate_prob[i] = *(ptr + j);
                    plate_idx[i] = j;
                }
            }
            ptr += CTC_CHAR_NUMBER;

        }

        for (int i = 0; i < CTC_TIME_LENGTH; i++)
        {
            printf("idx =%d,conf =%f\n", plate_idx[i], plate_prob[i]);
        }
        //ctc decode 
        int last_label = -1; // 记录上一个label值
        std::string str;
        std::vector<int> plateIdx;
        int plate_lenghth = 0;
        std::vector<float> plate_conf;
        for (int i = 0; i < CTC_TIME_LENGTH; i++)
        {
            int label = plate_idx[i];
            if (label != 0 && label != last_label) // 去除#或连续重复字符
            {
                plate_lenghth += 1;
                str += type_map[label - 1];
                plateIdx.push_back(label - 1);
                plate_conf.push_back(plate_prob[label]);
            }
            last_label = label;
        }
        printf("plate_number=%s\n", str.c_str());
        plateInfo.number = str;
        plateInfo.plate_length = plate_lenghth;
        for (int i = 0; i < plate_lenghth; i++)
        {
            plateInfo.conf.push_back(plate_conf[i]);
            plateInfo.plateIndex.push_back(plateIdx[i]);
        }

2.C++没有转置

cpp 复制代码
 //
#define CTC_TIME_LENGTH 24
#define CTC_CHAR_NUMBER 82

        float plate_prob[CTC_TIME_LENGTH] = { 0.0f };
        int  plate_idx[CTC_TIME_LENGTH] = { 0 };

        float* pdata0 = outData;
        for (int i = 0; i < CTC_TIME_LENGTH; i++)
        {
            for (int j = 0; j < CTC_CHAR_NUMBER; j++)
            {
                if (plate_prob[i] < *(pdata0 + j* CTC_TIME_LENGTH))
                {
                    plate_prob[i] = *(pdata0 + j* CTC_TIME_LENGTH); //pdata0 + j*CTC_TIME_LENGTH
                    plate_idx[i] = j;
                }
            }
            
            //pdata0+= CTC_CHAR_NUMBER;
            pdata0 ++;
        }

        //for(int i =0;i< CTC_TIME_LENGTH;i++)
       // printf("idx =%d,conf =%f\n", plate_idx[i], plate_prob[i]);

    //ctc decode 
        int last_label = -1; // 记录上一个label值
        std::string str;
        std::vector<int> plateIdx;
        int plate_lenghth = 0;
        std::vector<float> plate_conf;
        for (int i = 0; i < CTC_TIME_LENGTH; i++)
        {
            int label = plate_idx[i];
            if (label >1  && label != last_label) // 去除#或连续重复字符 //20231219
            {
                plate_lenghth += 1;
                str += type_map[label - 1];
                plateIdx.push_back(label - 1);
                plate_conf.push_back(plate_prob[label]);
            }
            last_label = label;
        }
        plateInfo.number = str;
        printf("plate_number=%s\n", str.c_str());
        plateInfo.plate_length = plate_lenghth;

3.python 简单版取最大索引

python 复制代码
def decodePlate_old(plateName,pred):  # 

    preds = np.argmax(pred[0], axis=1)  # 找出概率最大的那个字符的序号

    pre = 0
    newPreds = []
    plate_index = []
    conf = []
    for i in range(len(preds)):
        if preds[i] != 0 and preds[i] != pre:
            newPreds.append(preds[i])
            conf.append(pred[0][i][preds[i]])
        pre = preds[i]
    plate = ""
    for i in newPreds:
        plate += plateName[int(i) - 1]
        plate_index.append(int(i) - 1)

    return plate, plate_index, conf

4.python 复杂版

python 复制代码
def decodePlate(plateName,pred):

    preds=pred[0]
    max_conf =[]
    max_index=[]

    second_conf=[]
    second_index=[]

    for i in range(preds.shape[0]):
        index = np.argmax(preds[i])
        conf = np.max(preds[i])
        max_conf.append(conf)
        max_index.append(index)
        #second
        sorted_indices = np.argsort(preds[i])
        second_max_index = sorted_indices[len(preds[i]) - 2]
        second_conf.append(preds[i][second_max_index])
        second_index.append(second_max_index)

   
    pre = 0
    newPreds = []
    plate_index = []
    conf = []
#decode 有的会取第二个索引以及置信度
    for i in range(len(max_index)):
        if max_index[i] != 0 and max_index[i] != pre:
            if max_index[i]==1:
                newPreds.append(second_index[i])
                conf.append(second_conf[i])
            else:
                newPreds.append(max_index[i])
                conf.append(max_conf[i])
        pre = max_index[i]
    plate = ""
    for i in newPreds:
        plate += plateName[int(i) - 1]
        plate_index.append(int(i) - 1)

    return plate, plate_index, conf
相关推荐
野渡拾光1 小时前
【考研408数据结构-05】 串与KMP算法:模式匹配的艺术
数据结构·考研·算法
AI人工智能+1 小时前
一种融合AI与OCR的施工许可证识别技术,提升工程监管效率,实现自动化、精准化处理。
人工智能·自动化·ocr·施工许可证识别
tainshuai3 小时前
用 KNN 算法解锁分类的奥秘:从电影类型到鸢尾花开
算法·分类·数据挖掘
Coovally AI模型快速验证9 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
pusue_the_sun9 小时前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
RaymondZhao3410 小时前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
zhangfeng113310 小时前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类
啊阿狸不会拉杆11 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路11 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
你知道网上冲浪吗12 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析