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
相关推荐
FirstFrost --sy1 小时前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
森焱森1 小时前
垂起固定翼无人机介绍
c语言·单片机·算法·架构·无人机
搂鱼1145142 小时前
(倍增)洛谷 P1613 跑路/P4155 国旗计划
算法
Yingye Zhu(HPXXZYY)2 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
无聊的小坏坏3 小时前
三种方法详解最长回文子串问题
c++·算法·回文串
长路 ㅤ   3 小时前
Java后端技术博客汇总文档
分布式·算法·技术分享·编程学习·java后端
秋说3 小时前
【PTA数据结构 | C语言版】两枚硬币
c语言·数据结构·算法
qq_513970444 小时前
力扣 hot100 Day37
算法·leetcode
不見星空4 小时前
leetcode 每日一题 1865. 找出和为指定值的下标对
算法·leetcode
我爱Jack4 小时前
时间与空间复杂度详解:算法效率的度量衡
java·开发语言·算法