【问题标题】:Pass arguments from nodejs to python script using python-shell使用 python-shell 将参数从 nodejs 传递到 python 脚本
【发布时间】:2022-08-17 19:27:33
【问题描述】:

我正在尝试在 Nodejs 项目中运行 python 脚本。在我的 python 脚本中传递本地视频 URL 时一切正常,但我需要从我的 MongoDB 中检索视频 URL 并将其传递给 python 脚本。有没有办法我可以做到这一点?我进行了一些研究,但找不到解决此问题的方法。

app.get(\'/test\', callName); 
  
function callName(req, res) { 
  PythonShell.run(\'ai_part/test.py\', null, function (err) {  
    if (err) throw err;
    console.log(\'finished\');
  });
} 

Python代码:

#Loading the video input
cap = cv2.VideoCapture(\'test.wmv\')

_, img = cap.read()
height, width, _ = img.shape
copy = img.copy()

不知道够不够清楚,但是我需要将从我的mongo数据库获取的视频URL传入python函数\'VideoCapture(url)\'。提前致谢!

    标签: python node.js


    【解决方案1】:

    就像the documentation 告诉你的那样,

    function callName(req, res) {
      let options = {
        mode: 'text',
        args: [res]
      };
      PythonShell.run('ai_part/test.py', options, function (err) {  
        if (err) throw err;
        console.log('finished');
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2016-03-20
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2020-11-09
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多