开发者

Opencv: import highgui with python

开发者 https://www.devze.com 2023-03-01 07:38 出处:网络
With the following code: import cv cv.NamedWindow(\"w1\", cv.CV_WINDOW_AUTOSIZE) camera_index = 0 capture = cv.CaptureFromCAM(camera_index)

With the following code:

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()

Traceback (mos开发者_如何学Got recent call last): File "pycam.py", line 21, in repeat() File "pycam.py", line 12, in repeat c = highgui.cvWaitKey(10) NameError: global name 'highgui' is not defined Cleaned up camera.


There have been quite a few changes in the new API. The following will work:

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()

It is a simpler, cleaner syntax!


This means that highgui doesn't exist. Try importing this way: from opencv import *.

If that doesn't work try and check your opencv instalation.

0

精彩评论

暂无评论...
验证码 换一张
取 消