<reponame>JinGyeSetBirdsFree/FudanOCR<gh_stars>10-100
import torch
import torch.nn as nn
import collections
import random
import numpy as np
import cv2
from faker import Faker
# from CNumber import cnumber

import datetime


class strLabelConverterForCTC(object):

    def __init__(self, alphabet):
        self.alphabet = alphabet + ' '  # for `-1` index

        self.dict = {}
        for i, char in enumerate(alphabet):
            # NOTE: 0 is reserved for 'blank' required by wrap_ctc
            self.dict[char] = i + 1

    def encode(self, text, depth=0, lower=True):
        """Support batch or single str."""
        if isinstance(text, str):
            if lower:
                text = text.lower()
            for char in text:
                # Fix the bug
                if self.alphabet.find(char) == -1:
                    print(char)
            text = [self.dict[char] for char in text]
            length = [len(text)]
        elif isinstance(text, collections.Iterable):
            length = [len(s) for s in text]
            text = ''.join(text)
            text, _ = self.encode(text)

        if depth:
            return text, len(text)
        return (torch.IntTensor(text), torch.IntTensor(length))

    def decode(self, t, length, raw=False):
        if length.numel() == 1:
            length = length[0]
            t = t[:length]
            if raw:
                return ''.join([self.alphabet[i - 1] for i in t])
            else:
                char_list = []
                for i in range(length):
                    if t[i] != 0 and (not (i > 0 and t[i - 1] == t[i])):
                        char_list.append(self.alphabet[t[i] - 1])
                return ''.join(char_list).replace(' ', '')
        else:
            texts = []
            index = 0
            for i in range(length.numel()):
                l = length[i]
                texts.append(self.decode(
                    t[index:index + l], torch.IntTensor([l]), raw=raw))
                index += l
            return texts
import torch
import torch.nn as nn
import collections
import random
import numpy as np
import cv2
from faker import Faker
# from CNumber import cnumber

import datetime


class strLabelConverterForCTC(object):

    def __init__(self, alphabet):
        self.alphabet = alphabet + ' '  # for `-1` index

        self.dict = {}
        for i, char in enumerate(alphabet):
            # NOTE: 0 is reserved for 'blank' required by wrap_ctc
            self.dict[char] = i + 1

    def encode(self, text, depth=0, lower=True):
        """Support batch or single str."""
        if isinstance(text, str):
            if lower:
                text = text.lower()
            for char in text:
                # Fix the bug
                if self.alphabet.find(char) == -1:
                    print(char)
            text = [self.dict[char] for char in text]
            length = [len(text)]
        elif isinstance(text, collections.Iterable):
            length = [len(s) for s in text]
            text = ''.join(text)
            text, _ = self.encode(text)

        if depth:
            return text, len(text)
        return (torch.IntTensor(text), torch.IntTensor(length))

    def decode(self, t, length, raw=False):
        if length.numel() == 1:
            length = length[0]
            t = t[:length]
            if raw:
                return ''.join([self.alphabet[i - 1] for i in t])
            else:
                char_list = []
                for i in range(length):
                    if t[i] != 0 and (not (i > 0 and t[i - 1] == t[i])):
                        char_list.append(self.alphabet[t[i] - 1])
                return ''.join(char_list).replace(' ', '')
        else:
            texts = []
            index = 0
            for i in range(length.numel()):
                l = length[i]
                texts.append(self.decode(
                    t[index:index + l], torch.IntTensor([l]), raw=raw))
                index += l
            return texts
# from CNumber import cnumber # for `-1` index # NOTE: 0 is reserved for 'blank' required by wrap_ctc Support batch or single str. # Fix the bug