导入opencv模块

2024-09-28 05:19:56 发布

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

我有一个简单的代码,如下所述:

import cv
from opencv.cv import *
from opencv.highgui import *

img = cv.LoadImage("test.jpg")
cap = cv.CreateCameraCapture(0)
while cv.WaitKey(1) != 10:
    img = cv.QueryFrame(cap)
    cv.ShowImage("cam view", img)
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))    

但我面对这个错误:

# AttributeError: 'module' object has no attribute 'LoadImage'

当我将代码更改为以下内容时:

import cv
#from opencv.cv import *
#from opencv.highgui import *

img = cv.LoadImage("test.jpg")
cap = cv.CreateCameraCapture(0)
while cv.WaitKey(1) != 10:
    img = cv.QueryFrame(cap)
    cv.ShowImage("cam view", img)
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))    

现在第一个错误得到了解决,另一个错误又出现了。

AttributeError: 'module' object has no attribute 'LoadHaarClassifierCascade'

我需要这两个模块,但似乎他们有冲突得到她。

现在我该怎么办?


Tags: 代码fromtestimportimg错误opencvcv
2条回答

你怎样才能拿到你进口的东西?

# imports the cv module, all stuff contained in it and
# the module itself is now accessible via: cv.classname, cv.functionname
# where classname, functionname is the name of the class/function which
# the cv module provides..
import cv

# imports everything contained in the opencv.cv module
# after this import it is available via it's classname, functionname, etc.
# Attention: without prefix!!
from opencv.cv import *

# @see opencv.cv import 
from opencv.highgui import *

@有关python中的模块和导入的详细信息,请参见python modules

如果您可以提供哪些类包含在哪个模块中,我可以为您的问题添加特定的解决方案。

在OpenCV中加载haar分类器(无论如何在python接口中)只需使用cv.load。

import cv
cascade = cv.Load('haarcascade_frontalface_alt.xml')

请参阅示例here

而且,OpenCV源代码附带的示例非常好(在OpenCV-2.xx/samples/python)。

相关问题 更多 >

    热门问题