Python和JavaScript链接

问题描述

我试图通过使用PYthon-shell将Javascript Electron应用程序与python深度学习引擎链接。但是我不知道现在要去哪里。

这是python代码

from keras.models import load_model
import cv2
import numpy as np
import os
from PIL import ImageTk,Image

from sklearn.preprocessing import label
import sys




model = load_model('model.h5')
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

img = cv2.imread('file/4/1.jpg')

img = np.reshape(img,[1,28,3])
class1 = model.predict_classes(img)

print("the pridicted number is",class1 + 1)

sys.stdout.flush()

javascript代码如下:

 let {PythonShell} = require('python-shell')
  var path = require('path');
function getPrediction() {
 
  
  let pyInput ={
    scriptPath : path.join('/../BackEnd/'),pyPaht :path.join( '/../BackEnd/')

  }


PythonShell = new PythonShell('numbers.py',pyInput);


pyshell.on('message',function(message) {
    alert(message);
  })

}

以下是错误输出

index.js:35 Uncaught ReferenceError: Cannot access 'PythonShell' before initialization
    at jsFunction 

解决方法

我很确定

PythonShell = new PythonShell('numbers.py',pyInput);

应阅读

pyshell = new PythonShell('numbers.py',pyInput);

另外,您将alert误写为aler