我正在尝试使用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)
答案 0 :(得分:2)
对我来说,它适用于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()