issue1038 / nodejs /transformer.js
jrsimuix's picture
initial test
102748f verified
import { pipeline, env, RawImage } from '@huggingface/transformers';
import sharp from 'sharp';
import { readFileSync } from 'fs';
env.localModelPath = './'; // Path to your ONNX model
env.allowRemoteModels = false; // Disable remote models
// Load the ONNX model
const imageClassifier = await pipeline('image-classification', 'saved-model/bk');
// Load and preprocess the image
const imageBuffer = readFileSync('./training_images/shirt/00e745c9-97d9-429d-8c3f-d3db7a2d2991.jpg');
const image = await sharp(imageBuffer).resize(128, 128).raw().toBuffer();
// Run inference
const results = await imageClassifier(image);
console.log(results);
// import { pipeline, env, RawImage } from '@huggingface/transformers';
// import sharp from 'sharp';
// // Configure environment
// env.localModelPath = './'; // Path to your ONNX model
// env.allowRemoteModels = false; // Disable remote models
// async function preprocessImage(imagePath) {
// const imageBuffer = await sharp(imagePath)
// .resize(128, 128) // Resize to model's expected dimensions
// .raw() // Get raw pixel data
// .toBuffer();
// const array = new Float32Array(imageBuffer.length).map((_, i) => imageBuffer[i] / 255.0); // Normalize to [0, 1]
// // RawImage expects data as Uint8ClampedArray, convert and reshape accordingly
// return new RawImage(
// Uint8ClampedArray.from(array.map(v => v * 255)), // Rescale back for RawImage
// 128,
// 128,
// 3 // Channels
// );
// }
// async function classifyImage(imagePath) {
// const classifier = await pipeline('image-classification', 'saved-model/');
// // Preprocess the image
// const preprocessedImage = await preprocessImage(imagePath);
// // Run the model inference
// const results = await classifier(preprocessedImage);
// console.log(results);
// return results[0]?.label || 'Unknown';
// }
// // Example usage
// classifyImage('./training_images/shirt/00e745c9-97d9-429d-8c3f-d3db7a2d2991.jpg')
// .then(partNumber => {
// console.log(`Predicted Part Number: ${partNumber}`);
// })
// .catch(error => console.error(error));