jrsimuix commited on
Commit
102748f
1 Parent(s): ed22dc9

initial test

Browse files
Files changed (1) hide show
  1. nodejs/transformer.js +62 -0
nodejs/transformer.js ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { pipeline, env, RawImage } from '@huggingface/transformers';
2
+ import sharp from 'sharp';
3
+ import { readFileSync } from 'fs';
4
+
5
+ env.localModelPath = './'; // Path to your ONNX model
6
+ env.allowRemoteModels = false; // Disable remote models
7
+
8
+ // Load the ONNX model
9
+ const imageClassifier = await pipeline('image-classification', 'saved-model/bk');
10
+
11
+ // Load and preprocess the image
12
+ const imageBuffer = readFileSync('./training_images/shirt/00e745c9-97d9-429d-8c3f-d3db7a2d2991.jpg');
13
+ const image = await sharp(imageBuffer).resize(128, 128).raw().toBuffer();
14
+
15
+ // Run inference
16
+ const results = await imageClassifier(image);
17
+ console.log(results);
18
+
19
+
20
+ // import { pipeline, env, RawImage } from '@huggingface/transformers';
21
+ // import sharp from 'sharp';
22
+
23
+ // // Configure environment
24
+ // env.localModelPath = './'; // Path to your ONNX model
25
+ // env.allowRemoteModels = false; // Disable remote models
26
+
27
+ // async function preprocessImage(imagePath) {
28
+ // const imageBuffer = await sharp(imagePath)
29
+ // .resize(128, 128) // Resize to model's expected dimensions
30
+ // .raw() // Get raw pixel data
31
+ // .toBuffer();
32
+
33
+ // const array = new Float32Array(imageBuffer.length).map((_, i) => imageBuffer[i] / 255.0); // Normalize to [0, 1]
34
+
35
+ // // RawImage expects data as Uint8ClampedArray, convert and reshape accordingly
36
+ // return new RawImage(
37
+ // Uint8ClampedArray.from(array.map(v => v * 255)), // Rescale back for RawImage
38
+ // 128,
39
+ // 128,
40
+ // 3 // Channels
41
+ // );
42
+ // }
43
+
44
+ // async function classifyImage(imagePath) {
45
+ // const classifier = await pipeline('image-classification', 'saved-model/');
46
+
47
+ // // Preprocess the image
48
+ // const preprocessedImage = await preprocessImage(imagePath);
49
+
50
+ // // Run the model inference
51
+ // const results = await classifier(preprocessedImage);
52
+
53
+ // console.log(results);
54
+ // return results[0]?.label || 'Unknown';
55
+ // }
56
+
57
+ // // Example usage
58
+ // classifyImage('./training_images/shirt/00e745c9-97d9-429d-8c3f-d3db7a2d2991.jpg')
59
+ // .then(partNumber => {
60
+ // console.log(`Predicted Part Number: ${partNumber}`);
61
+ // })
62
+ // .catch(error => console.error(error));