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
相关推荐
强盛小灵通专卖员2 小时前
分类分割详细指标说明
人工智能·深度学习·算法·机器学习
IT猿手6 小时前
基于强化学习 Q-learning 算法求解城市场景下无人机三维路径规划研究,提供完整MATLAB代码
神经网络·算法·matlab·人机交互·无人机·强化学习·无人机三维路径规划
万能程序员-传康Kk9 小时前
旅游推荐数据分析可视化系统算法
算法·数据分析·旅游
PXM的算法星球9 小时前
【并发编程基石】CAS无锁算法详解:原理、实现与应用场景
算法
ll7788119 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
烨然若神人~9 小时前
算法第十七天|654. 最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
算法
爱coding的橙子9 小时前
每日算法刷题Day2 5.10:leetcode数组1道题3种解法,用时40min
算法·leetcode
程序媛小盐10 小时前
贪心算法:最小生成树
算法·贪心算法·图论
字节旅行10 小时前
大语言模型主流架构解析:从 Transformer 到 GPT、BERT
gpt·语言模型·transformer
Panesle10 小时前
分布式异步强化学习框架训练32B大模型:INTELLECT-2
人工智能·分布式·深度学习·算法·大模型