繁体   English   中英

TensorFlowJS 是否需要 HTML? 或者我可以从我的电脑上传图片和视频吗?

[英]Does TensorFlowJS require HTML? Or can I upload images and video from my PC?

我正在尝试创建一个在 Discord.js 上运行的 Object 分类器 discord 机器人,一切都比较顺利,但我遇到了这个错误:

pixels passed to tf.browser.frompixels() cannot be null

据我了解,这是因为我使用的是计算机中的图像,而不是带有“ <img> ”标签的 HTML 图像。

所以我想知道我是否错了,有办法解决这个问题还是这不可能?

注意:我使用 COCO-SSD 作为预训练的 model

您可以使用经过训练的 model 从计算机中的视频或图像中检测 object。 我已经完成了一个完整的项目,你可以在这里查看

或按照以下步骤操作。
1-加载 model

const coco = require('@tensorflow-models/coco-ssd')

const loadModel =async()=>{
    try{
        const model = await coco.load()
        return model
    }catch(err){
        console.log("there was an error loading the model trying re-loading attempt...")
        return 0;
    }
}

module.exports = loadModel

2-读取图像并转动并转换它,以便 model 可以读取它。

const fs = require('fs')
const tf = require('@tensorflow/tfjs-node')

ImageName = '../VidUploads/imageUp-1589491328827.jpg'

const readImage = (path)=>{
    const buffer = fs.readFileSync(path)
    try { 
      const pixels =tf.node.decodeImage(new Uint8Array(buffer),3)
      console.log(`image shape ${pixels.shape}`)
      return pixels 
    }catch(err){
      console.log(`something went wrong while converting the image to tensor`)
      return -1 
    }
   
   
}

3-使用model

const imagePrediction =async(model , image)=>{

           return ( await model.detect(image))    
}

注意: imagePrediction将步骤 2 中的 model 和步骤 1 中的图像作为输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM