Haar- Cascade人脸检测OpenCv

时间:2016-06-24 06:27:00

标签: python opencv

我使用以下代码使用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

1 个答案:

答案 0 :(得分:2)

使用分类器的完整路径。