1

我想做的事:我想用mobileNet从TensorFlow.js图像分类中得到概率最高的className。我想将类名作为字符串。

问题:我不知道如何将类名隔离为字符串。有这个命令吗?或者你知道我该如何解决我的问题吗?

我的代码:

theAi();

async function theAi() {
    const tf = require('@tensorflow/tfjs'),
    mobilenet = require('@tensorflow-models/mobilenet'),
    tfnode = require('@tensorflow/tfjs-node'),
    fs = require('fs-extra');

    const imageBuffer = await fs.readFile("./jesus.jpg"),
    tfimage = tfnode.node.decodeImage(imageBuffer),
    mobilenetModel = await mobilenet.load();  

    const results = await mobilenetModel.classify(tfimage);
    console.log(results);
};

和输出:

[
  {
    className: 'chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour',
    probability: 0.5979475378990173
  },
  { className: 'vestment', probability: 0.14261281490325928 },
  { className: 'fountain', probability: 0.03441018983721733 }
]

所以在这种情况下,我想得到字符串('chain mail, ring mail, mail, chain armor, chain armour, ring armour, ring armour')。

4

1 回答 1

1

这是一个纯 JavaScript 问题。如果你得到这个结果,你可以只搜索 m aximum:

const maxClass = result.reduce(function(prev, current) {
    return (prev.probability > current.probability) ? prev : current;
})["className"];
于 2020-10-12T12:53:47.947 回答