Haar-级联人脸检测OpenCv

x8diyxa7  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(142)

我使用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
ddhy6vgd

ddhy6vgd1#

使用分类器的完整路径。

3duebb1j

3duebb1j2#

你可以使用代码并尝试它是否适合你。

from __future__ import print_function
import cv2 as cv
import argparse
import numpy as np
import pywt

def detectAndDisplay(frame):
    frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    frame_gray = cv.equalizeHist(frame_gray)
    # -- Detect faces
    faces = face_cascade.detectMultiScale(frame_gray)
    for (x, y, w, h) in faces:
        center = (x + w//2, y + h//2)
        frame = cv.ellipse(frame, center, (w//2, h//2),
                           0, 0, 360, (255, 0, 0), 4)
        faceROI = frame_gray[y:y+h, x:x+w]
        # -- In each face, detect eyes
        eyes = eyes_cascade.detectMultiScale(faceROI)
        for (x2, y2, w2, h2) in eyes:
            eye_center = (x + x2 + w2//2, y + y2 + h2//2)
            radius = int(round((w2 + h2)*0.25))
            frame = cv.circle(frame, eye_center, radius, (0, 255, 0), 10)
    cv.imshow('Capture - Face detection', frame)

parser = argparse.ArgumentParser(
    description='Code for Cascade Classifier tutorial.')
parser.add_argument('--face_cascade', help='Path to face cascade.',
                    default='./opencv/haarcascade/haarcascade_frontalface_default.xml')
parser.add_argument('--eyes_cascade', help='Path to eyes cascade.',
                    default='./opencv/haarcascade/haarcascade_eye.xml')
parser.add_argument(
    '--camera', help='Camera divide number.', type=int, default=0)
args = parser.parse_args()
face_cascade_name = args.face_cascade
eyes_cascade_name = args.eyes_cascade
face_cascade = cv.CascadeClassifier()
eyes_cascade = cv.CascadeClassifier()
# -- 1. Load the cascades
if not face_cascade.load(cv.samples.findFile(face_cascade_name)):
    print('--(!)Error loading face cascade')
    exit(0)
if not eyes_cascade.load(cv.samples.findFile(eyes_cascade_name)):
    print('--(!)Error loading eyes cascade')
    exit(0)
camera_device = args.camera
# -- 2. Read the video stream
cap = cv.VideoCapture(camera_device)
if not cap.isOpened:
    print('--(!)Error opening video capture')
    exit(0)
while True:
    ret, frame = cap.read()
    if frame is None:
        print('--(!) No captured frame -- Break!')
        break
    detectAndDisplay(frame)
    if cv.waitKey(10) == 27:
        break

相关问题