Beispiel #1
0
def main():
    parser = argparse.ArgumentParser(
        description='Converts the IAM-Offline dataset to a graves version.')
    parser.add_argument('input', help='The input dataset')
    parser.add_argument('output', help='The output dataset')
    parser.add_argument('--output-skeletons',
                        required=True,
                        help='An output folder for skeletonization results')
    args = parser.parse_args()
    print(args)

    iamOfflineDataset = IamOfflineDataset(args.input)

    if args.output_skeletons and not os.path.isdir(args.output_skeletons):
        os.makedirs(args.output_skeletons)

    skeletonDataset = dict()

    with Skeletonizer() as skeletonizer:
        for formId, formImg in tqdm(iamOfflineDataset.formsIterator()):
            outFile = os.path.join(args.output_skeletons, formId + '.png')
            outSkelFile = os.path.join(args.output_skeletons,
                                       formId + '_skel.png')

            skeletonBlurImg = None
            skeletonImg = None

            # Use cacheing if skeleton dir is provided
            if os.path.isfile(outFile):
                skeletonBlurImg = Image.open(outFile)
                if os.path.isfile(outSkelFile):
                    skeletonImg = Image.open(outSkelFile)
                    print('Found cached file: ' + outSkelFile)

            if not skeletonBlurImg:
                skeletonBlurImg = skeletonizer.skeletonize_blurred(formImg)
                skeletonBlurImg.save(outFile)

            if not skeletonImg:
                skeletonImgRaw = skeletonizer.skeletonize_sharp(
                    skeletonBlurImg)
                skeletonImg = Image.fromarray(
                    skeletonImgRaw.astype('uint8') * 255)
                skeletonImg.save(outSkelFile)

            skeletonDataset[formId] = outSkelFile

    outputDataset = ConvertedGravesDataset()

    def imgLoader(form_id):
        return Image.open(skeletonDataset[form_id])

    for (lineId, lineImg,
         lineText) in tqdm(iamOfflineDataset.linesIterator(imgLoader)):
        # print('SkeletonImg:', lineId, lineImg, lineText)
        penPositions = sample_to_penpositions(lineImg)
        outputDataset.addSample(penPositions, lineText)

    outputDataset.save(args.output)
def main():

    parser = argparse.ArgumentParser(
        description=
        'The first working version (without pen style transfer). Modifies the content of an image.'
    )
    parser.add_argument('--text-in', help='The input text', required=True)
    parser.add_argument('--text-out', help='The output text', required=True)
    parser.add_argument('input', help='The input file')
    args = parser.parse_args()
    print(args)

    inputImg = Image.open(args.input)

    with Skeletonizer() as skeletonizer:
        skeletonBlurImg = skeletonizer.skeletonize_blurred(inputImg)
        skeletonImg = skeletonizer.skeletonize_sharp(skeletonBlurImg)

    penPositions = sample_to_penpositions(skeletonImg)

    with GravesWriter() as writer:
        newPenPositions = writer.write(args.text_out, args.text_in,
                                       penPositions)

    newPenPositions = align(newPenPositions, penPositions)

    newSkeletonBlurImg, newSkeletonImg = render_skeleton(
        newPenPositions, inputImg.size)

    with PenStyleTransfer() as penStyleTransfer:
        outputImg = penStyleTransfer.transferStyle(newSkeletonBlurImg,
                                                   inputImg)

    print("Done. Displaying results ...")

    plt.figure('Full Pipeline', figsize=(16, 9))
    plt.subplot(3, 2, 1)
    plt.imshow(inputImg)
    plt.subplot(3, 2, 3)
    plt.imshow(skeletonBlurImg)
    plt.subplot(3, 2, 5)
    plt.imshow(skeletonImg, cmap='binary', vmax=10)
    plotPenPositions(penPositions)
    plt.subplot(3, 2, 6)
    plt.imshow(newSkeletonImg, cmap='binary', vmax=256 * 10)
    plotPenPositions(newPenPositions)
    plt.subplot(3, 2, 4)
    plt.imshow(newSkeletonBlurImg)
    plt.subplot(3, 2, 2)
    plt.imshow(outputImg)
    plt.show()
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser(
        description='Converts a dataset of skeletons to a graves dataset.')
    parser.add_argument('input', help='The input skeletons folder')
    parser.add_argument('output', help='The output folder')
    parser.add_argument(
        '--sort-by',
        default='mean',
        help=
        'The sorting order. Can be one of \'first\', \'last\', \'leftmost\', \'rightmost\', \'mean\'.'
    )
    args = parser.parse_args()
    print(args)

    fileNames = [
        f for f in os.listdir(args.input)
        if os.path.isfile(os.path.join(args.input, f))
    ]

    if not os.path.isdir(args.output):
        os.makedirs(args.output)

    for fileName in tqdm(fileNames):
        #print(fileName)
        strokeId = os.path.splitext(os.path.basename(fileName))[0]
        # print(strokeId, lineText)

        fullName = os.path.join(args.input, fileName)
        lineImg = ImageOps.invert(Image.open(fullName).convert('L'))

        # print('SkeletonImg:', lineId, lineImg, lineText)
        penPositions = sample_to_penpositions(lineImg, args.sort_by)

        svg = svgwrite.Drawing(os.path.join(args.output, strokeId + '.svg'),
                               profile='tiny',
                               size=(str(lineImg.width) + 'px',
                                     str(lineImg.height) + 'px'))
        svg.add(svg.rect(size=('100%', '100%'), fill='white'))

        render_penpositions_to_svg(penPositions, svg)

        svg.save()
Beispiel #4
0
def main():
    parser = argparse.ArgumentParser(
        description='Converts a dataset of skeletons to a graves dataset.')
    parser.add_argument('input', help='The input skeletons folder')
    parser.add_argument('labels', help='The text labels as .npy file')
    parser.add_argument('output', help='The output dataset')
    parser.add_argument(
        '--sort-by',
        default='mean',
        help=
        'The sorting order. Can be one of \'first\', \'last\', \'leftmost\', \'rightmost\', \'mean\'.'
    )
    args = parser.parse_args()
    print(args)

    fileNames = [
        f for f in os.listdir(args.input)
        if os.path.isfile(os.path.join(args.input, f))
    ]
    textAnnotations = np.load(args.labels, allow_pickle=True).item()

    outputDataset = ConvertedGravesDataset()

    for fileName in tqdm(fileNames):
        #print(fileName)
        strokeId = os.path.splitext(os.path.basename(fileName))[0]
        lineText = textAnnotations[strokeId]
        # print(strokeId, lineText)

        fullName = os.path.join(args.input, fileName)
        lineImg = ImageOps.invert(Image.open(fullName).convert('L'))

        # print('SkeletonImg:', lineId, lineImg, lineText)
        penPositions = sample_to_penpositions(lineImg, args.sort_by)
        outputDataset.addSample(penPositions, lineText)

    outputDataset.save(args.output)
def main():

    parser = argparse.ArgumentParser(description='The first working version (without pen style transfer). Modifies the content of an image.')
    parser.add_argument('--text-in', help='The input text', required=True)
    parser.add_argument('--text-out', help='The output text', required=True)
    parser.add_argument('input', help='The input file')
    parser.add_argument('--output', help='The output file')
    args = parser.parse_args()
    print(args)

    inputImg = Image.open(args.input)

    with ForegroundExtractor() as foregroundExtractor:
        foreground = foregroundExtractor.extract_foreground(inputImg)
        backgroundMask = foregroundExtractor.create_background_mask(foreground)
        #print(np.asarray(foreground) / 255.0)
        #print(np.asarray(backgroundMask))
        background_masked = (np.asarray(inputImg)/255.0) * backgroundMask

        background_img = Image.fromarray(np.maximum(0, np.minimum(255, background_masked * 255)).astype(np.uint8))
        background_img.save('tmp.png')
        foreground.save('tmp2.png')

    with BackgroundFiller() as backgroundFiller:
        background = backgroundFiller.fill(inputImg, backgroundMask)
        background.save('tmp3.png')

    with Skeletonizer() as skeletonizer:
        skeletonBlurImg = skeletonizer.skeletonize_blurred(inputImg)
        skeletonImg = skeletonizer.skeletonize_sharp(skeletonBlurImg)



    penPositions = sample_to_penpositions(skeletonImg)

    with GravesWriter() as writer:
        newPenPositions = writer.write(args.text_out, args.text_in, penPositions)

    newPenPositions = align(newPenPositions, penPositions)

    newSkeletonBlurImg, newSkeletonImg = render_skeleton(newPenPositions, inputImg.size)

    #with Colorizer() as colorizer:
    #    outputImg = colorizer.colorize(newSkeletonBlurImg)

    with PenStyleTransfer() as penStyleTransfer:
        outputImg = penStyleTransfer.transferStyle(newSkeletonBlurImg, foreground)

    blendedImg = blend(outputImg, background)

    print("Done. Displaying results ...")

    plt.figure('Full Pipeline', figsize=(16, 9))
    plt.subplot(3, 3, 1)
    plt.imshow(inputImg)

    plt.subplot(3, 3, 2)
    plt.imshow(foreground)
    plt.subplot(3, 3, 5)
    plt.imshow(background_masked)
    plt.subplot(3, 3, 8)
    plt.imshow(background)


    plt.subplot(3, 3, 4)
    plt.imshow(skeletonBlurImg)
    plt.subplot(3, 3, 7)
    plt.imshow(skeletonImg, cmap='binary', vmax=10)
    plotPenPositions(penPositions)
    plt.subplot(3, 3, 9)
    plt.imshow(newSkeletonImg, cmap='binary', vmax=256*10)
    plotPenPositions(newPenPositions)
    plt.subplot(3, 3, 6)
    plt.imshow(newSkeletonBlurImg)
    plt.subplot(3, 3, 3)
    plt.imshow(blendedImg)
    plt.show()

    if args.output:
        Image.fromarray(255 - skeletonImg.astype(np.uint8)*255).save(args.output)