File size: 2,242 Bytes
102748f |
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 |
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));
|