如何创建自己的手写数据集,例如IAM数据集

时间:2018-09-13 12:23:02

标签: tensorflow dataset

我需要创建自己的手写字符数据集,其格式类似于Iam Handwriting Database。我不知道如何像这样创建数据集,我需要您可以从其站点检查数据集格式,我需要创建data / ascii / words.txt和data / words /

1 个答案:

答案 0 :(得分:1)

没有创建IAM Handwriting Database的说明。但是您可以在这里找到:Build a Handwritten Text Recognition System using TensorFlow

import os
import numpy as np
import cv2

class DataProvider():
    "this class creates machine-written text for a word list. TODO: change getNext() to return your samples."

    def __init__(self, wordList):
        self.wordList = wordList
        self.idx = 0

    def hasNext(self):
        return self.idx < len(self.wordList)

    def getNext(self):
        img = np.ones((32, 128), np.uint8)*255
        word = self.wordList[self.idx]
        self.idx += 1
        cv2.putText(img,word,(2,20), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0), 1, cv2.LINE_AA)
        return (word, img)


def createIAMCompatibleDataset(dataProvider):
    "this function converts the passed dataset to an IAM compatible dataset"

    # create files and directories
    f = open('words.txt', 'w+')
    if not os.path.exists('sub'):
        os.makedirs('sub')
    if not os.path.exists('sub/sub-sub'):
        os.makedirs('sub/sub-sub')

    # go through data and convert it to IAM format
    ctr = 0
    while dataProvider.hasNext():
        sample = dataProvider.getNext()

        # write img
        cv2.imwrite('sub/sub-sub/sub-sub-%d.png'%ctr, sample[1])

        # write filename, dummy-values and text
        line = 'sub-sub-%d'%ctr + ' X X X X X X X ' + sample[0] + '\n'
        f.write(line)

        ctr += 1


if __name__ == '__main__':
    words = ['some', 'words', 'for', 'which', 'we', 'create', 'text-images']
    dataProvider = DataProvider(words)
    createIAMCompatibleDataset(dataProvider)

Harald Scheidl制作的源代码。