Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每次我调用NodeJS子进程Spawn时,它都会运行整个python代码_Javascript_Python_Node.js_Tensorflow_Spawn - Fatal编程技术网

Javascript 每次我调用NodeJS子进程Spawn时,它都会运行整个python代码

Javascript 每次我调用NodeJS子进程Spawn时,它都会运行整个python代码,javascript,python,node.js,tensorflow,spawn,Javascript,Python,Node.js,Tensorflow,Spawn,我有一段将Nodejs连接到Python脚本的代码。该脚本包含带有Tensor flow后端的ML模型等等,它基本上给出了一个字符串输出。我通过.child-process-spawn将一个图像URL从节点js发送到python,并将其识别的表达式作为字符串返回。基本上,我在做面部识别,用python编码,但通过节点js调用,并将字符串作为JSON数据(RESTAPI)发送到响应 我面临的问题是,每当我调用spawn时,它都会运行整个python代码,只要python脚本必须加载所有模块(如果我

我有一段将Nodejs连接到Python脚本的代码。该脚本包含带有Tensor flow后端的ML模型等等,它基本上给出了一个字符串输出。我通过.child-process-spawn将一个图像URL从节点js发送到python,并将其识别的表达式作为字符串返回。基本上,我在做面部识别,用python编码,但通过节点js调用,并将字符串作为JSON数据(RESTAPI)发送到响应

我面临的问题是,每当我调用spawn时,它都会运行整个python代码,只要python脚本必须加载所有模块(如果我们从顶部开始并最终给出输出),它就需要花费很长时间

下面是python代码

from gtts import gTTS
language = 'en'
#myobj = gTTS(text='Do you know the person? Yes or No', lang=language, slow=True)
#myobj.save("question1.mp3")
#myobj = gTTS(text='What is his or her name', lang=language, slow=True)
#myobj.save("question2.mp3")
import csv
import pandas as pd   
import numpy as np
#with open('database.csv','w') as f:
 # writer=csv.writer(f) 
 # writer.writerow(['Chinmay',embedded])
face_embeddings=np.array(pd.read_csv('database.csv',header=None))
face_names=np.array(pd.read_csv('database_names.csv',header=None))

from cv2 import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches

from align import AlignDlib
import numpy as np 
import matplotlib.pyplot as plt
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
import os 
from torch.autograd import Variable  
from model import create_model

import transforms as transforms
from skimage import io
from skimage.transform import resize 
from models import *

import matplotlib.pyplot as plt
from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
from util.model import CNNModel, generate_caption_beam_search
import os

from config import config
from pickle import load
import sys

cut_size = 44
transform_test = transforms.Compose([
    transforms.TenCrop(cut_size),
    transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
])
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
final_text=''
nn4_small2_pretrained = create_model()
nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5')

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

def load_image(path): 
    img = cv2.imread(path, 1)
    # OpenCV loads images with color channels
    # in BGR order. So we need to reverse them
    return img[...,::-1]


def extract_features(filename, model, model_type):
    if model_type == 'inceptionv3':
        from keras.applications.inception_v3 import preprocess_input
        target_size = (299, 299)
    elif model_type == 'vgg16':
        from keras.applications.vgg16 import preprocess_input
        target_size = (224, 224)
      # Loading and resizing image
    image = load_img(filename, target_size=target_size)
    # Convert the image pixels to a numpy array
    image = img_to_array(image)
    # Reshape data for the model
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    # Prepare the image for the CNN Model model
    image = preprocess_input(image)
    # Pass image into model to get encoded features
    features = model.predict(image, verbose=0)
    return features
def getrecogstr( imgurl ): 

    # Path of Image
    #image_file=imgurl

    image_file = sys.argv[1]

    # Initialize the OpenFace face alignment utility
    alignment = AlignDlib('models/landmarks.dat')

    # Load an image
    jc_orig = load_image(image_file)

    # Detect face and return bounding box -
    bb = alignment.getAllFaceBoundingBoxes(jc_orig)
    net = VGG('VGG19')
    checkpoint = torch.load(os.path.join('FER2013_VGG19', 'PrivateTest_model.t7'),map_location='cpu')
    net.load_state_dict(checkpoint['net'])

    # Load the tokenizer
    tokenizer_path = config['tokenizer_path']
    tokenizer = load(open(tokenizer_path, 'rb'))

    # Max sequence length (from training)
    max_length = config['max_length']
    caption_model = load_model('model.hdf5')
    image_model = CNNModel(config['model_type'])

    for i in bb:

        # Transform image using specified face landmark indices and crop image to 96x96
        jc_aligned = alignment.align(96, jc_orig, i, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE)

        location=(i.height()+i.width())/(jc_orig.shape[0]+jc_orig.shape[1])

        #  Finding the emotion of cropped image
        gray = rgb2gray(jc_aligned)
        gray = resize(gray, (48,48), mode='symmetric').astype(np.uint8)
        img = gray[:, :, np.newaxis]

        img = np.concatenate((img, img, img), axis=2)
        img = Image.fromarray(img)
        inputs = transform_test(img)

        #net.cuda()
        net.eval()
        ncrops, c, h, w = np.shape(inputs)

        inputs = inputs.view(-1, c, h, w)
        #inputs = inputs.cuda()
        inputs = Variable(inputs, volatile=True)
        outputs = net(inputs)
        outputs_avg = outputs.view(ncrops, -1).mean(0)  # avg over crops
        score = F.softmax(outputs_avg)
        _, predicted = torch.max(outputs_avg.data, 0)
    # Find the name of the person in the image

        jc_aligned = (jc_aligned / 255.).astype(np.float32)
        embeddings = nn4_small2_pretrained.predict(np.expand_dims(jc_aligned, axis=0))[0]
        print("@@")
        print(embeddings)
        matched_embeddings=1000

        for j in range(len(face_embeddings)): 
            temp=np.sum(np.square(embeddings-face_embeddings[j]))
            if (temp<=0.56 and temp <matched_embeddings):
                matched_embeddings=np.sum(np.square(embeddings-face_embeddings[j]))
                face_index=j
        print(temp)
        print('above')
        if matched_embeddings!=1000:
            face_name=face_names[face_index][0]
            print("@@known")
        else:
            face_name='Unknown'
            print("@@unknown")
            #print("Unknown Person detected. Do you know this person yes or no ?")
            #Play welcome1.mp3

            #Play welcome2.mp3 if input is yes

        final_text+= face_name+' expression is '+class_names[int(predicted.cpu().numpy())] + "."
        print("@@"+final_text)
        sys.stdout.flush()
getrecogstr()



我只想跳过每次加载模块的那一部分。我知道我们必须运行模块才能将其存储在内存中,但这需要很长时间。可以像Python脚本那样一直运行,我们可以在运行过程中从节点JS发送参数并调用一个可以返回该字符串的函数,

您可以在节点和生成的Python进程之间使用全局变量和消息通信。 我从教程中得到了关于消息队列的想法,但这里可以应用相同的方法

app.js

const-app=require('express')();
const uuid=require('uuid');
const spawn=require(“子进程”)。spawn;
var py=spawn('python',[“/face.py]”);
var globalobj={}
//任何数据到达时,都将存储在globalobj中。
py.stdout.on('data',函数(data){
试一试{
const{id,msg}=JSON.parse(data.toString());
globalobj[id]=msg;
}捕捉(错误){
//如果接收到的数据块不完整(子进程发送了大量输出),json解析将失败。
}
});
常量延迟=()=>新承诺(解析=>{
设置超时(()=>{
解决();
}, 4000);
});
app.get('/test',异步(req,res,next)=>{
const url=req.query.imgurl;
const id=uuid.v4();
write(JSON.stringify({id,url})+“\n”);
等待延迟();
//如果没有来自子进程的响应,globalobj将没有id键。
if(globalobj[id]!=未定义){
res.send(globalobj[id]);
删除globalobj[id];
}否则{
res.status(500).send('No response from child process');
}
});
app.listen(3000,'localhost',()=>{
log(`server start on port 3000`);
});
缺点是在延迟之后得到响应的消息将累积在全局对象中。另外,
py.stdout.on('data',函数(data){})
返回流中的数据,因此如果消息较大,它将被nodejs分割成块。看到这个了吗

可以找到在写入子stdin时使用
\n
的原因

main.py

导入系统,json 尽管如此: stdin=sys.stdin.readline() 如果标准输入: data=json.loads(stdin) #你在这里计算吗 打印(json.dumps({'id':data['id'],'msg':'your message'}),flush=True) stdin=无
当我快速测试时,它工作了,但可能不是在所有情况下都工作。在使用此方法之前,请仔细测试。

您的问题是,您的python代码在运行python代码时会运行?如果您希望重复运行python,但又不需要重复启动成本,则需要实现某种形式的python守护进程或服务器,以便将请求转发到该服务器。否,我不想从一开始就运行整个python代码,我只想调用函数getrecogstrI(),它需要加载所有模块并需要时间(比如10秒左右)。所以,是的,您需要实现一个python守护进程或服务器来公开该函数。我们如何做到这一点?一些代码有帮助
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const port = 1000;
const spawn = require("child_process").spawn;
app.use(bodyParser.json()); // application/json

app.use((req, res, next) => {
 res.setHeader('Access-Control-Allow-Origin', '*');
 res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, PATCH, DELETE');
 res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
 next();
});
app.get('/test', (req, res, next) => {
 const imgurl = req.query.imgurl;
var process = spawn('python', ["./final.py",
   imgurl, 
 ]); 
 process.stdout.on('data', function (data) {
   const recog_str = data.toString().split('@@')[3];
   console.log(recog_str); 
 res.json(recog_str)

 })
})


server.listen(port, () => {

 console.log("Ok");
})