From f11f46b3a5ff5095c84303afbc30f67e3edbfd05 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Sat, 22 Feb 2025 19:51:52 +0530 Subject: [PATCH] Added test2 converted from python to js. Using vishnun0027's pretrained model. --- models/test2.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 models/test2.js diff --git a/models/test2.js b/models/test2.js new file mode 100644 index 0000000..e5eb797 --- /dev/null +++ b/models/test2.js @@ -0,0 +1,40 @@ +// npm install @huggingface/huggingface jimp + +const fs = require('fs'); +const { ImageProcessor, Model } = require('@huggingface/huggingface'); +const Jimp = require('jimp'); // For image processing + +async function main() { + const modelName = 'vishnun0027/Crop_Disease_model_1'; + + // Load the image processor and model + const imageProcessor = await ImageProcessor.fromPretrained(modelName); + const model = await Model.fromPretrained(modelName); + + // Load your image + let image; + try { + image = await Jimp.read('/home/overnion/Status200/tomato.png'); // Replace with the actual path to your image + // Convert the image to RGB if it's not already + if (image.bitmap.colorType !== 2) { // 2 means RGB + image = image.colorType(2); + } + } catch (error) { + console.error("Error: Unable to open image. Check the file type or path."); + console.error(error); + return; + } + + // Prepare the image for the model + const inputs = imageProcessor(images: image, returnTensors: "pt"); + + // Make the prediction + const outputs = await model(inputs); + const logits = outputs.logits; + const predictedClassIdx = logits.argMax(-1).dataSync()[0]; + + // Print the predicted class + console.log("Predicted class:", model.config.id2label[predictedClassIdx]); +} + +main().catch(console.error);