当前位置:网站首页>Face detection based on opencv
Face detection based on opencv
2022-07-31 02:10:00 【program black】
Face detection is a prerequisite for processing face pictures,Study if needed,Then the sampling of the data set can be combined with the face detection function for linked collection and filtering.此处,我们主要采用pythonProgramming language to process the face information in the picture.
First we need to install in our computeropencv,If there is a problem with the installation,可以尝试通过anacondaDo a new environment installation.
First we import the module
import cv2 #opencv2库
import matplotlib.pyplot as plt #plt绘图库
import numpy as np
import os
import scipy.io
在这里,We don't just have to detect the faces we collect,We also need to label the different faces that appear in a frame,If two faces are the same,Then we assume they are the same person,store their information together.(This part of the code is not released yet)
Get face information
in the code for face detection,We first need to load our cascaded classifier,在opencv中,The function of our cascaded classifier is :CascadeClassifier
We load open sourcehaarcascade_frontalface_default.xml人脸分类器
下载链接:https://github.com/kipr/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
We filter the information classified by our cascade classifier through the cascade classifier,Get the face data we want.我们可以使用 分类器的detectMultiScale方法.
该方法中的imageThe parameter needs to be an image information,scanleFactorParameters are parameters that specify how much the image size is reduced at each image scale,minNeighborsThe parameter specifies how many neighbors each candidate rectangle should retain,我们在这里对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),But its essence is an array,我们通过len方法获得faces数组中的个数,那么如果len(faces)<0,There are no faces detected in our images.
人脸裁剪
通过for循环遍历faces数组,Get our imagetensor实际上是numpy.Array的数据类型,并通过start_pos和end_posGet the start coordinate and the end coordinate on the diagonal in the 2D coordinate system.got a rectangle.
for (x, y, w, h) in faces:
start_point = (x,y)
end_point = (x+w,y+h)
#image drawing
cv2.rectangle(img, start_point, end_point, (255, 0, 0), 2) #color参数的顺序是BGR,因此(255,0,0)Shown are blue lines,##The last parameter is the thickness of the rectangle.
#图像裁剪,并保存为temp.png
crop = img[y:(y + h), x:(x + w)]
人脸绘制
在裁剪后,We do the final processing on the image,Temporarily our processing results
finalimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.figure(figsize=(5,5))
plt.imshow(finalimg)
plt.axis("off")
plt.show()
The complete code is not released yet.
效果展示
输入图像

输出图像

而在实际的使用过程中,We put the same faces in the same folder,and ready for subsequent operations.
例子:

For similar picture streams,We may be able to use some technical means to do some future-oriented things~!
边栏推荐
- leetcode-952:按公因数计算最大组件大小
- 怎样做好一个创业公司CTO?
- There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
- First acquaintance with C language -- array
- How to design the changing system requirements
- PDF 拆分/合并
- Inner monologue from a female test engineer...
- coldfusion文件读取漏洞(CVE-2010-2861)
- C语言小程序 -- 常见经典练习题
- Introduction and use of Drools WorkBench
猜你喜欢

直播预告 | KDD2022博士论文奖冠亚军对话

基于FPGA的售货机

mmdetection训练一个模型相关命令

What are the project management tools like MS Project

Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值

leetcode-952: Calculate max component size by common factor

ShardingJDBC使用总结

Drools Rule Properties, Advanced Syntax

Unity界面总体介绍

The final exam first year course
随机推荐
ShardingJDBC usage summary
The effective square of the test (one question of the day 7/29)
12张图带你彻底搞懂服务限流、熔断、降级、雪崩
《MySQL数据库进阶实战》读后感(SQL 小虚竹)
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
真正的CTO,是一个懂产品的技术人
mysql view
Tower of Hanoi problem
Teach you how to configure Jenkins automated email notifications
Unity界面总体介绍
Is there a way to earn 300 yuan a day by doing a side business?
cudaMemcpy study notes
leetcode-952:按公因数计算最大组件大小
类似 MS Project 的项目管理工具有哪些
二层广播风暴(产生原因+判断+解决)
934. 最短的桥
初识C语言 -- 数组
VSCode Plugin: Nested Comments
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
Drools WorkBench的简介与使用