hear识别人脸

# -*- coding: utf-8 -*-
"""
Created on Tue Feb 27 14:56:16 2018


@author: Administrator
"""


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,1.3,5)
    global crop########否则未声明下面无法调用
    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] # 使用切片操作直接提取感兴趣的区域
        #crop=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    
    return crop


# openCV里已经训练好的haar人脸检测器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load('D:\opencv\sources\data\haarcascades/haarcascade_frontalface_default.xml')
path_jaffe='E:\\HOG_SVM\\files_smile'
path_face='E:\\HOG_SVM\\face_smile'
#遍历处理文件里所有人脸图像
for file in os.listdir(path_jaffe):
    jaffe_pic = os.path.join(path_jaffe,file)
    img = cv2.imread(jaffe_pic)
    path_face = os.path.join(path_face,file)
    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(crop,(100,100),interpolation=cv2.INTER_CUBIC)
    #保存图像
    cv2.imwrite(path_face, resized_img)  


    
    
    
#############################################################
'''
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


faces=face_cascade.detectMultiScale(gray,1.3,5)
for (x,y,h,w) in faces:
    img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.namedWindow('faces Detected!')
cv2.imshow('faces Detected!',img)
cv2.imwrite('faces.jpg',img)
cv2.waitKey(0)
'''

你可能感兴趣的:(算法,进步)