【发布时间】:2016-10-26 16:30:45
【问题描述】:
我使用以下代码使用 OpenCv Python 提供的 Haar 级联分类器检测人脸。但是没有检测到人脸,也没有绘制人脸周围的正方形。如何解决?
import cv2
index=raw_input("Enter the index No. : ")
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
cap = cv2.VideoCapture(0)
cont=0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=10,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
for (x, y, w, h) in faces:
#cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('frame',frame)
inpt=cv2.waitKey(1)
if inpt & 0xFF == ord('q'):
break
elif inpt & 0xFF == ord('s') :
#name='G:\XCODRA\Integrated_v_01\EigenFaceRecognizer\img2'+index+"."+(str(cont))+".png"
name='IC_image\\'+index+"."+(str(cont))+".png"
resized = cv2.resize(gray,None,fx=200, fy=200, interpolation = cv2.INTER_AREA)
img=cv2.equalizeHist(resized)
cv2.imwrite(name,img)
print cont
cont+=1
【问题讨论】:
-
代替 cascPath = "haarcascade_frontalface_default.xml" 是否可以插入绝对完整路径,例如 cascPath = "C:\\folder\\haarcascade_frontalface_default.xml" 或 cascPath = "/folder/haarcascade_frontalface_default.xml" ?如果你在那里使用反斜杠,请确保使用双反斜杠(因为字符串转义字符)。
-
使用绝对路径解决了问题。非常感谢。
-
您可以使用关系路径,但您必须确保使用正确的工作目录。