当前位置:网站首页>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~!
边栏推荐
- [1153]mysql中between的边界范围
- LeetCode 1161 最大层内元素和[BFS 二叉树] HERODING的LeetCode之路
- Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
- Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
- Detailed explanation of STP election (step + case)
- Static routing + PAT + static NAT (explanation + experiment)
- 软件测试报告有哪些内容?
- 最大路径和
- VSCode Plugin: Nested Comments
- What are the project management tools like MS Project
猜你喜欢

Drools基本介绍,入门案例,基本语法

Force buckled brush the stairs (7/30)

The real CTO is a technical person who understands products

图像处理技术的心酸史

Problems that need to be solved by the tcp framework

《MySQL数据库进阶实战》读后感(SQL 小虚竹)

vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验

【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步

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

leetcode-1161:最大层内元素和
随机推荐
【AcWing 第62场周赛】
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
Can an inexperienced college graduate switch to software testing?my real case
Charging effect simulation
MySql installation and configuration super detailed tutorial and simple method of building database and table
两个有序数组间相加和的Topk问题
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
C language applet -- common classic practice questions
The final exam first year course
Basic introduction to ShardingJDBC
Real-time image acquisition based on FPGA
基于FPGA的图像实时采集
mysql view
Drools规则属性,高级语法
系统需求多变如何设计
keep-alive cache component
Arbitrum 专访 | L2 Summer, 脱颖而出的 Arbitrum 为开发者带来了什么?
221. Largest Square
16. Registration Center-consul