reactjs 如何在JS React项目中集成Clarifai API?

k5hmc34c  于 2023-03-08  发布在  React
关注(0)|答案(1)|浏览(165)

我正在使用Clarifai API开发Udemy Course(Zero to Mastery)中的人脸检测项目。不幸的是,我遇到了一个我不理解的错误。以下是代码:
App.js:

import "./App.css";
import React, { Component } from "react";
import Navigation from "./Components/Navigation/Navigation";
import Logo from "./Components/Logo/Logo";
import ImageLinkForm from "./Components/ImageLinkForm/ImageLinkForm";
import Rank from "./Components/Rank/Rank";
import ParticlesBg from "particles-bg";
import Clarifai from "clarifai";
import FaceRecognition from "./Components/FaceRecognition/FaceRecognition";

process.nextTick = setImmediate;

const app = new Clarifai.App({
  apiKey: "7a2f4c6565204e56bc4e6c1fc52e2dd8",
});

const particlesOptions = {
  particles: {
    number: {
      value: 3,
      density: {
        enable: true,
        value_area: 800,
      },
    },
  },
};

class App extends Component {
  constructor() {
    super();
    this.state = {
      input: "",
    };
  }
  onInputChange = (event) => {
    console.log(event.target.value);
  };

  onButtonSubmit = () => {
    console.log("click!");
    app.models
      .predict(
        "6dc7e46bc9124c5c8824be4822abe105",
        "https://samples.clarifai.com/face-det.jpg"
      )
      .then((response) =>
        this.displayFaceBox(this.calculateFaceLocation(response))
      )
      .catch((err) => console.log(err));
  };

  render() {
    return (
      <div className="App">
        <ParticlesBg
          className="particles"
          params={particlesOptions}
          bg={true}
        />
        <Navigation />
        <Logo />
        <Rank />
        <ImageLinkForm
          onInputChange={this.onInputChange}
          onButtonSubmit={this.onButtonSubmit}
        />
        <FaceRecognition />
      </div>
    );
  }
}
export default App;

和图像链接表:

import React from "react";

const ImageLinkForm = ({ onInputChange, onButtonSubmit }) => {
  return (
    <div>
      <p className="f3">{"This magic brain will detect faces"}</p>
      <div className="center">
        <div className="form center pa4 br3 shadow-5">
          <input
            className="f4 pa2 w-70 center"
            type="text"
            onChange={onInputChange}
          ></input>
          <button
            className="w-30 grow f4 link ph3 pv2 dib white bg-light-purple"
            onClick={onButtonSubmit}
          >
            Detect
          </button>
        </div>
      </div>
    </div>
  );
};

export default ImageLinkForm;

下面是错误:谢谢你的帮助!
我尝试将react-scrpits更改为版本4.0.2并安装axios npm(-〉Im getting this error trying to use the clarifai api, how can i fix it?)。我还尝试更改Clarifai模块的版本(未更改)。可能是XML请求有问题,我还不太理解。我有一个问题,通过另一种方式集成JS的RestAPI,而不是在课程中完成。感谢您的帮助!

6rqinv9w

6rqinv9w1#

如果使用带有Clarifai API的ReactJS,建议您通过JavaScript HTTP通道使用它。您可以在他们的docs平台上查看JavaScript(REST)示例,并了解如何将Clarifai API集成到您的React项目中。对于Node.js项目,建议使用Node gRPC客户端。

相关问题