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
相关推荐
kyle~11 小时前
排序---插入排序(Insertion Sort)
c语言·数据结构·c++·算法·排序算法
Boop_wu11 小时前
[数据结构] 队列 (Queue)
java·jvm·算法
hn小菜鸡12 小时前
LeetCode 3643.垂直翻转子矩阵
算法·leetcode·矩阵
ゞ 正在缓冲99%…13 小时前
leetcode101.对称二叉树
算法
max50060013 小时前
基于多元线性回归、随机森林与神经网络的农作物元素含量预测及SHAP贡献量分析
人工智能·python·深度学习·神经网络·随机森林·线性回归·transformer
YuTaoShao13 小时前
【LeetCode 每日一题】3000. 对角线最长的矩形的面积
算法·leetcode·职场和发展
2zcode13 小时前
基于Matlab可见光通信系统中OOK调制的误码率性能建模与分析
算法·matlab·php
纵有疾風起14 小时前
数据结构中的排序秘籍:从基础到进阶的全面解析
c语言·数据结构·算法·排序算法
纪元A梦14 小时前
贪心算法应用:推荐冷启动问题详解
算法·贪心算法
听风说雨的人儿14 小时前
腾讯面试题之编辑距离
算法