gpt4 book ai didi

javascript - 使用Python-Shell运行Python脚本后无响应

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:09 25 4
gpt4 key购买 nike

我有一个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 脚本的片段:

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)

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

最佳答案

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

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

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

另请注意,这可能应该是 res.send而不是req.send ,取决于您的程序,我建议您 return如果有错误防止“ header 已发送”异常。

关于javascript - 使用Python-Shell运行Python脚本后无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55463749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com