-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_collector.html
More file actions
294 lines (244 loc) · 9.04 KB
/
data_collector.html
File metadata and controls
294 lines (244 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>INKFORGE Handwriting Collector</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
margin-top: 0;
}
.prompt-box {
font-size: 1.2em;
margin: 20px 0;
padding: 15px;
background: #eef2ff;
border-radius: 6px;
color: #3730a3;
}
canvas {
border: 2px solid #cbd5e1;
border-radius: 4px;
cursor: crosshair;
background: white;
touch-action: none;
/* Prevent scrolling when drawing on touch devices */
}
.controls {
margin-top: 20px;
display: flex;
gap: 10px;
}
button {
padding: 10px 20px;
font-size: 1em;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
}
.btn-clear {
background: #ef4444;
color: white;
}
.btn-save {
background: #10b981;
color: white;
}
.btn-next {
background: #3b82f6;
color: white;
}
button:hover {
opacity: 0.9;
}
.setup {
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 2px solid #eee;
}
input {
padding: 8px;
font-size: 1em;
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>INKFORGE Handwriting Collector</h1>
<div class="setup">
<label><strong>Writer ID:</strong></label>
<input type="text" id="writerId" value="me" placeholder="e.g., me, friend1">
<span style="font-size:0.9em;color:#666;margin-left:10px;">Used to separate different handwriting
styles.</span>
</div>
<div>
<strong>Please write the following sentence:</strong>
<div class="prompt-box" id="promptText">The quick brown fox jumps over the lazy dog.</div>
</div>
<canvas id="canvas" width="760" height="200"></canvas>
<div class="controls">
<button class="btn-clear" onclick="clearCanvas()">Clear Canvas</button>
<button class="btn-save" onclick="saveData()">Save & Download</button>
<button class="btn-next" onclick="nextPrompt()">Next Sentence</button>
</div>
<div style="margin-top: 20px; font-size: 0.9em; color: #666;">
<strong>Instructions:</strong> Write on the canvas above using a mouse, stylus, or touch. Click "Save &
Download" when done. It will download an XML file of your strokes.
</div>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const writerInput = document.getElementById('writerId');
const promptText = document.getElementById('promptText');
let isDrawing = false;
let strokes = []; // Array of strokes. Each stroke is an array of points.
let currentStroke = [];
let startTime = 0;
let fileCounter = 0;
// A list of example sentences that cover a lot of letters.
const prompts = [
"The quick brown fox jumps over the lazy dog.",
"Pack my box with five dozen liquor jugs.",
"How vexingly quick daft zebras jump!",
"Sphinx of black quartz, judge my vow.",
"Two driven jocks help fax my big quiz.",
"A wizard's job is to vex chumps quickly in fog.",
"Hello world, this is my custom handwriting data!",
"Numbers test: 0 1 2 3 4 5 6 7 8 9."
];
let promptIndex = 0;
// Set up canvas style
ctx.lineWidth = 2;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.strokeStyle = '#000';
function getCoordinates(e) {
const rect = canvas.getBoundingClientRect();
if (e.touches && e.touches.length > 0) {
return {
x: e.touches[0].clientX - rect.left,
y: e.touches[0].clientY - rect.top
};
}
return {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
}
function startDrawing(e) {
e.preventDefault();
isDrawing = true;
currentStroke = [];
if (strokes.length === 0) startTime = Date.now();
const coords = getCoordinates(e);
ctx.beginPath();
ctx.moveTo(coords.x, coords.y);
currentStroke.push({ x: coords.x, y: coords.y, time: Date.now() - startTime });
}
function draw(e) {
if (!isDrawing) return;
e.preventDefault();
const coords = getCoordinates(e);
ctx.lineTo(coords.x, coords.y);
ctx.stroke();
currentStroke.push({ x: coords.x, y: coords.y, time: Date.now() - startTime });
}
function stopDrawing(e) {
if (!isDrawing) return;
e.preventDefault();
isDrawing = false;
if (currentStroke.length > 0) {
strokes.push(currentStroke);
}
}
// Event Listeners for Mouse
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
// Event Listeners for Touch/Stylus
canvas.addEventListener('touchstart', startDrawing, { passive: false });
canvas.addEventListener('touchmove', draw, { passive: false });
canvas.addEventListener('touchend', stopDrawing);
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
strokes = [];
}
function nextPrompt() {
promptIndex = (promptIndex + 1) % prompts.length;
promptText.innerText = prompts[promptIndex];
clearCanvas();
}
function generateXML() {
let xml = `<?xml version="1.0" encoding="UTF-8"?>\n<WhiteboardCapture>\n <StrokeSet>\n`;
for (const stroke of strokes) {
xml += ` <Stroke>\n`;
for (const pt of stroke) {
// Formatting to 2 decimal places to match IAM style sizes roughly
xml += ` <Point x="${Math.round(pt.x)}" y="${Math.round(pt.y)}" time="${pt.time}" />\n`;
}
xml += ` </Stroke>\n`;
}
xml += ` </StrokeSet>\n</WhiteboardCapture>`;
return xml;
}
function generateTranscription(lineId, text) {
// Simple transcription format the script understands:
// writer-form-line "transcription text"
return `${lineId} "${text}"\n`;
}
function downloadFile(filename, content, mimeType) {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function saveData() {
if (strokes.length === 0) {
alert("Canvas is empty! Please write something first.");
return;
}
const writer = writerInput.value.trim() || "me";
const formId = String(fileCounter).padStart(3, '0');
const lineId = `${writer}-${formId}-00`; // e.g., me-000-00
const text = promptText.innerText;
// 1. Generate XML
const xmlContent = generateXML();
downloadFile(`${lineId}.xml`, xmlContent, 'application/xml');
// 2. Generate Transcription Text
// Note: preprocess.py looks for writer-form.txt (e.g., me-000.txt)
const txtFilename = `${writer}-${formId}.txt`;
const txtContent = generateTranscription(lineId, text);
// Use a short timeout so the browser allows multiple downloads
setTimeout(() => {
downloadFile(txtFilename, txtContent, 'text/plain');
fileCounter++;
nextPrompt();
}, 500);
}
</script>
</body>
</html>