我现在有一个p5摄像头视频预测系统正在工作。目前,我正在尝试将其插入一个React应用程序中,以创建一个更完整的web应用程序。
我的问题是,预测现在只在我的p5草图中进行,我希望预测值被传递到prediction的App.js中以供进一步的构建。是否有这样做的方法?
我用的是反应-P5包装。
这里是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;任何帮助都很感激!
发布于 2019-01-31 06:24:42
我试了一下,想出了一个解决办法。虽然不太雅致,但应该可以。我在sketch.js中做了一个非常简单的测试项目,我试图说明两种访问信息的方法。需要注意的是timesClicked变量和updateWithProps函数。
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.updateWithProps() = function(newProps){
if(newProps.getCoords){
p.sendCoords = newProps.getCoords;
}
}
p.mouseClicked = function() {
p.sendCoords(p.mouseX, p.mouseY);
timesClicked++;
}
};timesClicked是一个可以导入的变量,它可以计算鼠标被单击的次数。它可以从草图范围内修改,并从其他文件中导入。
updateWithProps是一个函数,每当组件接收到道具并可以在草图中定义时,就从Reacti-P5包装库调用它。
这样,您的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);
}
}运行时,每次单击时,草图将在App.js文件中执行App.js ()函数,或者,每次按空格键时,都会从App.js文件访问timesClicked变量。我认为您可以修改它,以便“发送”或“读取”预测值。
发布于 2022-08-09 15:38:59
从朱利安先前的回答中更新:截至目前(2022年8月),p.myCustomRedrawAccordingToNewPropsHandler()已改名为p.updateWithProps()
https://stackoverflow.com/questions/54432515
复制相似问题