【问题标题】:Python and JavaScript linkPython 和 JavaScript 链接
【发布时间】:2021-02-23 10:32:03
【问题描述】:

我试图通过使用 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, 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 

【问题讨论】:

  • 出了什么问题?用错误/输出更新问题,以便人们可以尝试回答。 ;)
  • 这里是错误输出 "index.js:35 Uncaught ReferenceError: Cannot access 'PythonShell' before initialization at jsFunction

标签: javascript electron revitpythonshell


【解决方案1】:

我很确定

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

应该阅读

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

另外,您将 alert 拼错为 aler

【讨论】:

  • 谢谢。错误消息还显示,在“加入”方法 8index.js:31 Uncaught TypeError: Cannot read property 'join' of undefined at getPrediction (index.js:31) at HTMLButtonElement.onclick (index.html:29)
  • 如果没有深入的 PythonShell 知识,我认为我无法解决这个问题。您应该将其作为一个新问题提出。
  • 可能是拼写错误:pyPaht -> pyPath?
  • 听起来path.join 失败了。也许require('path') 没有返回对象。
猜你喜欢
  • 2013-09-19
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 2011-07-09
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多