我有一个 Express Node.js 应用程序,但我想使用以下命令运行机器学习 python codw。
我已经找到了一些在线解决方案来运行 python 机器学习代码。通过此 stackoverflow 链接 How to call a Python function from Node.js
但通过这个解决方案,我只需运行 python 代码。
var spawn = require("child_process").spawn;
var process = spawn('python', ["./my_script.py"]);
process.stdout.on('data', function (data) {
res.send(data.toString());
})
使用此代码,我只需运行代码 python hello.py
。但我想使用以下命令运行代码 python test.py --model dataset/test.model --图片s.jpg
最佳答案
您可以使用python-shell
import {PythonShell} from 'python-shell';
const options = {
args: [
'--image',
's.jpg'
]
};
PythonShell.run('script.py', options, (err, results) => {
// your code
});
如果你想使用spawn,你可以这样做:
const args = ['script.py', '--image', 's.jpg'];
const process = spawn('python', args);
关于python - 如何使用node js运行python程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651642/