智能识别系统----视频人脸检测(一)

有空的时候把项目部署到github上

项目目录

在这里插入图片描述

提取人脸

首先编写一个人脸检测的算法

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageDraw
import traceback
def face_detection_(image,scaleFactor_,minNeighbors_):
    '''
    输入图像,返回人脸图片
    '''
	# 转成灰度图像
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    # 创建一个级联分类器 加载一个.xml分类器文件 它既可以是Haar特征也可以是LBP特征的分类器
    face_detecter = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
    # 多个尺度空间进行人脸检测   返回检测到的人脸区域坐标信息
    faces = face_detecter.detectMultiScale(image=gray, scaleFactor=scaleFactor_, minNeighbors=minNeighbors_)
    # print('检测人脸信息如下:\n', faces)
    image=cv.cvtColor(image, cv.COLOR_BGR2RGB)
    # for x, y, w, h in faces:
    #     # 在原图像上绘制矩形标识
    #     cv.rectangle(img=image, pt1=(x, y), pt2=(x+w, y+h), color=(0, 0, 255), thickness=2)
    # plt.imshow(image)
    # assert faces.shape[0]==1
    try:
        (x,y,w,h)=faces[0]
        face_=image[y:y+h,x:x+w,:]
    except Exception as e:
        # print('faces: ',faces)
        # print('it may be cause by scaleFactor or minNeighbors, that the face is not be recognize')
        # print('so i would just return null')
        # traceback.print_exc()
        return None
    return face_# 返回单张人脸
    '''这是多张人脸的提取,以后再搞'''
    # image_list=[]
    # try:
    #     # 提取多张人脸传入list
    #     for (x, y, w, h) in faces:
    #         image_list.append(image[y:y+h,x:x+w,:])
    # except Exception as e:
    #     print('faces: ',faces)
    #     print('it may be cause by scaleFactor or minNeighbors, that the face is not be recognize')
    #     print('so i would just return null')
    #     traceback.print_exc()
    #     return None
    # # 返回人脸图像list
    # return image_list

测试的效果

filepath=r'..\data\db\6_FaceOcc2\train\0003.jpg'
src=cv.imread(filepath)
face_detecter = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
# 多个尺度空间进行人脸检测   返回检测到的人脸区域坐标信息
faces = face_detecter.detectMultiScale(image=src, scaleFactor=1.03, minNeighbors=20)
for x, y, w, h in faces:
        # 在原图像上绘制矩形标识
        cv.rectangle(img=src, pt1=(x, y), pt2=(x+w, y+h), color=(0, 0, 255), thickness=2)
plt.imshow(src)
faces

在这里插入图片描述
下一步,我们要将这个算法自动化,即自动对图像数据集进行人脸检测与分割,并将分割好的人脸图像保存在人脸数据集目录

首先创建这个人脸数据集目录

import os

def mkdir(path):
	folder = os.path.exists(path)
	if not folder :                   
		os.makedirs(path) 
	else: 
		print('dir is existed')        
		
file_path=r'data\db_face\\'
dir_list=os.listdir(r'..\data\db')
for dir in dir_list:
	filePath=file_path+dir
	mkdir(filePath)
	mkdir(filePath+'\\train')
	mkdir(filePath+'\\test')

然后写了一个全自动的人脸提取器,可以对所有类别的人的图像进行人脸提取

data_path=r'..\data\db\\'

for db_name in os.listdir(data_path):
    tt_path=os.path.join(data_path,db_name)
    for data_set 
  • 17
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值