openCV|Haar人脸检测与提取

python3.5.2+openCV3.2.0

import cv2
import numpy as np

img = cv2.imread('F:\\jaffe\\KA.AN1.39.tiff')
# 读取训练好的haar分类器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# 图像灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 运用Haar分类器扫描图像识别人脸
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=10,minSize=(30, 30),flags=0)
 
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces found", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

错误:

runfile('D:/program/python/tensorflow/minist_data_test.py', wdir='D:/program/python/tensorflow')
Traceback (most recent call last):

  File "", line 1, in 
    runfile('D:/program/python/tensorflow/minist_data_test.py', wdir='D:/program/python/tensorflow')

  File "E:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "E:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/program/python/tensorflow/minist_data_test.py", line 48, in 
    faces = face_cascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=10,minSize=(30, 30),flags=0)

error: D:\Build\OpenCV\opencv-3.2.0\modules\objdetect\src\cascadedetect.cpp:1681: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

在引入haar那个xml文件的位置,加入打印文件内容,看看能不能找到这个文件:

f= open('haarcascade_frontalface_default.xml','r')
print(f.read())
果真没有找到:

FileNotFoundError: [Errno 2] No such file or directory: 'haarcascade_frontalface_default.xml'

去下载opencv_python-3.2.0+contrib-cp35-cp35m-win_amd64.whl,不知道是不是没有装它的原因。

再去装就装不上了,因为之前已经有了一个版本了,所以再装装不上。

后来从同学那里搞来了

haarcascade_frontalface_default.xml

把它放在程序目录里就好啦=0=


更新程序:检测+提取

import cv2
import numpy as np
import os

def fetch_face_pic(img,face_cascade):
    # 将图像灰度化
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 人脸检测
   faces = face_cascade.detectMultiScale(gray,scaleFactor=1.1,
                                          minNeighbors=10,minSize=(30, 30),flags=0)    
    for (x, y, w, h) in faces:
        #cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)  # 使用rectangle()可以绘出检测出的人脸区域
        crop = img[y:y+h, x:x+w] # 使用切片操作直接提取感兴趣的区域
    return crop

# openCV里已经训练好的haar人脸检测器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

path_jaffe='F:\\FERdatabase\\ckplus_64\\anger'

#遍历处理文件里所有人脸图像
for file in os.listdir(path_jaffe):
    jaffe_pic = os.path.join(path_jaffe,file)
    img = cv2.imread(jaffe_pic)
    crop = fetch_face_pic(img,face_cascade)
    
    #cv2.imshow("Faces found", img)
    #cv2.imshow('Crop image', crop)  
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()
    # 将图像缩放到64*64大小
   resized_img=cv2.resize(img,(64,64),interpolation=cv2.INTER_CUBIC)
    #保存图像
   cv2.imwrite(jaffe_pic, resized_img)  

在提取人脸的时候,遇到了问题,就是提取的人脸和检测到的人脸不重合,

发现是这里的问题,下面为正确写法:

crop = img[y:y+h, x:x+w]

自己误写为:

crop = img[x:x+w,y:y+h]
改正就好啦~

你可能感兴趣的:(毕业设计)