我正在尝试使用OpenCV库创建面部和眼睛检测。这是我一直在使用的代码。它运行顺利,没有错误,但是唯一的问题是没有显示任何结果,使用此代码没有发现任何面孔和眼睛

import cv2
import sys
import numpy as np
import os

# Get user supplied values
imagePath = sys.argv[1]


# Create the haar cascade
faceCascade = cv2.CascadeClassifier('C:\Users\Karthik\Downloads\Programs\opencv\sources\data\haarcascades\haarcascad_frontalface_default.xml')
eyeCascade= cv2.CascadeClassifier('C:\Users\Karthik\Downloads\Programs\opencv\sources\data\haarcascades\haarcascade_eye.xml')

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(30, 30),
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

print "Found {0} faces!".format(len(faces))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = image[y:y+h, x:x+w]

    eyes = eyeCascade.detectMultiscale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0, 255, 0), 2)


cv2.imshow("Faces found", image)
print image.shape
cv2.waitKey(0)

最佳答案

对我来说,它可以在Ubuntu 15.10上的jupyter笔记本中使用OpenCV 3.1.0-dev和python 3.4

可能是您有一个简单的错字?
haarcascad_frontalface_default.xml => haarcascade_frontalface_default.xml
和这里:
eyes = eyeCascade.detectMultiscale(roi_gray)=> eyeCascade.detectMultiScale(roi_gray)
那是我的工作代码:

%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt

import cv2
import sys
import numpy as np
import os

# Create the haar cascade
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeCascade= cv2.CascadeClassifier('haarcascade_eye.xml')

# Read the image
image = cv2.imread('lena.png', 0)

if image is None:
    raise ValueError('Image not found')

# Detect faces in the image
faces = faceCascade.detectMultiScale(image)

print('Found {} faces!'.format(len(faces)))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), 255, 2)
    roi = image[y:y+h, x:x+w]

    eyes = eyeCascade.detectMultiScale(roi)
    for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi,(ex,ey),(ex+ew,ey+eh), 255, 2)


plt.figure()
plt.imshow(image, cmap='gray')
plt.show()

python - Python-无法检测到脸部和眼睛?-LMLPHP

关于python - Python-无法检测到脸部和眼睛?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39314610/

10-12 22:07
查看更多