throughmark
Version:
Find and Annotate Features in Images, From Objects to Concepts
75 lines (74 loc) • 3.12 kB
JavaScript
import { Throughmark } from "./Throughmark.js";
import { writeFile, mkdir } from "fs/promises";
import { join } from "path";
import { existsSync } from "fs";
const DEFAULT_IMAGE = join(process.cwd(), "/samples/automobile/car0.jpg");
const OUTPUT_DIR = join(process.cwd(), "output");
const PRICING = {
anthropic: {
input: 0.003, // $3.00 per 1M tokens = $0.003 per 1K tokens
output: 0.015, // $15.00 per 1M tokens = $0.015 per 1K tokens
},
openai: {
input: 0.0025, // $2.50 per 1M tokens = $0.0025 per 1K tokens
output: 0.01, // $10.00 per 1M tokens = $0.01 per 1K tokens
},
};
const run = async () => {
try {
// Parse command line args
const args = process.argv.slice(2);
const imagePath = args[0] || DEFAULT_IMAGE;
const showGrid = !args.includes("nogrid");
const useOpenAI = !args.includes("anthropic"); // Use OpenAI by default
if (!existsSync(imagePath)) {
console.error(`Image not found: ${imagePath}`);
console.error("Please provide an image path: yarn start <path-to-image>");
process.exit(1);
}
await mkdir(OUTPUT_DIR, { recursive: true });
const config = {
llm: {
provider: useOpenAI ? "openai" : "anthropic",
},
};
const gridder = new Throughmark(config);
// Save the grid overlay for reference
const gridOverlay = await gridder.generateGridOverlayImage(imagePath);
await writeFile(join(OUTPUT_DIR, "grid-overlay.png"), gridOverlay);
// Get analysis
console.log("Analyzing image...");
const analysis = await gridder.analyze({
imagePath,
prompt: "Highlight the damage in the center car only.",
});
console.log("\nAnalysis:");
console.log(JSON.stringify(analysis, null, 2)); // Pretty print JSON
// Generate simple highlighted image
const highlighted = await gridder.generateSimpleHighlight({
imagePath,
analysis,
showGrid,
});
await writeFile(join(OUTPUT_DIR, "highlighted.png"), highlighted);
console.log("\nHighlighted image saved to:", join(OUTPUT_DIR, "highlighted.png"));
// Calculate cost based on provider
if (analysis.tokens) {
const pricing = useOpenAI ? PRICING.openai : PRICING.anthropic;
const cost = (analysis.tokens.input / 1000) * pricing.input +
(analysis.tokens.output / 1000) * pricing.output;
console.log("\nToken Usage:");
console.log(`Input tokens: ${analysis.tokens.input}`);
console.log(`Output tokens: ${analysis.tokens.output}`);
console.log(`Total tokens: ${analysis.tokens.total}`);
console.log(`Total cost: $${cost.toFixed(4)} (${useOpenAI ? "OpenAI" : "Anthropic"} pricing)`);
}
}
catch (error) {
console.error("Error:", error);
if (error instanceof Error) {
console.error("Stack trace:", error.stack);
}
}
};
run();