Skip to content

Commit e6a0480

Browse files
author
sjvasquez
committed
interpolation
1 parent 0d976af commit e6a0480

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
data/raw/ascii
22
data/raw/lineStrokes
33
data/raw/original
4+
data/processed
45

56
logs
67
predictions

demo.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def _draw(self, strokes, lines, filename, stroke_colors=None, stroke_widths=None
159159
styles = [12 for i in lines]
160160

161161
hand.write(
162-
filename='img/all_star2.svg',
162+
filename='img/all_star.svg',
163163
lines=lines,
164164
biases=biases,
165165
styles=styles,
@@ -171,7 +171,7 @@ def _draw(self, strokes, lines, filename, stroke_colors=None, stroke_widths=None
171171
styles = np.cumsum(np.array([len(i) for i in lines]) == 0).astype(int)
172172

173173
hand.write(
174-
filename='img/downtown2.svg',
174+
filename='img/downtown.svg',
175175
lines=lines,
176176
biases=biases,
177177
styles=styles,
@@ -183,7 +183,7 @@ def _draw(self, strokes, lines, filename, stroke_colors=None, stroke_widths=None
183183
styles = [7 for i in lines]
184184

185185
hand.write(
186-
filename='img/give_up2.svg',
186+
filename='img/give_up.svg',
187187
lines=lines,
188188
biases=biases,
189189
styles=styles,

drawing.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import matplotlib.pyplot as plt
44
import numpy as np
55
from scipy.signal import savgol_filter
6+
from scipy.interpolate import interp1d
67

78

89
alphabet = [
@@ -93,6 +94,40 @@ def denoise(coords):
9394
return coords
9495

9596

97+
def interpolate(coords, factor=2):
98+
"""
99+
interpolates strokes using cubic spline
100+
"""
101+
coords = np.split(coords, np.where(coords[:, 2] == 1)[0] + 1, axis=0)
102+
new_coords = []
103+
for stroke in coords:
104+
105+
if len(stroke) == 0:
106+
continue
107+
108+
xy_coords = stroke[:, :2]
109+
110+
if len(stroke) > 3:
111+
f_x = interp1d(np.arange(len(stroke)), stroke[:, 0], kind='cubic')
112+
f_y = interp1d(np.arange(len(stroke)), stroke[:, 1], kind='cubic')
113+
114+
xx = np.linspace(0, len(stroke) - 1, factor*(len(stroke)))
115+
yy = np.linspace(0, len(stroke) - 1, factor*(len(stroke)))
116+
117+
x_new = f_x(xx)
118+
y_new = f_y(yy)
119+
120+
xy_coords = np.hstack([x_new.reshape(-1, 1), y_new.reshape(-1, 1)])
121+
122+
stroke_eos = np.zeros([len(xy_coords), 1])
123+
stroke_eos[-1] = 1.0
124+
stroke = np.concatenate([xy_coords, stroke_eos], axis=1)
125+
new_coords.append(stroke)
126+
127+
coords = np.vstack(new_coords)
128+
return coords
129+
130+
96131
def normalize(offsets):
97132
"""
98133
normalizes strokes to median unit norm
@@ -118,12 +153,22 @@ def offsets_to_coords(offsets):
118153
return np.concatenate([np.cumsum(offsets[:, :2], axis=0), offsets[:, 2:3]], axis=1)
119154

120155

121-
def draw(offsets, ascii_seq=None, align_strokes=True, denoise_strokes=True, save_file=None):
156+
def draw(
157+
offsets,
158+
ascii_seq=None,
159+
align_strokes=True,
160+
denoise_strokes=True,
161+
interpolation_factor=None,
162+
save_file=None
163+
):
122164
strokes = offsets_to_coords(offsets)
123165

124166
if denoise_strokes:
125167
strokes = denoise(strokes)
126168

169+
if interpolation_factor is not None:
170+
strokes = interpolate(strokes, factor=interpolation_factor)
171+
127172
if align_strokes:
128173
strokes[:, :2] = align(strokes[:, :2])
129174

img/banner.svg

+2
Loading

readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Handwriting Synthesis
2-
Implementation of the handwriting synthesis experiments in the paper <a href="https://arxiv.org/abs/1308.0850">Generating Sequences with Recurrent Neural Networks</a> by Alex Graves. The implementation very closely follows the original paper, with a few slight deviations, and the generated samples are of similar quality to those presented in the paper.
2+
![](img/banner.svg)
3+
Implementation of the handwriting synthesis experiments in the paper <a href="https://arxiv.org/abs/1308.0850">Generating Sequences with Recurrent Neural Networks</a> by Alex Graves. The implementation closely follows the original paper, with a few slight deviations, and the generated samples are of similar quality to those presented in the paper.
34

45
## Usage
56
```python
@@ -48,12 +49,11 @@ The following samples were generated with varying style and fixed bias. Each ve
4849
### Demo #3
4950
The following samples were generated with a fixed style and varying bias. Each verse has a lower bias than the previous, with the last verse being unbiased.
5051

51-
**Leonard Cohen – Hallelujah (<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">lyrics</a>)
52-
![](img/give_up.svg)**
52+
**Leonard Cohen – Hallelujah (<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">lyrics</a>)**
53+
![](img/give_up.svg)
5354

5455
## Contribute
55-
This project was intended to serve as a reference implementation for a research paper, but since the results are of decent quality, it may worthwile to make the project more broadly usable. I plan to continue focusing on the machine learning side of things. That said, I'd welcome contributors who can:
56+
This project was intended to serve as a reference implementation for a research paper, but since the results are of decent quality, it may be worthwile to make the project more broadly usable. I plan to continue focusing on the machine learning side of things. That said, I'd welcome contributors who can:
5657

5758
- Package this, and otherwise make it look more like a usable software project and less like research code.
5859
- Add support for more sophisticated drawing, animations, or anything else in this direction. Currently, the project only creates some simple svg files.
59-

0 commit comments

Comments
 (0)