Skip to content

Commit

Permalink
classify-image improvements (#43)
Browse files Browse the repository at this point in the history
* pass --port, --random-order from classify-images

* use image dir as static dir

* be a little more clever
  • Loading branch information
danvk authored Sep 22, 2024
1 parent edde21c commit 952113e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
29 changes: 27 additions & 2 deletions classify-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import fs from 'fs';
import {Command} from 'commander';

import {dedent} from './utils';
import path from 'path';

const temp = require('temp').track();

Expand All @@ -27,21 +28,27 @@ function list(val: string) {
}

interface CLIArgs {
port?: number;
output: string;
labels: string[];
max_width?: number;
randomOrder?: boolean;
}

const program = new Command();
program
.version('2.1.1')
.usage('[options] /path/to/images/*.jpg | images.txt')
.option('-p, --port <n>', 'Run on this port (default 4321)', parseInt)
.option('-o, --output <file>',
'Path to output CSV file (default output.csv)', 'output.csv')
.option('-l, --labels <csv>',
'Comma-separated list of choices of labels', list, ['Yes', 'No'])
.option('-w, --max_width <pixels>',
'Make the images this width when displaying in-browser', parseInt)
.option('-r, --random-order',
'Serve images in random order, rather than sequentially. This is useful for ' +
'generating valid subsamples or for minimizing collisions during group localturking.')
.parse()

if (program.args.length == 0) {
Expand All @@ -62,11 +69,19 @@ if (fs.existsSync(options.output)) {
const csvInfo = temp.openSync({suffix: '.csv'});
const templateInfo = temp.openSync({suffix: '.html'});

const images = program.args;
let staticDir: string | null = null;
let images = program.args;
if (images.length === 1 && images[0].endsWith('.txt')) {
fs.writeSync(csvInfo.fd, 'path\n');
fs.writeSync(csvInfo.fd, fs.readFileSync(images[0], 'utf8'));
} else {
const anyOutsideCwd = images.some(p => path.isAbsolute(p) || p.startsWith('..'));
if (anyOutsideCwd) {
staticDir = path.dirname(images[0]);
images = images.map(p => path.relative(staticDir!, p));
} else {
staticDir = '.';
}
fs.writeSync(csvInfo.fd, 'path\n' + images.join('\n') + '\n');
}
fs.closeSync(csvInfo.fd);
Expand Down Expand Up @@ -94,6 +109,16 @@ html += dedent`
fs.writeSync(templateInfo.fd, html);
fs.closeSync(templateInfo.fd);

const args = ['localturk', '--static-dir', '.', templateInfo.path, csvInfo.path, options.output];
const opts = [];
if (staticDir) {
opts.push('--static-dir', staticDir);
}
if (options.port) {
opts.push('--port', options.port.toString());
}
if (options.randomOrder) {
opts.push('--random-order');
}
const args = ['localturk', ...opts, templateInfo.path, csvInfo.path, options.output];
console.log('Running ', args.join(' '));
child_process.spawn(args[0], args.slice(1), {stdio: 'inherit'});
1 change: 1 addition & 0 deletions localturk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface CLIArgs {

const program = new Command();

// If you add an option here, consider adding it in classify-images.ts as well.
program
.version('2.1.1')
.usage('[options] template.html tasks.csv outputs.csv')
Expand Down

0 comments on commit 952113e

Please sign in to comment.