-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
627 lines (533 loc) · 20.9 KB
/
index.html
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
<title>String Art</title>
</head>
<body>
<div class="container">
<div class="jumbotron" style="padding-bottom: 20px !important;">
<h1>String Art Machine</h1>
<br/><br/>
<div class="row">
<div class="col">
<div class="form-group">
<label for="numberOfLines">Number of Lines</label>
<input type="text" class="form-control" id="numberOfLines">
</div>
</div>
</div>
<label class="btn btn-primary btn-file">
Click Here and select an Image to Start <input type="file" id="fileInput" style="display: none;">
</label>
</div>
<div class="col text-left">
<!-- Hidden loading text, will be shown after file selection -->
<p id="loadingText" class="hidden">Loading...</p>
</div>
<div class="row">
<div class="col">
<div id="step1" class="inputoutput center hidden">
<img class="centerImage" id="imageSrc" alt="No Image" />
</div>
<div id="step2" class="inputoutput center hidden">
<div class="caption">Cropped and Grayscaled:</div>
<canvas class="centerCanvasMedium" id="canvasOutput" ></canvas>
</div>
<div id="step3" class="inputoutput center hidden">
<div class="caption">String Art Output:</div>
<div id="drawStatus"></div>
<canvas class="centerCanvasLarge" id="canvasOutput2" ></canvas>
</div>
</div>
</div>
<div class="row">
<div class="col text-left">
<button id="retryButton" class="btn btn-warning hidden" onclick="retry()">Retry</button>
</div>
<div class="col text-right">
<button id="proceedButton" class="btn btn-success hidden" onclick="proceed()">Proceed</button>
</div>
</div>
</div>
<script type="text/javascript">
let IMG_SIZE = 500;
let MAX_LINES = 4000;// 4000;
let N_PINS = 256;
let MIN_LOOP = 20;
let MIN_DISTANCE = 20;
let LINE_WEIGHT = 20;
let FILENAME = "";
let SCALE = 20;
let HOOP_DIAMETER = 0.625;
let img;
// set up element variables
let imgElement = document.getElementById("imageSrc")
let inputElement = document.getElementById("fileInput");
inputElement.addEventListener("change", (e) => {
imgElement.src = URL.createObjectURL(e.target.files[0]);
}, false);
var ctx=document.getElementById("canvasOutput").getContext("2d");
var ctx2=document.getElementById("canvasOutput2").getContext("2d");
let drawStatus = document.getElementById("drawStatus");
let numberOfLines = document.getElementById("numberOfLines");
const loadingText = document.getElementById("loadingText");
let length;
var R = {};
//pre initilization
let pin_coords;
let center;
let radius;
let line_cache_y;
let line_cache_x;
let line_cache_length;
let line_cache_weight;
//line variables
let error;
let img_result;
let result;
let line_mask;
let line_sequence;
let pin;
let thread_length;
let last_pins;
let listenForKeys = false;
window.onload = function() {
const esp32Url = 'http://192.168.4.1'; // Update this with the correct ESP32 URL
fetch(`${esp32Url}/status`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Check if the data contains progress
if (data.progress > 0) {
// Machine has existing state, redirect to control page
console.log('Existing state found, redirecting...');
window.location.href = 'control.html'; // Redirect to control page
} else {
console.log('No state found, staying on index page.');
}
})
.catch(error => {
console.error('Error checking machine state:', error);
});
};
//*******************************
// Line Generation
//*******************************
imgElement.onload = function() {
listenForKeys = false;
console.log(N_PINS) ;
inputElement.disabled = true ;
loadingText.classList.remove("hidden") ;
showStep(1);
// Take uploaded picture, square up and put on canvas
base_image = new Image();
base_image.src = imgElement.src;
ctx.canvas.width = IMG_SIZE;
ctx.canvas.height = IMG_SIZE;
ctx2.canvas.weight = IMG_SIZE * 2;
ctx2.canvas.height = IMG_SIZE * 2;
ctx.clearRect(0,0, IMG_SIZE, IMG_SIZE);
var selectedWidth = base_image.width;
var selectedHeight = base_image.height;
var xOffset = 0;
var yOffset = 0;
// square crop center of picture
if(base_image.height > base_image.width){
selectedWidth = base_image.width;
selectedHeight = base_image.width;
yOffset = Math.floor((base_image.height - base_image.width) / 2);
}else if(base_image.width > base_image.height) {
selectedWidth = base_image.height;
selectedHeight = base_image.height;
xOffset = Math.floor((base_image.width - base_image.height) / 2)
}
ctx.drawImage(base_image, xOffset, yOffset, selectedWidth, selectedHeight, 0, 0, IMG_SIZE, IMG_SIZE);
length = IMG_SIZE;
// make grayscale by averaging the RGB channels.
// extract out the R channel because that's all we need and push graysacle image onto canvas
var imgPixels = ctx.getImageData(0, 0, IMG_SIZE, IMG_SIZE);
R = img_result = nj.ones([IMG_SIZE, IMG_SIZE ]).multiply(0xff);
var rdata = [];
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
rdata.push(avg);
}
}
R.selection.data = rdata;
ctx.putImageData(imgPixels, 0, 0, 0, 0, IMG_SIZE, IMG_SIZE);
//circle crop canvas
ctx.globalCompositeOperation='destination-in';
ctx.beginPath();
ctx.arc(IMG_SIZE/2,IMG_SIZE/2, IMG_SIZE/2, 0, Math.PI*2);
ctx.closePath();
ctx.fill();
// start processing
NonBlockingCalculatePins();
}
function NonBlockingCalculatePins(){
// set up necessary variables
console.log("Calculating pins...");
status.textContent = "Calculating pins...";
pin_coords = [];
center = length / 2;
radius = length / 2 - 1/2
let i = 0;
(function codeBlock(){
if(i < N_PINS){
angle = 2 * Math.PI * i / N_PINS;
pin_coords.push([Math.floor(center + radius * Math.cos(angle)),
Math.floor(center + radius * Math.sin(angle))]);
i++;
setTimeout(codeBlock, 0);
} else {
console.log('Done Calculating pins');
status.textContent = "Done Calculating pins";
showStep(2);
NonBlockingPrecalculateLines();
}
})();
}
function NonBlockingPrecalculateLines(){
// set up necessary variables
console.log("Precalculating all lines...");
status.textContent = "Precalculating all lines...";
line_cache_y = Array.apply(null, {length: (N_PINS * N_PINS)});
line_cache_x = Array.apply(null, {length: (N_PINS * N_PINS)});
line_cache_length = Array.apply(null, {length: (N_PINS * N_PINS)}).map(Function.call, function(){return 0;});
line_cache_weight = Array.apply(null, {length: (N_PINS * N_PINS)}).map(Function.call, function(){return 1;});
let a = 0;
(function codeBlock(){
if(a < N_PINS){
for (b = a + MIN_DISTANCE; b < N_PINS; b++) {
x0 = pin_coords[a][0];
y0 = pin_coords[a][1];
x1 = pin_coords[b][0];
y1 = pin_coords[b][1];
d = Math.floor(Number(Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0)*(y1 - y0))));
xs = linspace(x0, x1, d);
ys = linspace(y0, y1, d);
line_cache_y[b*N_PINS + a] = ys;
line_cache_y[a*N_PINS + b] = ys;
line_cache_x[b*N_PINS + a] = xs;
line_cache_x[a*N_PINS + b] = xs;
line_cache_length[b*N_PINS + a] = d;
line_cache_length[a*N_PINS + b] = d;
}
a++;
setTimeout(codeBlock, 0);
} else {
console.log('Done Precalculating Lines');
status.textContent = "Done Precalculating Lines";
NonBlockingLineCalculator();
showStep(3);
}
})();
}
function NonBlockingLineCalculator(){
// set up necessary variables
console.log("Drawing Lines...");
status.textContent = "Drawing Lines...";
error = nj.ones([IMG_SIZE, IMG_SIZE]).multiply(0xff).subtract(nj.uint8(R.selection.data).reshape(IMG_SIZE, IMG_SIZE));
img_result = nj.ones([IMG_SIZE, IMG_SIZE ]).multiply(0xff);
result = nj.ones([IMG_SIZE * SCALE, IMG_SIZE * SCALE]).multiply(0xff);
result = new cv.matFromArray(IMG_SIZE * SCALE, IMG_SIZE * SCALE, cv.CV_8UC1, result.selection.data);
line_mask = nj.zeros([IMG_SIZE, IMG_SIZE], 'float64');
line_sequence = [];
pin = 0;
line_sequence.push(pin);
thread_length = 0;
last_pins = [];
let l = 0;
(function codeBlock(){
if(l < MAX_LINES){
if(l%10 == 0){
draw();
}
max_err = -1;
best_pin = -1;
for(offset=MIN_DISTANCE; offset < N_PINS - MIN_DISTANCE; offset++){
test_pin = (pin + offset) % N_PINS;
if(last_pins.includes(test_pin)){
continue;
}else {
xs = line_cache_x[test_pin * N_PINS + pin];
ys = line_cache_y[test_pin * N_PINS + pin];
line_err = getLineErr(error, ys, xs) * line_cache_weight[test_pin * N_PINS + pin];
if( line_err > max_err){
max_err = line_err;
best_pin = test_pin;
}
}
}
line_sequence.push(best_pin);
xs = line_cache_x[best_pin * N_PINS + pin];
ys = line_cache_y[best_pin * N_PINS + pin];
weight = LINE_WEIGHT * line_cache_weight[best_pin * N_PINS + pin];
line_mask = nj.zeros([IMG_SIZE, IMG_SIZE], 'float64');
line_mask = setLine(line_mask, ys, xs, weight);
error = subtractArrays(error, line_mask);
p = new cv.Point(pin_coords[pin][0] * SCALE, pin_coords[pin][1] * SCALE);
p2 = new cv.Point(pin_coords[best_pin][0] * SCALE, pin_coords[best_pin][1] * SCALE);
cv.line(result, p, p2, new cv.Scalar(0, 0, 0), 2, cv.LINE_AA, 0);
x0 = pin_coords[pin][0];
y0 = pin_coords[pin][1];
x1 = pin_coords[best_pin][0];
y1 = pin_coords[best_pin][1];
dist = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
thread_length += HOOP_DIAMETER / length * dist;
last_pins.push(best_pin);
if(last_pins.length > 20){
last_pins.shift();
}
pin = best_pin;
//update status
drawStatus.textContent = l + " Lines drawn | " + Math.round((l / MAX_LINES) * 100) + "% complete";
l++;
setTimeout(codeBlock, 0);
} else {
console.log('Done Drawing Lines');
Finalize();
}
})();
}
function draw(){
let dsize = new cv.Size(IMG_SIZE * 2, IMG_SIZE * 2);
let dst = new cv.Mat();
cv.resize(result, dst, dsize, 0, 0, cv.INTER_AREA);
cv.imshow('canvasOutput2', dst);
dst.delete();
}
function Finalize() {
let dsize = new cv.Size(IMG_SIZE * 2, IMG_SIZE * 2);
let dst = new cv.Mat();
cv.resize(result, dst, dsize, 0, 0, cv.INTER_AREA);
console.log("complete");
drawStatus.textContent = MAX_LINES + " Lines drawn | 100% complete";
loadingText.classList.add("hidden");
cv.imshow('canvasOutput2', dst);
console.log(line_sequence);
status.textContent = "Complete";
dst.delete(); result.delete();
window.scrollTo({ top: 5000, left: 0, behavior: 'smooth' });
document.getElementById('retryButton').classList.remove('hidden');
document.getElementById('proceedButton').classList.remove('hidden');
}
function retry() {
// Reload the page when the "Retry" button is clicked.
location.reload();
}
function proceed() {
const gcode = generateGCode() ;
console.log(gcode) ;
// Send the string to the ESP32 and redirect to a new webpage.
sendDataToESP32(gcode);
}
function sendDataToESP32(dataString) {
// Get the canvas output (final rendered image)
const canvas = document.getElementById("canvasOutput2");
const imageData = canvas.toDataURL("image/png"); // Get image data as Base64 string
const data = {
image: imageData, // The Base64 encoded image string
data: dataString,
};
// Send the image and additional data to the ESP32 server
fetch('http://192.168.4.1/upload', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => {
if (response.redirected) {
// If the response is a redirect, manually handle it
window.location.href = response.url; // This will redirect the browser to the control page
} else if (!response.ok) {
throw new Error('Failed to send data to ESP32');
}
return response.text(); // Read the response as text, if needed
})
.then(result => {
// Handle the response from the ESP32 if needed
console.log('Image and data successfully sent to ESP32:', result);
})
.catch(error => {
console.error('Error sending image and data to ESP32:', error);
});
}
function generateGCode() {
const yInVal = 28.0; // Desired position Y inside work area
const yOutVal = 0.0; // Desired position of Y outside work area
const pinCount = 256;
const radius = 11.75; // Radius of pins used to calculate thread length
let threadLength = 0.0; // The length of each line of string
let lineCount = 0; // Total count of lines
const radPerDelta = Math.PI / 128;
let fromPin = 0;
let gcode = "";
// Initial G-code setup
gcode += "G90\n"; // Absolute positioning mode
gcode += "G21\n"; // Metric mode
gcode += `G0 Y${yInVal.toFixed(2)}\n`; // Move the needle into the work
line_sequence.forEach((toPin, index) => {
if (index === 0) {
fromPin = toPin; // Set the starting pin
gcode += `G10 L20 P0 X${fromPin}\n`; // Set the current pin as the start pin
} else {
let delta = toPin - (fromPin % pinCount);
if (delta !== 0) {
const chordLen = 2.0 * radius * Math.sin((radPerDelta * Math.abs(delta)) / 2.0);
threadLength += chordLen;
}
gcode += `; ...Go from ${fromPin} to ${toPin}\n`;
// Ensure movement doesn't exceed half a turn
if (Math.abs(delta) < pinCount / 2) {
toPin = fromPin + delta;
} else {
if (delta > 0) {
toPin = fromPin - (pinCount - Math.abs(delta));
} else {
toPin = fromPin + (pinCount - (fromPin % pinCount)) + toPin;
}
}
gcode += pinWrapGCode(toPin, yInVal, yOutVal);
fromPin = toPin;
lineCount += 1;
}
});
return gcode;
// Helper function for pin wrapping G-code
function pinWrapGCode(pinNumber, yIn, yOut) {
let gcode = `G0 X${(pinNumber + 0.5).toFixed(2)} Y${yIn.toFixed(2)}\n`;
gcode += `G0 Y${yOut.toFixed(2)}\n`;
gcode += `G0 X${(pinNumber - 0.5).toFixed(2)}\n`;
gcode += `G0 Y${yIn.toFixed(2)}\n`;
return gcode;
}
}
function getLineErr(arr, coords1, coords2){
let result = new Uint8Array(coords1.length);
for(i=0;i<coords1.length;i++){
result[i] = arr.get(coords1[i], coords2[i]);
}
return getSum(result);
}
function setLine(arr, coords1, coords2, line){
for(i=0;i<coords1.length;i++){
arr.set(coords1[i], coords2[i], line);
}
return arr;
}
function compareMul(arr1, arr2){
let result = new Uint8Array(arr1.length);
for(i=0;i<arr1.length;i++){
result[i] = (arr1[i] < arr2[i]) * 254 + 1 ;
}
return result;
}
function compareAbsdiff(arr1, arr2){
let rsult = new Uint8Array(arr1.length);
for(i=0;i<arr1.length;i++){
rsult[i] = (arr1[i] * arr2[i]);
}
return rsult;
}
function subtractArrays(arr1, arr2) {
for(i=0; i<arr1.selection.data.length;i++){
arr1.selection.data[i] = arr1.selection.data[i] - arr2.selection.data[i]
if(arr1.selection.data[i] < 0){
arr1.selection.data[i] = 0;
}else if (arr1.selection.data[i] > 255){
arr1.selection.data[i] = 255;
}
}
return arr1;
}
function subtractArraysSimple(arr1, arr2) {
for(i=0; i<arr1.length;i++){
arr1[i] = arr1[i] - arr2[i];
}
return arr1;
}
function getSum(arr) {
let v = 0;
for(i=0;i<arr.length;i++){
v = v + arr[i];
}
return v;
}
function makeArr(startValue, stopValue, cardinality) {
var arr = [];
var currValue = startValue;
var step = (stopValue - startValue) / (cardinality - 1);
for (var i = 0; i < cardinality; i++) {
arr.push(Math.round(currValue + (step * i)));
}
return arr;
}
function AddRGB(arr1, arr2, arr3){
for(i=0;i<arr1.data.length;i++){
var avg = (arr1.data[i] + arr2.data[i] + arr3.data[i]);
arr1.data[i] = avg;
}
return arr1;
}
function linspace(a,b,n) {
if(typeof n === "undefined") n = Math.max(Math.round(b-a)+1,1);
if(n<2) { return n===1?[a]:[]; }
var i,ret = Array(n);
n--;
for(i=n;i>=0;i--) { ret[i] = Math.floor((i*b+(n-i)*a)/n); }
return ret;
}
function showStep(id){
let step1 = document.getElementById("step1");
let step2 = document.getElementById("step2");
let step3 = document.getElementById("step3");
switch (id){
case 1:
step1.classList.remove('hidden');
step2.classList.add('hidden');
step3.classList.add('hidden');
break;
case 2:
step1.classList.add('hidden');
step2.classList.add('hidden');
step3.classList.add('hidden');
break;
case 3:
step1.classList.add('hidden');
step2.classList.add('hidden');
step3.classList.remove('hidden');
break;
default:
break;
}
}
function onOpenCvReady() {
numberOfLines.value = MAX_LINES;
numberOfLines.addEventListener("keyup", function(event) {
MAX_LINES = parseInt(event.target.value);
});
}
</script>
<script async src="opencv.js" onload="onOpenCvReady();" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/gh/nicolaspanel/[email protected]/dist/numjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>