Opencv错误初始化完成

时间:2018-04-11 14:56:15

标签: python node.js opencv

我一直使用python-shell包运行带有nodejs的python脚本..我收到此错误:

Error: init done

at PythonShell.parseError (F:\github\pythonShellDemo\node_modules\python-shell\index.js:191:17)
at terminateIfNeeded (F:\github\pythonShellDemo\node_modules\python-shell\index.js:98:28)
at ChildProcess.<anonymous> (F:\github\pythonShellDemo\node_modules\python-shell\index.js:89:9)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
经过一些调试和研究,我知道这个错误是用opencv但我找不到任何解决方案..

这是代码:

import cv2
import zbar
from PIL import Image
import sys

video = cv2.VideoCapture(0)
count=0
qrcode=[]
while True:
  ret, frame = video.read()
  cv2.imshow('Camera', frame)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break
  grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  image = Image.fromarray(grayscale)
  width, height = image.size
  zbarimage = zbar.Image(width, height,'Y800', image.tobytes())
  scanner = zbar.ImageScanner()
  scanner.scan(zbarimage)
  for x in zbarimage:
    if count == 0:
      qrcode=x.data
      count=count+1
  if qrcode:
    break
video.release()
cv2.destroyAllWindows()

print(qrcode)
sys.stdout.flush()

我正在使用python 2.7

更新: nodejs用于调用python脚本的代码:

PythonShell.run('python/scan.py', options, function (err, results) {
  if (err) {
    console.log(err)
    reject(err)
  }
  // results is an array consisting of messages collected during execution
  console.log(results)
  resolve(results)
})

更新: 我试过只运行opencv而没有zbar ..仍然有错误

import cv2

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:1)

OpenCV库将init done打印到标准错误输出。它不是错误,只是调试打印。 python-shell然后将此变为错误。来自python-shell文档:

  

如果脚本写入stderr或使用非零代码退出,则会引发错误。

似乎只能通过recompiling the library with a parameter set来抑制输出。