【问题标题】:No Response After Running Python Script Using Python-Shell使用 Python-Shell 运行 Python 脚本后没有响应
【发布时间】:2019-08-23 03:05:28
【问题描述】:

我有一个 nodeJS 应用程序,我需要运行一个 python 脚本才能获得特定的响应。我正在使用 python-shell 来做到这一点,但我没有得到任何回应。

我也尝试过使用子进程,同样的响应。

这里我调用python脚本:

var ps = require('python-shell');
ps.PythonShell.run('./face_detect.py', array1, function (err, data) {
    if (err) req.send(err);
    req.send(data.toString())
}); 

这是我的 python 脚本的 sn-p:

import cv2
import sys
import os
import numpy as np

students = sys.argv[1]
# get the names and put them in an array ---> subjects
imagePath = "class/welcome.jpg"
cascPath = "haarcascade_frontalface_alt.xml"

faceCascade = cv2.CascadeClassifier(cascPath)

.....
for (x, y, w, h) in faces:
    num = 0
    crop_img = cv2.UMat(image[y-40:y+h+100,x-40:x+h+40])
    cv2.imwrite("face" + str(num) + ".jpg", crop_img)
    test_img = cv2.imread("face" + str(num) + ".jpg")
    num = num + 1
    predicted_img1 = predict(test_img)
absences["A"] = 1
for name, a in absences.items():
    if a == 0:
        noshow.append(name)
print(noshow)
cv2.waitKey(0)

我希望它返回一个数组。 谁能帮我解决这个问题?

【问题讨论】:

  • cv2.waitkey(0) 之前的最终打印结果是什么?
  • 这是一个字符串数组
  • 您正在打印的数组noshow 是否有您要查找的数据?
  • 是的,我的问题是 nodejs 中的函数返回空

标签: javascript python node.js no-response


【解决方案1】:

将参数从 Nodejs python-shell 传递到 Python 脚本的 correct syntax 是:

ps.PythonShell.run('./face_detect.py', { args: array1 }, function (err, data) { ... })

这里 Python 脚本中的 sys.argv[1] 值将不包含 Nodejs array1 值,因为您没有在 PythonShell 选项中设置 args 属性。

还要注意,这可能应该是 res.send 而不是 req.send,具体取决于您的程序,如果出现错误以防止“标头已发送”异常,我建议您使用 return

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 2017-07-25
    相关资源
    最近更新 更多