当前位置:网站首页>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~!
边栏推荐
- mysql 视图
- 来自一位女测试工程师的内心独白...
- The PC side determines the type of browser currently in use
- 软件测试缺陷报告---定义,组成,缺陷的生命周期,缺陷跟踪产后处理流程,缺陷跟踪处理流程,缺陷跟踪的目的,缺陷管理工具
- Charging effect simulation
- MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
- Programmer's debriefing report/summary
- 成为比开发硬气的测试人,我都经历了什么?
- CV-Model【3】:MobileNet v2
- ShardingJDBC usage summary
猜你喜欢
Can an inexperienced college graduate switch to software testing?my real case
934. The Shortest Bridge
Fiddler captures packets to simulate weak network environment testing
mmdetection训练一个模型相关命令
f.grid_sample
Drools WorkBench的简介与使用
【Map与Set】之LeetCode&牛客练习
两个有序数组间相加和的Topk问题
What does a software test report contain?
What are the project management tools like MS Project
随机推荐
f.grid_sample
Crawler text data cleaning
CV-Model【3】:MobileNet v2
What have I experienced when I won the offer of BAT and TMD technical experts?
来自一位女测试工程师的内心独白...
加密生活,Web3 项目合伙人的一天
Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
coldfusion8后台计划任务拿shell
力扣刷题之爬楼梯(7/30)
C language applet -- common classic practice questions
系统需求多变如何设计
软件测试报告有哪些内容?
The PC side determines the type of browser currently in use
ShardingJDBC使用总结
To write good test cases, you must first learn test design
ShardingJDBC usage summary
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
Drools Rule Properties, Advanced Syntax
STP选举(步骤+案列)详解