0

i am new to Tensorflow.js so, i don't quite understand it completely. I have trained an object detection model using Azure Custom Vision (customvision.ai) and as described in there documentation i exported the model for Tensorflow.js for offline use in my Node.js Project.

They have a very brief guide on Github (https://github.com/microsoft/customvision-tfjs) on how to use the model with Tensorflow.js, i tried following the steps but was not able to run it.

If anyone has experience with Tensorflow.js, please help me in this i am stuck on it for past 3 days.

This is the Code that i am trying to execute, i am getting Error

'await can only be used with async functions'

even though the loadModelAsync() function is Async.

const cvstfjs = require('@microsoft/customvision-tfjs');

let model = new cvstfjs.ObjectDetectionModel();
await model.loadModelAsync('model.json');
const image = document.getElementById('image');
const result = await model.executeAsync(image);
Aakash Bhadana
  • 302
  • 2
  • 17
  • That isn't node.js code, that's JavaScript code in HTML. Where are you stuck Node.js -wise? – AKX Feb 17 '20 at 12:20
  • @AKX Thanks for replying. In the Guide here (https://github.com/microsoft/customvision-tfjs). They have provided a code snippet to use the model in Node.js application. But when i try i get the error (import statement cannot be used outside modules). So, i tried using web page snippet. Can you help me in figuring how to run that in Node js – Aakash Bhadana Feb 17 '20 at 12:23
  • You will need to use a `require` statement (`const cvstfjs = require('@microsoft/customvision-tfjs');`) instead. – AKX Feb 17 '20 at 12:24
  • @AKX i tried but then i get the error (await can only be used with async function) although i checked in the library myself that the function loadModelAsync() is actually Async. – Aakash Bhadana Feb 17 '20 at 12:25
  • Yes, the function in which you call async functions with `await` will need to be `async`. – AKX Feb 17 '20 at 12:29
  • @AKX It is Async, still the Error is coming – Aakash Bhadana Feb 17 '20 at 12:30
  • Add your code in the question then... – AKX Feb 17 '20 at 12:31
  • @AKX Ok i just added – Aakash Bhadana Feb 17 '20 at 12:35

2 Answers2

1

As the error says, you will need to wrap any await calls in an async function:

const cvstfjs = require('@microsoft/customvision-tfjs');

async function doThings() {
  let model = new cvstfjs.ObjectDetectionModel();
  await model.loadModelAsync('model.json');
  const image = document.getElementById('image');
  const result = await model.executeAsync(image);
  return result;
}

doThings().then((result) => {
  console.log(result);
});
AKX
  • 152,115
  • 15
  • 115
  • 172
  • This code also gives Error : (node:15392) UnhandledPromiseRejectionWarning: TypeError: Only absolute URLs are supported at getNodeRequestOptions (C:\Users\Aakash Bhadana\Desktop\tensorflowjs\node_modules\node-fetch\lib\index.js:1284:9) at C:\Users\Aakash Bhadana\Desktop\tensorflowjs\node_modules\node-fetch\lib\index.js:1370:19 – Aakash Bhadana Feb 17 '20 at 12:47
  • Right, that's another problem then. It's unable to load your model from that path. (Based on the exception a full HTTP URL would probably work.) – AKX Feb 17 '20 at 12:49
  • Yes you are right, i changed the filename with localhost URL, now the error is for 'document' not defined. How to access HTML image element in Node.js app ? – Aakash Bhadana Feb 17 '20 at 12:52
  • The Code snippet has document.getElementById() in between Node.js code, it doesn't makes sense to me ?? Can you help on how to do that. – Aakash Bhadana Feb 17 '20 at 13:18
  • You'll need to get the image data from somewhere. Depends on your application. – AKX Feb 17 '20 at 13:30
0

Did you see ONNX.js that run ONNX models using JavaScript. https://github.com/microsoft/onnxjs

enter image description here

Ram
  • 2,459
  • 1
  • 7
  • 14