将值从 p5 sketch 传递到 React(使用 react-p5-wrapper)
Passing value from p5 sketch to React (with react-p5-wrapper)
我有一个 p5 网络摄像头视频预测系统正在运行。目前,我正在尝试将其插入 React 应用程序以创建更完整的网络应用程序。
我的问题是,现在只在我的 p5 sketch 中进行预测,我希望将预测值传递给 React 的 App.js 以进行进一步构建。有什么方法吗?
顺便说一句,我正在使用 react-p5-wrapper。
这是sketch.js:
import "react-p5-wrapper/node_modules/p5/lib/addons/p5.dom";
import ml5 from 'ml5';
let mobileNet;
let video;
let label='model loading...';
function sketch (p) {
p.setup = function () {
p.createCanvas(1000, 1000);
//imitialize the webcam stream in a object
video = p.createCapture(p.VIDEO);
//hide the webcam stream
video.hide();
//initialize the mobilenet object with a callback
mobileNet= ml5.imageClassifier('MobileNet',video,ModelLoaded);
};
p.draw = function () {
p.image(video,0,0);
p.textSize(16);
p.fill(255,140,0);
p.text(label,10,450);
};
};
function ModelLoaded()
{
console.log('Model is ready');
//predicting the image
mobileNet.predict(result)
}
//callback function to get the results
function result(err,res)
{
//check for errors
if(err)
{
//log the error if any
console.error(err)
}
else{
//get the label from the json result
label = res[0].className;
//predicting the image again
mobileNet.predict(result)
}
}
export default sketch;
我的 App.js 目前看起来像这样:
import React, { Component } from 'react';
// import logo from './logo.svg';
import './App.css';
import sketch from './sketch';
import P5Wrapper from 'react-p5-wrapper';
class App extends Component {
componentDidMount(){
}
render() {
return (
<div className="App">
<P5Wrapper sketch={sketch} />
</div>
);
}
}
export default App;
感谢任何帮助!
我试了一下,想出了一个解决方案。它不是很优雅,但应该可以。我在 sketch.js 中做了一个非常简单的测试项目,我试图说明两种访问信息的方法。需要注意的是 timesClicked 变量和 myCustomRedrawAccordingToNewPropsHandler 函数。
export let timesClicked = 0;
export default function sketch (p) {
p.setup = function () {
p.createCanvas(300, 300);
};
p.draw = function () {
p.background(0);
p.fill(255);
p.ellipse(p.mouseX, p.mouseY, 100, 100);
};
p.myCustomRedrawAccordingToNewPropsHandler = function(newProps){
if(newProps.getCoords){
p.sendCoords = newProps.getCoords;
}
}
p.mouseClicked = function() {
p.sendCoords(p.mouseX, p.mouseY);
timesClicked++;
}
};
timesClicked是一个可以导入的变量,统计鼠标被点击的次数。它可以从草图范围内修改并从其他文件导入。
myCustomRedrawAccordingToNewPropsHandler 是一个从 react-p5-wrapper 库调用的函数,只要组件接收到 props 并且可以在草图内定义.
有了这个,你的 App.js 文件可以像这样修改:
import React, { Component } from 'react';
import P5Wrapper from 'react-p5-wrapper';
import sketch from './sketch';
import {timesClicked} from './sketch';
function getCoords(){
console.log(arguments);
}
class App extends Component {
componentDidMount(){
}
render() {
return (
<div className="App">
<P5Wrapper sketch={sketch} getCoords={getCoords}/>
</div>
);
}
}
export default App;
document.body.onkeyup = function(e){
if(e.keyCode == 32){
console.log(timesClicked);
}
}
当运行时,每次点击,sketch都会执行getCoords()文件中的App.js函数,或者,每次按下 space 栏时,将从 App.js 文件访问 timesClicked 变量。我认为您可以修改它以 "send" 或 "read" 预测值。
我有一个 p5 网络摄像头视频预测系统正在运行。目前,我正在尝试将其插入 React 应用程序以创建更完整的网络应用程序。
我的问题是,现在只在我的 p5 sketch 中进行预测,我希望将预测值传递给 React 的 App.js 以进行进一步构建。有什么方法吗?
顺便说一句,我正在使用 react-p5-wrapper。
这是sketch.js:
import "react-p5-wrapper/node_modules/p5/lib/addons/p5.dom";
import ml5 from 'ml5';
let mobileNet;
let video;
let label='model loading...';
function sketch (p) {
p.setup = function () {
p.createCanvas(1000, 1000);
//imitialize the webcam stream in a object
video = p.createCapture(p.VIDEO);
//hide the webcam stream
video.hide();
//initialize the mobilenet object with a callback
mobileNet= ml5.imageClassifier('MobileNet',video,ModelLoaded);
};
p.draw = function () {
p.image(video,0,0);
p.textSize(16);
p.fill(255,140,0);
p.text(label,10,450);
};
};
function ModelLoaded()
{
console.log('Model is ready');
//predicting the image
mobileNet.predict(result)
}
//callback function to get the results
function result(err,res)
{
//check for errors
if(err)
{
//log the error if any
console.error(err)
}
else{
//get the label from the json result
label = res[0].className;
//predicting the image again
mobileNet.predict(result)
}
}
export default sketch;
我的 App.js 目前看起来像这样:
import React, { Component } from 'react';
// import logo from './logo.svg';
import './App.css';
import sketch from './sketch';
import P5Wrapper from 'react-p5-wrapper';
class App extends Component {
componentDidMount(){
}
render() {
return (
<div className="App">
<P5Wrapper sketch={sketch} />
</div>
);
}
}
export default App;
感谢任何帮助!
我试了一下,想出了一个解决方案。它不是很优雅,但应该可以。我在 sketch.js 中做了一个非常简单的测试项目,我试图说明两种访问信息的方法。需要注意的是 timesClicked 变量和 myCustomRedrawAccordingToNewPropsHandler 函数。
export let timesClicked = 0;
export default function sketch (p) {
p.setup = function () {
p.createCanvas(300, 300);
};
p.draw = function () {
p.background(0);
p.fill(255);
p.ellipse(p.mouseX, p.mouseY, 100, 100);
};
p.myCustomRedrawAccordingToNewPropsHandler = function(newProps){
if(newProps.getCoords){
p.sendCoords = newProps.getCoords;
}
}
p.mouseClicked = function() {
p.sendCoords(p.mouseX, p.mouseY);
timesClicked++;
}
};
timesClicked是一个可以导入的变量,统计鼠标被点击的次数。它可以从草图范围内修改并从其他文件导入。
myCustomRedrawAccordingToNewPropsHandler 是一个从 react-p5-wrapper 库调用的函数,只要组件接收到 props 并且可以在草图内定义.
有了这个,你的 App.js 文件可以像这样修改:
import React, { Component } from 'react';
import P5Wrapper from 'react-p5-wrapper';
import sketch from './sketch';
import {timesClicked} from './sketch';
function getCoords(){
console.log(arguments);
}
class App extends Component {
componentDidMount(){
}
render() {
return (
<div className="App">
<P5Wrapper sketch={sketch} getCoords={getCoords}/>
</div>
);
}
}
export default App;
document.body.onkeyup = function(e){
if(e.keyCode == 32){
console.log(timesClicked);
}
}
当运行时,每次点击,sketch都会执行getCoords()文件中的App.js函数,或者,每次按下 space 栏时,将从 App.js 文件访问 timesClicked 变量。我认为您可以修改它以 "send" 或 "read" 预测值。