当前位置:网站首页>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~!
边栏推荐
- 12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
- Calculate S=a+aa+…+aa…a
- keep-alive缓存组件
- Manchester City confuses fans with smart scarf that detects emotions
- 汉诺塔问题
- Introduction to flask series 】 【 flask - using SQLAlchemy
- Coldfusion file read holes (CVE - 2010-2861)
- uniapp uses 3rd party fonts
- 真正的CTO,是一个懂产品的技术人
- mysql 索引
猜你喜欢
汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络
Drools Rule Properties, Advanced Syntax
uniapp使用第三方字体
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
CV-Model [3]: MobileNet v2
Drools基本介绍,入门案例,基本语法
Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?
CV-Model【3】:MobileNet v2
Problems that need to be solved by the tcp framework
934. 最短的桥
随机推荐
Manchester City confuses fans with smart scarf that detects emotions
AI在医疗影像设备全流程应用
Drools WorkBench的简介与使用
MySQL stored procedure
Detailed explanation of STP election (step + case)
What are the project management tools like MS Project
Gateway路由的配置方式
软件测试报告有哪些内容?
mmdetection训练一个模型相关命令
What have I experienced to become a tester who is harder than development?
ShardingJDBC usage summary
《云原生的本手、妙手和俗手》——2022全国新高考I卷作文
There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
934. The Shortest Bridge
加密生活,Web3 项目合伙人的一天
12张图带你彻底搞懂服务限流、熔断、降级、雪崩
VSCode Plugin: Nested Comments
完整复制虚拟机原理(云计算)
[1153]mysql中between的边界范围
C语言小程序 -- 常见经典练习题