Python utils.strLabelConverter() Examples

The following are 5 code examples of utils.strLabelConverter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module utils , or try the search function .
Example #1
Source File: main.py    From ICDAR-2019-SROIE with MIT License 6 votes vote down vote up
def predict_this_box(image, model, alphabet):
    converter = utils.strLabelConverter(alphabet)
    transformer = dataset.resizeNormalize((200, 32))
    image = transformer(image)
    if torch.cuda.is_available():
        image = image.cuda()
    image = image.view(1, *image.size())
    image = Variable(image)

    model.eval()
    preds = model(image)

    _, preds = preds.max(2)
    preds = preds.transpose(1, 0).contiguous().view(-1)

    preds_size = Variable(torch.IntTensor([preds.size(0)]))
    raw_pred = converter.decode(preds.data, preds_size.data, raw=True)
    sim_pred = converter.decode(preds.data, preds_size.data, raw=False)
    print('%-30s => %-30s' % (raw_pred, sim_pred))
    return sim_pred 
Example #2
Source File: predict.py    From ctpn-crnn with MIT License 6 votes vote down vote up
def crnn_recognition(cropped_image, model):
    converter = utils.strLabelConverter(alphabet)

    image = cropped_image.convert('L')

    ##
    # w = int(image.size[0] / (280 * 1.0 / 160))
    transformer = dataset.resizeNormalize((280, 32))
    image = transformer(image)
    # if torch.cuda.is_available():
    #     image = image.cuda()
    image = image.view(1, *image.size())
    image = Variable(image)

    model.eval()
    preds = model(image)

    _, preds = preds.max(2)
    preds = preds.transpose(1, 0).contiguous().view(-1)

    preds_size = Variable(torch.IntTensor([preds.size(0)]))
    sim_pred = converter.decode(preds.data, preds_size.data, raw=False)
    print('results: {0}'.format(sim_pred))
    return sim_pred 
Example #3
Source File: test_utils.py    From crnn.pytorch with MIT License 5 votes vote down vote up
def checkConverter(self):
        encoder = utils.strLabelConverter('abcdefghijklmnopqrstuvwxyz')

        # Encode
        # trivial mode
        result = encoder.encode('efa')
        target = (torch.IntTensor([5, 6, 1]), torch.IntTensor([3]))
        self.assertTrue(equal(result, target))

        # batch mode
        result = encoder.encode(['efa', 'ab'])
        target = (torch.IntTensor([5, 6, 1, 1, 2]), torch.IntTensor([3, 2]))
        self.assertTrue(equal(result, target))

        # Decode
        # trivial mode
        result = encoder.decode(
            torch.IntTensor([5, 6, 1]), torch.IntTensor([3]))
        target = 'efa'
        self.assertTrue(equal(result, target))

        # replicate mode
        result = encoder.decode(
            torch.IntTensor([5, 5, 0, 1]), torch.IntTensor([4]))
        target = 'ea'
        self.assertTrue(equal(result, target))

        # raise AssertionError
        def f():
            result = encoder.decode(
                torch.IntTensor([5, 5, 0, 1]), torch.IntTensor([3]))
        self.assertRaises(AssertionError, f)

        # batch mode
        result = encoder.decode(
            torch.IntTensor([5, 6, 1, 1, 2]), torch.IntTensor([3, 2]))
        target = ['efa', 'ab']
        self.assertTrue(equal(result, target)) 
Example #4
Source File: test_utils.py    From crnn with MIT License 5 votes vote down vote up
def checkConverter(self):
        encoder = utils.strLabelConverter('abcdefghijklmnopqrstuvwxyz')

        # Encode
        # trivial mode
        result = encoder.encode('efa')
        target = (torch.IntTensor([5, 6, 1]), torch.IntTensor([3]))
        self.assertTrue(equal(result, target))

        # batch mode
        result = encoder.encode(['efa', 'ab'])
        target = (torch.IntTensor([5, 6, 1, 1, 2]), torch.IntTensor([3, 2]))
        self.assertTrue(equal(result, target))

        # Decode
        # trivial mode
        result = encoder.decode(
            torch.IntTensor([5, 6, 1]), torch.IntTensor([3]))
        target = 'efa'
        self.assertTrue(equal(result, target))

        # replicate mode
        result = encoder.decode(
            torch.IntTensor([5, 5, 0, 1]), torch.IntTensor([4]))
        target = 'ea'
        self.assertTrue(equal(result, target))

        # raise AssertionError
        def f():
            result = encoder.decode(
                torch.IntTensor([5, 5, 0, 1]), torch.IntTensor([3]))
        self.assertRaises(AssertionError, f)

        # batch mode
        result = encoder.decode(
            torch.IntTensor([5, 6, 1, 1, 2]), torch.IntTensor([3, 2]))
        target = ['efa', 'ab']
        self.assertTrue(equal(result, target)) 
Example #5
Source File: test_utils.py    From basicOCR with GNU General Public License v3.0 5 votes vote down vote up
def checkConverter(self):
        encoder = utils.strLabelConverter('abcdefghijklmnopqrstuvwxyz')

        # Encode
        # trivial mode
        result = encoder.encode('efa')
        target = (torch.IntTensor([5, 6, 1]), torch.IntTensor([3]))
        self.assertTrue(equal(result, target))

        # batch mode
        result = encoder.encode(['efa', 'ab'])
        target = (torch.IntTensor([5, 6, 1, 1, 2]), torch.IntTensor([3, 2]))
        self.assertTrue(equal(result, target))

        # Decode
        # trivial mode
        result = encoder.decode(
            torch.IntTensor([5, 6, 1]), torch.IntTensor([3]))
        target = 'efa'
        self.assertTrue(equal(result, target))

        # replicate mode
        result = encoder.decode(
            torch.IntTensor([5, 5, 0, 1]), torch.IntTensor([4]))
        target = 'ea'
        self.assertTrue(equal(result, target))

        # raise AssertionError
        def f():
            result = encoder.decode(
                torch.IntTensor([5, 5, 0, 1]), torch.IntTensor([3]))
        self.assertRaises(AssertionError, f)

        # batch mode
        result = encoder.decode(
            torch.IntTensor([5, 6, 1, 1, 2]), torch.IntTensor([3, 2]))
        target = ['efa', 'ab']
        self.assertTrue(equal(result, target))