当前位置:网站首页>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~!
边栏推荐
- Static routing + PAT + static NAT (explanation + experiment)
- 验证整数输入
- [1154]如何将字符串转换为datetime
- GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
- coldfusion文件读取漏洞(CVE-2010-2861)
- The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]
- How to do a startup CTO?
- mmdetection训练一个模型相关命令
- What are the project management tools like MS Project
- keep-alive cache component
猜你喜欢
![LeetCode 1161 最大层内元素和[BFS 二叉树] HERODING的LeetCode之路](/img/56/fcc8ee6f592abf0a374fc950a3362f.png)
LeetCode 1161 最大层内元素和[BFS 二叉树] HERODING的LeetCode之路

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

934. 最短的桥

The real CTO is a technical person who understands products

Layer 2 broadcast storm (cause + judgment + solution)

Static route analysis (the longest mask matching principle + active and standby routes)

To write good test cases, you must first learn test design

After reading "MySQL Database Advanced Practice" (SQL Xiao Xuzhu)

Between two orderly array of additive and Topk problem
![[Map and Set] LeetCode & Niu Ke exercise](/img/66/d812a6ad854cb0993c796760042150.png)
[Map and Set] LeetCode & Niu Ke exercise
随机推荐
cudaMemcpy study notes
MySQL的存储过程
What have I experienced when I won the offer of BAT and TMD technical experts?
leetcode-952: Calculate max component size by common factor
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
CV-Model【3】:MobileNet v2
简易表白小页面
软件测试报告有哪些内容?
What is the ideal college life?
[WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
f.grid_sample
Gateway routing configuration
uniapp uses 3rd party fonts
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
How to do a startup CTO?
mysql 索引
keep-alive cache component
加密生活,Web3 项目合伙人的一天
[1154] How to convert string to datetime
Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment