使用pythonshell npm在节点js中运行python脚本时出错

2024-04-29 15:54:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个具有图像处理功能的web应用程序。因此,我使用opencv python并使用python shell包实现了节点js的python脚本

index.js

var PythonShell = require('python-shell');

var options = {
    mode: 'text',
    pythonOptions:['-u'],
    scriptPath:'C:/Users/MB/PycharmProjects/NumberPlate/venv/Lib/site-packages',
};

PythonShell.PythonShell.run('./myScript.py', options, function (err, results) {
  if (err) throw err;
  console.log('finished: %j', results);
});

myScript.py(前几行)

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'


# Read the image file
image = cv2.imread('./t30.jpg')

# Convert to Grayscale Image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Canny Edge Detection
canny_edge = cv2.Canny(gray_image, 170, 200)

我所有的导入、环境变量和映像路径都正常,当我运行myScript.py时,它运行良好,并提供了输出

错误为:

 ----- Python Traceback -----
    File "C:\Users\user\PycharmProjects\NumberPlate\venv\Lib\site-packages\platedetection2.py", line 11, in <module>
      gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) {

只有在使用python-shell执行时才会出现错误 问题是什么?通过python shell运行opencv脚本可以吗


Tags: pyimage脚本varjsshellcv2opencv