【问题标题】:React: How to setup function to use image from stateReact:如何设置函数以使用来自状态的图像
【发布时间】:2021-09-29 08:25:11
【问题描述】:

我的 react 组件中有一个 imageClassification 函数,它使用 document.getElementById const img = document.getElementById('animal_image'); 从 img 标签中获取要分类的图像。

通过文件输入上传的图像更新状态,然后传递给 img 标签 <img id="animal_image" src={profileImg} alt="a test" />

如何更新下面的代码,以便classifyImage 函数从通过文件输入上传图像时获取图像,而不是从img 标签获取。即我如何在classifyImage函数中访问状态?

我查看了文件阅读器文档,但仍然无法弄清楚。 https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

import React, { Component } from 'react';
import * as automl from '@tensorflow/tfjs-automl';
import '@tensorflow/tfjs-backend-webgl';
import SampleDog from '../images/dogTest.jpg';
import './imageloader.css';

export class ImageLoader extends Component {

    state={
        profileImg: 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png'
      }

    fileSelectedHandle = (e) => {
        const file = e.target.files[0];
        const reader = new FileReader();

        reader.onload = () =>{
         if(reader.readyState === 2){
            this.setState({profileImg: reader.result})
        }
        }
        reader.readAsDataURL(file)
    }


    classifyImage = async () => {
        const model = await automl.loadImageClassification('./image_classification_model_v1/model.json')

        const img = document.getElementById('animal_image');

        const predictions = await model.classify(img)

        console.log('predictions', predictions)
    }

    render() {

        const { profileImg } = this.state

        return (
            <div className="container">
               <div className="img-main">
                <h2>Image Classification Demo</h2>
                <p>Upload an image of a cat or dog to check the prediction score</p>
                <div >
                    <input 
                        type="file" 
                        accept="image/*"
                        name="image-upload"
                        id="input"
                        onChange={this.fileSelectedHandle} 
                    />
                </div>
                <div>
                    <img id="animal_image" src={profileImg} alt="a test" />
                </div>
                    
                <button 
                    className="img-loader-button"
                    onClick={() => this.classifyImage()}>Predict Score
                </button>
               </div>

            </div>
        )
    }
}

export default ImageLoader

【问题讨论】:

  • 但是当前的代码有效,不是吗?你做出改变的动机是什么?
  • 哦,你的意思是你想在文件上传时触发model.classify()而不需要点击“预测分数”按钮?
  • 代码有效,但由于我使用的是反应,我喜欢从在文件上传时获取更新的状态处理图像,而不是使用 document.getElementBy 我怎样才能实现这样的东西: 常量预测 = 等待 model.classify(this.setState.profileImg)
  • 即如何在classifyImage函数中访问状态?

标签: javascript reactjs tensorflow2.0 filereader


【解决方案1】:

要访问该状态,您只需执行model.classify(this.state.profileImg)。但这不能直接工作,因为 tensorflow 需要一个图像元素或 ImageData 作为输入。

所以我猜你还是需要创建一个图像元素。

fileSelectedHandle = (e) => {
    const file = e.target.files[0];
    const reader = new FileReader();

    reader.onload = () =>{
      if(reader.readyState === 2) {
        const img = document.createElement("img");
        img.src = reader.result;
        this.setState({ profileImg: img });
      }
    }
    reader.readAsDataURL(file)
}

【讨论】:

  • 好的,在 imageclassify 函数中工作,但在 img 标签中停止工作
  • 我通过在状态一个为 imageclassify 函数和另一个为 img 标签添加两个对象来工作 ---- this.state = { currentImage: '', previewImage: } 然后在fileSelectedHandle 函数将上传的图片分配给 currentImage 和 previewImage
猜你喜欢
  • 2018-09-15
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
相关资源
最近更新 更多