我在树莓派 4 上有以下项目:我用 python 创建了一个人脸识别脚本,需要虚拟环境才能运行。该脚本打印出已检测到的人。
在 NodeJS 中,我想通过在节点(缩小版本)中运行脚本来接收答案:
const http = require("http");
const server = http.createServer((req, res) => {
var spawn = require('child_process').spawn,
py = spawn('python', ['faceReg.py'],)
py.stdout.on('data', function(data){
console.log('Data:' + data);
});
py.stdout.on('end', function(){
console.log('Python ended');
});
});
Run Code Online (Sandbox Code Playgroud)
当执行代码时,我立即得到一个“python结束”。
在我的 pi 上,当我在执行前运行以下命令时,我可以运行脚本:
source ~/.virtualenvs/cv2_env/bin/activate
Run Code Online (Sandbox Code Playgroud)
python脚本基本上是:
stop = False
while(stop==False):
print("Peter")
Run Code Online (Sandbox Code Playgroud)
更新
跑步时
py = spawn('~/.virtualenvs/cv2_env/bin/python', ['faceReg.py'])
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
events.js:174
throw er; // Unhandled 'error' event
^
Error: spawn ~/.virtualenvs/cv2_env/bin/python ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
at onErrorNT (internal/child_process.js:415:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
at …Run Code Online (Sandbox Code Playgroud)