2011-04-29 105 views
1

用下面的代碼:opencv的:進口highgui與蟒蛇

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

回溯(最近通話最後一個): 文件 「pycam.py」,第21行,在 重複() 文件「pycam。 py「,第12行,重複 c = highgui.cvWaitKey(10) NameError:全局名稱'highgui'未定義 已清理攝像頭。

回答

0

這意味着highgui不存在。嘗試以這種方式導入:from opencv import *

如果這不起作用,請嘗試並檢查您的opencv安裝。

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

這是一個更簡單,更乾淨的語法!

+0

你如何釋放捕獲? – auraham 2012-10-21 23:27:18