簡體   English   中英

使用Python-Shell運行Python腳本后無響應

[英]No Response After Running Python Script Using Python-Shell

我有一個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)

我希望它返回一個數組。 誰能幫我這個?

將參數從Nodejs python-shell傳遞到Python腳本的正確語法是:

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

在這里,Python腳本中的sys.argv[1]的值將不包含Nodejs array1值,因為您沒有在PythonShell選項中設置args屬性。

還請注意,這可能應該是res.send而不是req.send ,具體取決於您的程序,並且我建議您return是否存在錯誤以防止出現“已發送標頭”異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM