Run Tensorflow on Node.JS as Web Server to classify uploaded picture with an own TF-Model

Viewed 63

I would like to send an image to a server (HTTP Post) via an ESP32CAM to get the image classified with my own tensorflor model. As the baseline I would like to use the following script which already uses "@ google-cloud / vision" model to classify the image. It works so well so far.

I checked the Tensorflow help, but I haven't seen any reference examples

Markus

var fs = require('file-system');
const http = require('http');
const server = http.createServer();
const filePath = './resources/test.jpeg';

server.on('request', (request, response)=>{
    if(request.method == 'POST' && request.url === "/imageUpdate"){
        
        var ImageFile = fs.createWriteStream(filePath, {encoding: 'utf8'});
        request.on('data', function(data){
            ImageFile.write(data);
        });

        request.on('end',async function(){
            ImageFile.end();
            const labels = await labelAPI();
            response.writeHead(200, {'Content-Type' : 'application/json'});
            response.end(JSON.stringify(labels));
        });


    }else{
        console.log("error");
        response.writeHead(405, {'Content-Type' : 'text/plain'});
        response.end();
    }
});

async function labelAPI() {
  // Imports the Google Cloud client library
  const vision = require('@google-cloud/vision');

  // Creates a client
  const client = new vision.ImageAnnotatorClient();

  // Performs label detection on the image file
  const [result] = await client.labelDetection(filePath);
  const labels = result.labelAnnotations;
  var o = [];
  labels.forEach(label => {
    o.push({description: label.description, score: label.score});
  });
  return o;
}

const port = 8888;
server.listen(port)
console.log(`Listening at ${port}`)

I adapted the script as follows, but not sure about coding in "async function labelAPI()" block to get prediction running

var fs = require('file-system');
const http = require('http');
const server = http.createServer();
const filePath = './resources/test.jpeg';

const tf = require("@tensorflow/tfjs");
const tfn = require("@tensorflow/tfjs-node");
const handler = tfn.io.fileSystem("./my_model/model.json");

server.on('request', (request, response)=>{
    if(request.method == 'POST' && request.url === "/imageUpdate"){
        
        var ImageFile = fs.createWriteStream(filePath, {encoding: 'utf8'});
        request.on('data', function(data){
            ImageFile.write(data);
        });

        request.on('end',async function(){
            ImageFile.end();
            const labels = await labelAPI();
            response.writeHead(200, {'Content-Type' : 'application/json'});
            response.end(JSON.stringify(labels));
        });


    }else{
        console.log("error");
        response.writeHead(405, {'Content-Type' : 'text/plain'});
        response.end();
    }
});

 async function labelAPI() {
  // Imports own Tensorflow Model
  ...
  model = await tf.loadLayersModel(handler);
  console.log("Model loaded.");
  ...
}
const port = 8888;
server.listen(port)
console.log(`Listening at ${port}`)
0 Answers
Related