-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path04_Scan_TopCamera_Training_Dataset.js
More file actions
453 lines (412 loc) · 17.1 KB
/
Copy path04_Scan_TopCamera_Training_Dataset.js
File metadata and controls
453 lines (412 loc) · 17.1 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
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
/**
* 04: Scan the full predefined tray area with the Top camera for training data.
*
* Area:
* X 361 to 411
* Y 208 to 319
*
* Output:
* <OpenPnP config>/training_scans/<training_scan_id>/
* frames/*.png
* manifest.jsonl
* scan_info.json
*
* This script only captures images. It does not launch segmentation, CV,
* QA inspection, picking, dropping, or the halt-control GUI.
*/
try {
load(scripting.getScriptsDirectory().toString() + '/Examples/JavaScript/Utility.js');
}
catch (loadError) {
var scriptsDirectory = new java.io.File(scripting.getScriptsDirectory().toString());
load(new java.io.File(scriptsDirectory.getParentFile(), 'Examples/JavaScript/Utility.js').getAbsolutePath());
}
var imports = new JavaImporter(org.openpnp.model, java.io, javax.imageio);
with (imports) {
var scriptsRootDir = new File(scripting.getScriptsDirectory().toString());
var scriptsDir = scriptsRootDir.getName() === 'BugPicker'
? scriptsRootDir
: new File(scriptsRootDir, 'BugPicker');
if (!scriptsDir.exists()) {
scriptsDir = scriptsRootDir;
}
var projectDir = scriptsRootDir.getName() === 'BugPicker'
&& scriptsRootDir.getParentFile() !== null
&& scriptsRootDir.getParentFile().getName() === 'scripts'
? scriptsRootDir.getParentFile().getParentFile()
: scriptsRootDir.getParentFile();
function pad(number, width) {
var text = String(number);
while (text.length < width) {
text = '0' + text;
}
return text;
}
function timestamp() {
var now = new Date();
return now.getFullYear()
+ pad(now.getMonth() + 1, 2)
+ pad(now.getDate(), 2)
+ '_'
+ pad(now.getHours(), 2)
+ pad(now.getMinutes(), 2)
+ pad(now.getSeconds(), 2);
}
function positions(start, stop, step, descending) {
var values = [];
var epsilon = 0.000001;
if (descending) {
for (var value = start; value >= stop + epsilon; value -= step) {
values.push(value);
}
if (values.length === 0 || Math.abs(values[values.length - 1] - stop) > epsilon) {
values.push(stop);
}
}
else {
for (var value = start; value <= stop - epsilon; value += step) {
values.push(value);
}
if (values.length === 0 || Math.abs(values[values.length - 1] - stop) > epsilon) {
values.push(stop);
}
}
return values;
}
function jsonLine(frameIndex, fileName, x, y, requestedX, requestedY, width, height, unitsPerPixel) {
var record = {
frame_index: frameIndex,
file_name: fileName,
camera: 'Top',
x_mm: x,
y_mm: y,
requested_x_mm: requestedX,
requested_y_mm: requestedY,
image_width_px: width,
image_height_px: height,
units_per_pixel_x_mm: unitsPerPixel.x,
units_per_pixel_y_mm: unitsPerPixel.y
};
return JSON.stringify(record) + '\n';
}
function writeText(file, text) {
var writer = new FileWriter(file);
try {
writer.write(text);
}
finally {
writer.close();
}
}
function readText(file) {
var reader = new BufferedReader(new FileReader(file));
var lines = [];
try {
var line = reader.readLine();
while (line !== null) {
lines.push(String(line));
line = reader.readLine();
}
}
finally {
reader.close();
}
return lines.join('\n');
}
function readNumber(record, key, fallback) {
if (record[key] === undefined || record[key] === null || record[key] === '') {
return fallback;
}
var value = Number(record[key]);
if (isNaN(value)) {
throw new Error('Calibration value is not numeric: ' + key + '=' + record[key]);
}
return value;
}
function loadTrainingTrayCalibration(defaults) {
var localCalibrationFile = new File(scriptsDir, 'training_tray_calibration.json');
var controlCalibrationFile = new File(projectDir, 'control/training_tray_calibration.json');
var calibrationFile = localCalibrationFile.exists() ? localCalibrationFile : controlCalibrationFile;
var calibration = {
xLeft: defaults.xLeft,
xRight: defaults.xRight,
yTop: defaults.yTop,
yBottom: defaults.yBottom,
cameraXOffsetMm: defaults.cameraXOffsetMm,
cameraYOffsetMm: defaults.cameraYOffsetMm,
scanBoundsAreCameraCoordinates: defaults.scanBoundsAreCameraCoordinates,
xStepMm: defaults.xStepMm,
yStepMm: defaults.yStepMm,
source: 'built-in defaults'
};
if (!calibrationFile.exists()) {
return calibration;
}
var record = JSON.parse(readText(calibrationFile));
calibration.xLeft = readNumber(record, 'x_left_mm', calibration.xLeft);
calibration.xRight = readNumber(record, 'x_right_mm', calibration.xRight);
calibration.yTop = readNumber(record, 'y_top_mm', calibration.yTop);
calibration.yBottom = readNumber(record, 'y_bottom_mm', calibration.yBottom);
calibration.cameraXOffsetMm = readNumber(record, 'camera_x_offset_mm', calibration.cameraXOffsetMm);
calibration.cameraYOffsetMm = readNumber(record, 'camera_y_offset_mm', calibration.cameraYOffsetMm);
calibration.scanBoundsAreCameraCoordinates = record.scan_bounds_are_camera_coordinates === undefined
? calibration.scanBoundsAreCameraCoordinates
: Boolean(record.scan_bounds_are_camera_coordinates);
calibration.xStepMm = readNumber(record, 'x_step_mm', calibration.xStepMm);
calibration.yStepMm = readNumber(record, 'y_step_mm', calibration.yStepMm);
calibration.source = calibrationFile.getAbsolutePath();
return calibration;
}
function writeStatus(statusFile, status, scanId, frameIndex, totalFrames, message) {
var record = {
status: status,
scan_id: scanId,
frame_index: frameIndex,
total_frames: totalFrames,
message: message,
updated_at: new Date().toISOString()
};
writeText(statusFile, JSON.stringify(record, null, 2) + '\n');
}
function formatLocation(location) {
return 'X=' + location.x.toFixed(3)
+ ' Y=' + location.y.toFixed(3)
+ ' Z=' + location.z.toFixed(3)
+ ' R=' + location.rotation.toFixed(3);
}
function getUnitsPerPixelForCurrentZ(camera) {
try {
return camera.getUnitsPerPixelAtZ();
}
catch (error) {
return camera.getUnitsPerPixel();
}
}
function findCameraByName(name) {
function cameraMatches(camera) {
return camera && String(camera.getName()) === name;
}
try {
if (cameraMatches(machine.defaultHead.defaultCamera)) {
return machine.defaultHead.defaultCamera;
}
}
catch (defaultError) {
print('Could not check default head camera for ' + name + ': ' + defaultError);
}
try {
var headCameras = machine.defaultHead.getCameras();
for (var headIndex = 0; headIndex < headCameras.size(); headIndex++) {
var headCamera = headCameras.get(headIndex);
if (cameraMatches(headCamera)) {
return headCamera;
}
}
}
catch (headError) {
print('Could not enumerate head cameras while looking for ' + name + ': ' + headError);
}
try {
var machineCameras = machine.getCameras();
for (var machineIndex = 0; machineIndex < machineCameras.size(); machineIndex++) {
var machineCamera = machineCameras.get(machineIndex);
if (cameraMatches(machineCamera)) {
return machineCamera;
}
}
}
catch (machineError) {
print('Could not enumerate machine cameras while looking for ' + name + ': ' + machineError);
}
throw new Error('Camera not found: ' + name);
}
function printNozzleLocations(head) {
try {
var nozzles = head.getNozzles();
for (var i = 0; i < nozzles.size(); i++) {
var nozzle = nozzles.get(i);
print('Nozzle state: ' + nozzle.getName() + ' location=' + formatLocation(nozzle.location));
}
}
catch (error) {
print('Could not enumerate nozzle states: ' + error);
}
}
function parkNozzlesForScan(head) {
print('Parking nozzles before training scan XY motion.');
printNozzleLocations(head);
try {
var nozzles = head.getNozzles();
for (var i = 0; i < nozzles.size(); i++) {
try {
nozzles.get(i).moveToSafeZ();
print('Parked nozzle with OpenPnP safe Z: ' + nozzles.get(i).getName()
+ ' location=' + formatLocation(nozzles.get(i).location));
}
catch (parkError) {
print('Could not park nozzle ' + nozzles.get(i).getName() + ': ' + parkError);
}
}
}
catch (error) {
print('Could not enumerate nozzles for parking: ' + error);
}
print('Nozzle states after parking:');
printNozzleLocations(head);
}
task(function() {
var camera = machine.defaultHead.defaultCamera;
if (camera.getName() !== 'Top') {
camera = findCameraByName('Top');
}
parkNozzlesForScan(machine.defaultHead);
var calibration = loadTrainingTrayCalibration({
xLeft: 361.0,
xRight: 411.0,
yTop: 208.0,
yBottom: 319.0,
cameraXOffsetMm: -23.0,
cameraYOffsetMm: 64.0,
scanBoundsAreCameraCoordinates: false,
xStepMm: 8.0,
yStepMm: 5.0
});
var xLeft = calibration.xLeft;
var xRight = calibration.xRight;
var yTop = calibration.yTop;
var yBottom = calibration.yBottom;
var cameraXOffsetMm = calibration.cameraXOffsetMm;
var cameraYOffsetMm = calibration.cameraYOffsetMm;
var xStepMm = calibration.xStepMm;
var yStepMm = calibration.yStepMm;
var controlDir = new File(projectDir, 'control');
var statusFile = new File(controlDir, 'training_scan_status.json');
var outputRoot = new File(projectDir, 'training_scans');
var scanId = 'training_scan_' + timestamp();
controlDir.mkdirs();
var scanDir = new File(outputRoot, scanId);
var framesDir = new File(scanDir, 'frames');
framesDir.mkdirs();
var manifestFile = new File(scanDir, 'manifest.jsonl');
var scanInfoFile = new File(scanDir, 'scan_info.json');
var manifest = new FileWriter(manifestFile);
var xs = positions(xLeft, xRight, xStepMm, false);
var ys = positions(yTop, yBottom, yStepMm, false);
var frameIndex = 0;
var totalFrames = xs.length * ys.length;
print('Starting full-tray Top camera training scan: ' + scanId);
print('Frames directory: ' + framesDir.getAbsolutePath());
print('Training scan area: X=' + xLeft.toFixed(3) + '..' + xRight.toFixed(3)
+ ' Y=' + yTop.toFixed(3) + '..' + yBottom.toFixed(3)
+ ' (full tray range)');
print('Scan overlap step: X step=' + xStepMm.toFixed(3)
+ ' Y step=' + yStepMm.toFixed(3));
print('Grid: ' + xs.length + ' columns x ' + ys.length + ' rows'
+ ' = ' + totalFrames + ' frame(s)');
print('Training tray calibration source: ' + calibration.source);
print('Scan bounds are camera coordinates: ' + calibration.scanBoundsAreCameraCoordinates);
print('Camera X compensation: ' + cameraXOffsetMm.toFixed(3) + ' mm');
print('Camera Y compensation: +' + cameraYOffsetMm.toFixed(3) + ' mm');
writeStatus(statusFile, 'running', scanId, frameIndex, totalFrames, 'Training scan started');
var cameraLocation = camera.getLocation();
var unitsPerPixel = getUnitsPerPixelForCurrentZ(camera);
var firstCommandedX = calibration.scanBoundsAreCameraCoordinates ? xs[0] : xs[0] + cameraXOffsetMm;
var firstCommandedY = calibration.scanBoundsAreCameraCoordinates ? ys[0] : ys[0] + cameraYOffsetMm;
print('Top camera location at scan start: ' + formatLocation(cameraLocation));
print('First requested scan coordinate is X=' + xs[0].toFixed(3) + ' Y=' + ys[0].toFixed(3));
print('First commanded camera target will be X=' + firstCommandedX.toFixed(3)
+ ' Y=' + firstCommandedY.toFixed(3));
writeText(scanInfoFile, JSON.stringify({
scan_id: scanId,
purpose: 'training_dataset',
camera: 'Top',
output_dir: scanDir.getAbsolutePath(),
frames_dir: framesDir.getAbsolutePath(),
x_left_mm: xLeft,
x_right_mm: xRight,
y_top_mm: yTop,
y_bottom_mm: yBottom,
x_step_mm: xStepMm,
y_step_mm: yStepMm,
camera_x_offset_mm: cameraXOffsetMm,
camera_y_offset_mm: cameraYOffsetMm,
scan_bounds_are_camera_coordinates: calibration.scanBoundsAreCameraCoordinates,
calibration_source: calibration.source,
columns: xs.length,
rows: ys.length,
total_frames: totalFrames,
started_at: new Date().toISOString()
}, null, 2) + '\n');
try {
for (var row = 0; row < ys.length; row++) {
var leftToRight = (row % 2) === 0;
for (var col = 0; col < xs.length; col++) {
var scanX = leftToRight ? xs[col] : xs[xs.length - 1 - col];
var scanY = ys[row];
var x = calibration.scanBoundsAreCameraCoordinates ? scanX : scanX + cameraXOffsetMm;
var y = calibration.scanBoundsAreCameraCoordinates ? scanY : scanY + cameraYOffsetMm;
var requestedX = calibration.scanBoundsAreCameraCoordinates ? scanX - cameraXOffsetMm : scanX;
var requestedY = calibration.scanBoundsAreCameraCoordinates ? scanY - cameraYOffsetMm : scanY;
var currentCameraLocation = camera.getLocation();
var location = currentCameraLocation.add(new Location(
LengthUnit.Millimeters,
x - currentCameraLocation.x,
y - currentCameraLocation.y,
0,
0
));
print('Moving Top camera to training frame ' + frameIndex
+ ' target X=' + x.toFixed(3)
+ ' Y=' + y.toFixed(3)
+ ' requested X=' + requestedX.toFixed(3)
+ ' Y=' + requestedY.toFixed(3));
camera.moveTo(location);
print('Top camera after move: ' + formatLocation(camera.getLocation()));
var image = camera.settleAndCapture();
var fileName = 'frame_' + pad(frameIndex, 5)
+ '_' + timestamp()
+ '_x' + x.toFixed(2)
+ '_y' + y.toFixed(2)
+ '.png';
var imageFile = new File(framesDir, fileName);
ImageIO.write(image, 'PNG', imageFile);
manifest.write(jsonLine(
frameIndex,
'frames/' + fileName,
x,
y,
requestedX,
requestedY,
image.getWidth(),
image.getHeight(),
unitsPerPixel
));
manifest.flush();
print('Captured training image ' + fileName);
frameIndex++;
writeStatus(
statusFile,
'running',
scanId,
frameIndex,
totalFrames,
'Captured training image ' + frameIndex + ' of ' + totalFrames
);
}
}
}
finally {
manifest.close();
}
writeStatus(
statusFile,
'completed',
scanId,
frameIndex,
totalFrames,
'Training scan completed; images saved without CV or picking'
);
print('Completed full-tray Top camera training scan: ' + scanDir.getAbsolutePath());
print('No segmentation, QA inspection, picking, or dropping was started.');
});
}