OpenCV 3.0.0 beta Python:加载错误:'模块'对象没有属性'加载'

时间:2015-01-04 06:42:43

标签: python xml opencv opencv3.0

我正在尝试制作一个人脸检测程序,它会使用xml文件来训练分类器并从屏幕截图中识别面部,嘴巴和眼睛。

然而,当我尝试加载xml文件时,它给出了cv2没有'加载'属性。由于不同的版本和文档(使用3.0.0-bet)我之前遇到cv2的属性问题,我怀疑它是一个像语法错误一样简单的东西。但是我不确定,有人能告诉我导致问题的原因以及我该如何解决?

错误:

Traceback (most recent call last):
  File "/home/anthony/Documents/Programming/Python/Computer-Vision/Tests/nowayout.py", line 18, in <module>
    haarFace = cv2.Load('haarcascade_frontalface_default.xml')
AttributeError: 'module' object has no attribute 'Load'

代码:

# -*- coding: utf-8 -*-

from PIL import Image
from numpy import *
from pylab import *
import pyscreenshot as ImageGrab
import urllib
import cv2
#import cv

image=ImageGrab.grab()
ImageGrab.grab_to_file('image.png')

# input image
imcolor = cv2.imread('image.png') 

# loading the classifiers
haarFace = cv2.Load('haarcascade_frontalface_default.xml')
haarEyes = cv2.Load('haarcascade_eye.xml')
haarMouth= cv2.Load('haarcascade_mcs_mouth.xml')

# running the classifiers
storage = cv2.CreateMemStorage()
detectedFace = cv2.HaarDetectObjects(imcolor, haarFace, storage)
detectedEyes = cv2.HaarDetectObjects(imcolor, haarEyes, storage)
detectedMouth = cv2.HaarDetectObjects(imcolor, haarMouth, storage)

# draw a green rectangle where the face is detected
if detectedFace:
 for face in detectedFace:
  cv2.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv2.RGB(155, 105, 25),2)

# draw a purple rectangle where the eye is detected
if detectedEyes:
 for face in detectedEyes:
  cv2.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv2.RGB(155, 55, 200),2)
# draw a purple rectangle where the eye is detected
if detectedMouth:
 for face in detectedMouth:
  cv2.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv2.RGB(255, 0, 0),2)


cv2.NamedWindow('Face Detection', cv.CV_WINDOW_AUTOSIZE)
cv2.ShowImage('Face Detection', imcolor) 
cv2.WaitKey()

1 个答案:

答案 0 :(得分:1)

尝试将Load()更改为CascadeClassifier(),并将所有cv2.YourMethods更改为cv2.cv.YourMethods,看看是否有帮助。