Opencv:用python导入highgui

rta*_*oni 1 python macos opencv

使用以下代码:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
  global capture #declare as globals since we are assigning to them now
  global camera_index
  frame = cv.QueryFrame(capture)
  cv.ShowImage("w1", frame)
  c = highgui.cvWaitKey(10)
  if(c=="n"): #in "n" key is pressed while the popup window is in focus
    camera_index += 1 #try the next camera index
    capture = cv.CaptureFromCAM(camera_index)
    if not capture: #if the next camera index didn't work, reset to 0.
        camera_index = 0
        capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()
Run Code Online (Sandbox Code Playgroud)

回溯(最近一次调用最后一次):文件"pycam.py",第21行,在repeat()文件"pycam.py"中,第12行,重复c = highgui.cvWaitKey(10)NameError:全局名称'highgui'是未定义清理过的相机.

med*_*duz 6

新API中发生了很多变化.以下将有效:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
  global capture #declare as globals since we are assigning to them now
  global camera_index
  frame = cv.QueryFrame(capture)
  cv.ShowImage("w1", frame)
  c = cv.WaitKey(10)
  if(c=="n"): #in "n" key is pressed while the popup window is in focus
    camera_index += 1 #try the next camera index
    capture = cv.CaptureFromCAM(camera_index)
    if not capture: #if the next camera index didn't work, reset to 0.
        camera_index = 0
        capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()
Run Code Online (Sandbox Code Playgroud)

这是一种更简单,更清晰的语法!