-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
194 lines (174 loc) · 5.22 KB
/
index.mjs
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
#!/usr/bin/env zx
import smartCrop from "./smartcrop.mjs";
import applescript from "applescript";
import util from "util";
const sourceDir = "./sourcefiles/";
const cropDir = "./image_processing/cropped/";
const noBGDir = "./image_processing/nobg/";
const optimizedDir = "./image_processing/optimized/";
const outputDir = "./output/";
let currentPlotTask;
const removeBackground = async filename => {
return $`rembg i ${sourceDir}${filename} ${noBGDir}${filename}`;
}
const createSVG = (sourceFile, filename) => {
const input = cropDir + sourceFile;
const output = outputDir + filename + '.svg';
return $`python3 ./linedraw-master/linedraw.py -i ${input} --output=${output} -nh --contour_simplify=3`;
};
const optimizeSVG = (filename, landscape) => {
return $`vpype read ${outputDir}${filename}.svg layout --fit-to-margins 0mm ${landscape ? '--landscape' : ''} 99x148mm \
linemerge --tolerance 0.1mm \
linesort \
reloop \
linesimplify \
write --page-size 100x148mm ${landscape ? '--landscape' : ''} ${optimizedDir}${filename}.svg`
}
const plotSVG = (filename) => {
return $`axicli ${optimizedDir}${filename}.svg -o outputfile.svg -L2`;
}
const cropFaces = filename => {
return smartCrop(`${noBGDir}${filename}`, `${cropDir}${filename}`);
}
const plotFirstInQueue = () => {
let isCancelled = false;
let plotSVGPromise = false;
const task = {
isDone: false,
_execute: async () => {
const firstFile = getQueue()[0];
if (firstFile) {
console.log(chalk.blue("Plotting " + firstFile));
const extension = path.extname(firstFile);
const filename = path.basename(firstFile, extension);
await removeBackground(firstFile);
const orientation = await cropFaces(firstFile)
if (isCancelled) return;
await createSVG(firstFile, filename)
if (isCancelled) return;
await optimizeSVG(filename, orientation.landscape);
if (isCancelled) return;
plotSVGPromise = plotSVG(filename);
await plotSVGPromise;
if (isCancelled) return;
await removeFromQueue(firstFile);
console.log(chalk.green("DONE"))
task.isDone = true;
} else {
console.log(chalk.yellow("NO FILE"))
}
},
cancel: async () => {
isCancelled = true;
if (plotSVGPromise) {
await plotSVGPromise.kill('SIGINT')
}
}
};
task._execute();
return task;
}
const calibrate = async () => {
fs.copyFileSync('./assets/calibrate/calibrate.svg', './output/calibrate.svg');
await optimizeSVG('calibrate', false);
await plotSVG('calibrate');
console.log(chalk.green("DONE CALIBRATING"));
}
const removeFromQueue = async (sourceFile) => {
await fs.remove(`${sourceDir}${sourceFile}`)
console.log(chalk.red("Removed " + sourceFile));
}
const listQueue = () => {
const files = getQueue();
console.table(files.map(f => ({ name: f, timestamp: fs.statSync(sourceDir + f).mtime.toLocaleString() })));
}
const getQueue = () => {
const files = fs.readdirSync(sourceDir);
const filtered = files.filter(file => path.basename(file) !== ".DS_Store");
filtered.sort(function (a, b) {
return fs.statSync(sourceDir + a).mtime.getTime() -
fs.statSync(sourceDir + b).mtime.getTime();
});
return filtered;
}
const cancelCurrentPlotTaskIfNeeded = async () => {
if (currentPlotTask && !currentPlotTask.isDone) {
await currentPlotTask.cancel();
}
currentPlotTask = false;
}
const delay = (ms) => new Promise(resolve => setTimeout(() => resolve(), ms));
const takePicture = async () => {
const execAppleScript = util.promisify(applescript.execString)
await execAppleScript(`activate application "Photo Booth"`)
await delay(1000)
await execAppleScript(`
tell application "System Events" to tell process "Photo Booth"
delay 0.2
keystroke return using command down
end tell
`)
await delay(5000)
const home = await $`echo $HOME`;
await $`cp -p "\`ls -dtr1 "${home}/Pictures/Photo Booth Library/Pictures"/* | tail -1\`" "./sourcefiles"`
await execAppleScript(`activate application "Visual Studio Code"`)
}
const run = async () => {
try {
const choice = await question(`What do you want?
${chalk.inverse('p')} - plot next in queue
${chalk.inverse('f')} - take a picture
${chalk.inverse('l')} - list queue
${chalk.inverse('t')} - toggle pen up/down
${chalk.inverse('d')} - disengage motors
${chalk.inverse('c')} - calibration plot
${chalk.inverse('q')} - quit
`, { choices: ["p", "l", "t", 'd', 'c', 'q'] })
switch (choice) {
case "p":
console.log("get next file");
await cancelCurrentPlotTaskIfNeeded();
currentPlotTask = plotFirstInQueue();
run();
break;
case "f":
console.log("take picture");
await takePicture();
run();
break;
case "l":
console.log("list files");
listQueue();
run();
break;
case "d":
console.log("disengage motors");
await cancelCurrentPlotTaskIfNeeded();
await $`axicli --mode align`;
run();
break;
case "t":
console.log("Toggle up/down");
await cancelCurrentPlotTaskIfNeeded();
await $`axicli --mode toggle`;
run();
break;
case "c":
console.log("Calibrate");
await cancelCurrentPlotTaskIfNeeded();
await calibrate();
run();
break;
case "q":
console.log("quit");
break;
default:
console.log('Invalid choice');
run();
break;
}
} catch (err) {
console.log("ERROR", err)
}
}
run()