1

I think I am making a novice mistake but am having trouble figuring out what is going wrong.

Error:

C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing>node classify rlc.jpg (node:38620) UnhandledPromiseRejectionWarning: Error: cannot read as File: "model.json" at readFile (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\filereader\FileReader.js:266:15) at FileReader.self.readAsText (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\filereader\FileReader.js:295:7) at C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:226:36 at new Promise (<anonymous>) at BrowserFiles.<anonymous> (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:159:39) at step (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:48:23) at Object.next (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:29:53) at C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:23:71 at new Promise (<anonymous>) at __awaiter (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:19:12) (node:38620) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:38620) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Code:

const tf = require('@tensorflow/tfjs');
const tfnode = require('@tensorflow/tfjs-node');
const tmImage = require('@teachablemachine/image');
const fs = require('fs');
global.FileReader = require('filereader');
const uploadModel =  "model.json"
const uploadWeights = "weights.bin"
const uploadMetadata = "metadata.json"

const readImage = path => {
    const imageBuffer = fs.readFileSync(path);
    const tfimage = tfnode.node.decodeImage(imageBuffer);
    return tfimage;
}

const imageClassification = async path => {
    const image = readImage(path);
    const model = await tmImage.loadFromFiles(uploadModel,uploadWeights,uploadMetadata);
    const predictions = await model.predict(image);
    console.log('Classification Results:', predictions);
}

if (process.argv.length !== 3) throw new Error('Incorrect arguments: node classify.js <IMAGE_FILE>');

imageClassification(process.argv[2]);

File Structure:

/Testing
  /node_modules
  classify.js
  metadata.json
  model.json
  package-lock.json
  rlc.jpg
  weights.bin

Background: Trying to take what I have learned deploying image classifying model built in teachable machine with native javascript and adapt it to node js. I am a novice and am tripping over environment differences between node and the browser which all the tutorials I am following are based on.

Tutorials I am following:

1 Answer 1

1

Library expects File in loadFromFiles function, documentation in github. File is browser API that you cannot use in node by default. So you need to somehow polyfill that in node environment, check out these libraries node-fetch/fetch-blob node-file-api/file-api

Example usage with file-api:

const fs = require('fs');
const path = require('path');
const FileAPI = require('file-api');

const uploadModel =  "model.json"
const uploadModelPath = path.join(process.cwd(), uploadModel);

// polyfill
Object.keys(FileApi).forEach(key => { 
    process[key] = FileApi[key];
})

const uploadModelFile = new File({ 
  buffer: fs.readFileSync(uploadModelPath)
});

This library is pretty old though and it might not work, you can try search other polyfill libraries or write yours. You can see how file is read in tensor flow source code or you can fork and add possibility to work with node files.

Sign up to request clarification or add additional context in comments.

3 Comments

Working through your solution with file-api. Getting this error: C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\File\File.js:35 throw new Error("No name"); ^ Error: No name at new File (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\←[4mFile←[24m\File.js:35:13) at Object.<anonymous> (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\classify.js:24:25)". Going to keep digging thanks for the recommendations.
You need to specify name for the File like name: "abc-song.txt". You can see source code of libraries in node_moduels, I've used to understand problems by reading source code of libraries.
Figured that out, rolled back mime to a previous version to solve another problem, seems to be executing now but function "processMetaData" in the @teachablemachine module is throwing an error for invalid metada provided. The investigation continues, thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.