当前位置:网站首页>基于opencv实现人脸检测
基于opencv实现人脸检测
2022-07-31 02:02:00 【程序小黑】
人脸检测是对人脸图片进行处理的前提条件,如果需要进行学习,那么数据集的采样可以配合人脸检测功能进行联动采集和过滤。此处,我们主要采用python程序设计语言来对图片中的人脸信息进行处理。
首先我们需要在我们的计算机中安装opencv,如果安装出现问题的,可以尝试通过anaconda进行一个新环境的安装。
首先我们导入模块
import cv2 #opencv2库
import matplotlib.pyplot as plt #plt绘图库
import numpy as np
import os
import scipy.io
在这里,我们不仅要对我们采集到的人脸进行检测,我们还需要将一个画面中出现的不同的人脸打上一个标签,如果出现两个人脸相同的情况,那么我们认定他们是同一个人,就将他们的信息存储在一起。(这部分代码暂不放出)
获得人脸信息
在人脸检测的代码中,我们首先需要载入我们的级联分类器,在opencv中,我们的级联分类器的函数为:CascadeClassifier
我们载入开源的haarcascade_frontalface_default.xml人脸分类器
下载链接:https://github.com/kipr/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
我们通过级联分类器对我们级联分类器分类出的信息进行过滤,获得我们想要的人脸数据。我们可以使用 分类器的detectMultiScale方法。
该方法中的image参数需要是一个图片信息,scanleFactor参数则是指定在每个图像比例下图像尺寸减小多少的参数,minNeighbors参数指定每个候选矩形应保留多少个邻居的参数,我们在这里对scanleFactor设置为1.5,对minNeighbors设置为5。
那么我们的代码如下:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(image = img, scaleFactor = 1.5, minNeighbors = 5)
我们得到的faces是一个向量数组(类似于pytorch中的张量,也就是tensor),但是其本质是数组,我们通过len方法获得faces数组中的个数,那么如果len(faces)<0,我们的图片中就没有检测到任何的人脸。
人脸裁剪
通过for循环遍历faces数组,获得我们的像tensor实际上是numpy.Array的数据类型,并通过start_pos和end_pos获得在二维坐标系的起始坐标和对角线上的结束坐标。得到了一个矩形。
for (x, y, w, h) in faces:
start_point = (x,y)
end_point = (x+w,y+h)
#图像绘图
cv2.rectangle(img, start_point, end_point, (255, 0, 0), 2) #color参数的顺序是BGR,因此(255,0,0)显示的是蓝色的线条,##最后一个参数表示矩形框的厚度。
#图像裁剪,并保存为temp.png
crop = img[y:(y + h), x:(x + w)]
人脸绘制
在裁剪后,我们对图像进行最后的处理,暂时我们的处理结果
finalimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.figure(figsize=(5,5))
plt.imshow(finalimg)
plt.axis("off")
plt.show()
完整代码暂不放出。
效果展示
输入图像
输出图像
而在实际的使用过程中,我们把相同的人脸放在同一个文件夹中,并准备进行后续的操作。
例子:
对于类似的图片流,我们或许可以用一些技术手段来做一些面向未来的事情~!
边栏推荐
- leetcode-128: longest continuous sequence
- 12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
- pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
- rpm安装postgresql12
- Force buckled brush the stairs (7/30)
- cudaMemcpy学习笔记
- Inner monologue from a female test engineer...
- Observer mode (1)
- Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
- 用户交互+格式化输出
猜你喜欢
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
coldfusion8 background scheduled tasks take shell
英特尔软硬优化,赋能东软加速智慧医疗时代到来
Force buckled brush the stairs (7/30)
mysql 视图
After reading "MySQL Database Advanced Practice" (SQL Xiao Xuzhu)
ShardingJDBC usage summary
Introduction and use of Drools WorkBench
Observer mode (1)
系统需求多变如何设计
随机推荐
Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
Between two orderly array of additive and Topk problem
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
leetcode-1161:最大层内元素和
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
First acquaintance with C language -- array
Simple confession page
项目开发软件目录结构规范
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
Charging effect simulation
pc端判断当前使用浏览器类型
[1153] The boundary range of between in mysql
BAT卖不动「医疗云」:医院逃离、山头林立、行有行规
PDF split/merge
The final exam first year course
PDF 拆分/合并
Is there a way to earn 300 yuan a day by doing a side business?
Path and the largest
vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
The PC side determines the type of browser currently in use