otuva / handwriting-synthesis

Handwriting Synthesis with RNNs ✏️ (Migrated to TensorFlow v2)
71 stars 32 forks source link

How to easily practice a new style using your own handwriting #7

Open Pedrotyag opened 1 year ago

Pedrotyag commented 1 year ago

How to easily train a new style using your own handwriting like in this repo "https://github.com/swechhasingh/Handwriting-synthesis", where you would give enough data to have your own handwriting as an answer.

otuva commented 1 year ago

@Pedrotyag I think samples can be primed with the given text for the desired effect rather than implementing IAM-OnDB data gathering process from scratch. I'll have to look into other peoples implementations

Pedrotyag commented 1 year ago

Perfect

Tombstone2K commented 1 year ago

Can you help me with that ? Priming the handwriting input rather than using IAM-OnDB from scratch

jopke commented 1 year ago

The priming of styles will help steer the model but the output won't necessarily match the style that was primed.

For style '1' this is what the priming strokes look like: prime

Here is the output of the same text from the model: output

Here is one I created. The prime: 14prime

The output: 14output

In this case the primes are quite different but the outputs look to be very similar styles. I plan to keep messing with it, hopefully I am missing something...

SiphoxMM commented 1 year ago

The priming of styles will help steer the model but the output won't necessarily match the style that was primed.

For style '1' this is what the priming strokes look like: prime

Here is the output of the same text from the model: output

Here is one I created. The prime: 14prime

The output: 14output

In this case the primes are quite different but the outputs look to be very similar styles. I plan to keep messing with it, hopefully I am missing something...

Hello, how did you go about creating your own style?

jopke commented 1 year ago

I reverse engineered the built in styles. There are 2 npy files per style, one is a text string and the other is the svg stroke data. I recommend getting the code to display the strokes for a known file before trying your own. Then create an svg of your handwriting with relative strokes. You will have to extract the stroke data and manipulate it a bit (y axis is flipped for example)

If I get around to it I will clean up my script and do a pull request but overall it isn’t too difficult.

Pedrotyag commented 1 year ago

I reverse engineered the built in styles. There are 2 npy files per style, one is a text string and the other is the svg stroke data. I recommend getting the code to display the strokes for a known file before trying your own. Then create an svg of your handwriting with relative strokes. You will have to extract the stroke data and manipulate it a bit (y axis is flipped for example)

If I get around to it I will clean up my script and do a pull request but overall it isn’t too difficult.

Congratulations, it seems complicated to me.

If you can, I'll be here to use it.

acmattson3 commented 12 months ago

I reverse engineered the built in styles. There are 2 npy files per style, one is a text string and the other is the svg stroke data. I recommend getting the code to display the strokes for a known file before trying your own. Then create an svg of your handwriting with relative strokes. You will have to extract the stroke data and manipulate it a bit (y axis is flipped for example)

If I get around to it I will clean up my script and do a pull request but overall it isn’t too difficult.

I would love to see your script. I have a program that can generate SVG's from raw stroke data also gathered by said program. I am trying to figure out the .npy format for strokes and characters but I'm unfamiliar with numpy. Even the non-cleaned-up version of the script would be very helpful to my project.

jopke commented 11 months ago

Here is the file in its current state. This will take an SVG, get the strokes, tweak the data to match what is expected and write out the numpy array. As I said before this file is very raw, basically just me hacking away to make it work. I have had to switch focus to a totally unrelated project but hopefully some day I'll get back to this and be able to make a more meaningful contribution.

When you create your svg, make the paths relative.

import numpy as np
import svgpathtools

paths, attributes = svgpathtools.svg2paths('img/sosays.svg')

# first we concat all of the stokes into a single string. Some SVG files will have multiple path elements.
strokes = ''
for k, v in enumerate(attributes):
    if 'd' in v:
        strokes = strokes + ' ' + v['d']

# consolidate the ',' and ' ' deliniation
strokes = strokes.replace(',',' ').split(' ')
print(strokes)
# build out our 3d array. First element is x, second is y and 3rd is the command (move M or line L) of the next line.
stroke_arr = []
for k, v in enumerate(strokes):
    v = v.strip()
    if not v: continue

    if v == 'M' or v == 'L' or v == 'm' or v == 'l':
        op = 0.0
        shifted_key = k+3 #this shift is because the command on this line is for the next set of coordinates
        if shifted_key < len(strokes):
            shifted_op = strokes[shifted_key].strip()
            op = 1.0 if shifted_op == 'M' or shifted_op == 'm' else 0.0
        # y is flipped so we multiple by -1
        stroke_arr.append([float(strokes[k+1]),-1*float(strokes[k+2]),float(op)])

print(stroke_arr)

# start the pen at 0,0
stroke_arr[0][0] = 0.0
stroke_arr[0][1] = 0.0

np.save("model/style/style-15-strokes.npy",np.array(stroke_arr))
ImNotOssy commented 5 months ago

Here is the file in its current state. This will take an SVG, get the strokes, tweak the data to match what is expected and write out the numpy array. As I said before this file is very raw, basically just me hacking away to make it work. I have had to switch focus to a totally unrelated project but hopefully some day I'll get back to this and be able to make a more meaningful contribution.

When you create your svg, make the paths relative.

import numpy as np
import svgpathtools

paths, attributes = svgpathtools.svg2paths('img/sosays.svg')

# first we concat all of the stokes into a single string. Some SVG files will have multiple path elements.
strokes = ''
for k, v in enumerate(attributes):
    if 'd' in v:
        strokes = strokes + ' ' + v['d']

# consolidate the ',' and ' ' deliniation
strokes = strokes.replace(',',' ').split(' ')
print(strokes)
# build out our 3d array. First element is x, second is y and 3rd is the command (move M or line L) of the next line.
stroke_arr = []
for k, v in enumerate(strokes):
    v = v.strip()
    if not v: continue

    if v == 'M' or v == 'L' or v == 'm' or v == 'l':
        op = 0.0
        shifted_key = k+3 #this shift is because the command on this line is for the next set of coordinates
        if shifted_key < len(strokes):
            shifted_op = strokes[shifted_key].strip()
            op = 1.0 if shifted_op == 'M' or shifted_op == 'm' else 0.0
        # y is flipped so we multiple by -1
        stroke_arr.append([float(strokes[k+1]),-1*float(strokes[k+2]),float(op)])

print(stroke_arr)

# start the pen at 0,0
stroke_arr[0][0] = 0.0
stroke_arr[0][1] = 0.0

np.save("model/style/style-15-strokes.npy",np.array(stroke_arr))

Hello, did you ever hop back in to this project? I'm very interested in the whole style creation process. I haven't had success myself, I think it has to do with the way my svg was created. What method did you use to digitize your handwriting.

jopke commented 1 month ago

I have not. I think if you really want to replicate your own writing you need to train a model specifically on it. There are better approaches being researched. https://mbzuai.ac.ae/news/transformers-of-the-handwritten-word/