|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * This is an optimization for a common use case of localturk: classifying |
| 5 | + * images. When you use this script, you can skip creating a CSV file of inputs |
| 6 | + * and an HTML template. |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * |
| 10 | + * classify-images -o labels.csv --labels Yes,No,Maybe *.jpg |
| 11 | + * |
| 12 | + * This will present a web UI for classifying each image and put the output in |
| 13 | + * labels.csv. |
| 14 | + */ |
| 15 | + |
| 16 | +var child_process = require('child_process'), |
| 17 | + escape = require('escape-html'), |
| 18 | + fs = require('fs'), |
| 19 | + program = require('commander'), |
| 20 | + temp = require('temp').track(); |
| 21 | + |
| 22 | +function list(val) { |
| 23 | + return val.split(','); |
| 24 | +} |
| 25 | + |
| 26 | +program |
| 27 | + .version('1.1.0') |
| 28 | + .usage('[options] /path/to/images/*.jpg') |
| 29 | + .option('-o, --output <file>', |
| 30 | + 'Path to output CSV file (default output.csv)', 'output.csv') |
| 31 | + .option('-l, --labels <csv>', |
| 32 | + 'Comma-separated list of choices of labels', list, ['Yes', 'No']) |
| 33 | + .option('-w, --max_width <pixels>', |
| 34 | + 'Make the images this width when displaying in-browser', parseInt) |
| 35 | + .parse(process.argv) |
| 36 | + |
| 37 | +if (program.args.length == 0) { |
| 38 | + console.error('You must specify at least one image file!\n'); |
| 39 | + program.help(); // exits |
| 40 | +} |
| 41 | + |
| 42 | +if (fs.existsSync(program.output)) { |
| 43 | + console.warn('Output file ' + program.output + ' already exists.'); |
| 44 | + console.warn('Its contents will be assumed to be previously-generated labels.'); |
| 45 | + console.warn('If you want to start from scratch, either delete this file,'); |
| 46 | + console.warn('rename it or specify a different output via --output.\n'); |
| 47 | +} |
| 48 | + |
| 49 | +var csvInfo = temp.openSync({suffix: '.csv'}), |
| 50 | + templateInfo = temp.openSync({suffix: '.html'}); |
| 51 | + |
| 52 | +fs.writeSync(csvInfo.fd, 'path\n' + program.args.join('\n') + '\n'); |
| 53 | +fs.closeSync(csvInfo.fd); |
| 54 | + |
| 55 | +var buttonsHtml = program.labels.map(function(label, idx) { |
| 56 | + var buttonText = label + ' (' + (1 + idx) + ')'; |
| 57 | + return '<button type="submit" id=' + (1+idx) + ' name="label" value="' + label + '">' + escape(buttonText) + '</button>' |
| 58 | +}).join(' '); |
| 59 | +var widthHtml = program.max_width ? ' width="' + program.max_width + '"' : ''; |
| 60 | +var html = buttonsHtml + '\n<p><img src="${path}" ' + widthHtml + '></p>'; |
| 61 | + |
| 62 | +// Add keyboard shortcuts. 1=first button, etc. |
| 63 | +html += [ |
| 64 | + '<script>', |
| 65 | + 'window.addEventListener("keydown", function(e) {', |
| 66 | + ' var code = e.keyCode;', |
| 67 | + ' if (code < 48 || code > 57) return;', |
| 68 | + ' var el = document.getElementById(String.fromCharCode(code));', |
| 69 | + ' if (el) {', |
| 70 | + ' e.preventDefault();', |
| 71 | + ' el.click();', |
| 72 | + ' }', |
| 73 | + '});', |
| 74 | + '</script>' |
| 75 | +].join('\n'); |
| 76 | + |
| 77 | +fs.writeSync(templateInfo.fd, html); |
| 78 | +fs.closeSync(templateInfo.fd); |
| 79 | + |
| 80 | +var args = ['localturk.js', '-q', '--static_dir', '.', templateInfo.path, csvInfo.path, program.output]; |
| 81 | +console.log('Running ', args.join(' ')); |
| 82 | +child_process.spawn(args[0], args.slice(1), {stdio: 'inherit'}); |
0 commit comments