ocr中CTC解码相关

发布于:2024-12-18 ⋅ 阅读:(33) ⋅ 点赞:(0)

1.C++带有转置

 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++没有转置

 //
#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 简单版取最大索引

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 复杂版

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


网站公告

今日签到

点亮在社区的每一天
去签到