opencv-图像人脸识别和视频人脸识别
首先可以取opencv官方github下载识别模型xml文件:https://github.com/lonngxiang/opencv/tree/master/data/haarcascades
1,图像人脸识别
import cv2filepath =r"C:\Users\Lavector\Desktop\1111.jpg"
img = cv2.imread(filepath) # 读取图片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换灰色# OpenCV人脸识别分类器
classifier = cv2.CascadeClassifier(r"C:\Users\Lavector\Desktop\cv_model\opencv\data\haarcascades\haarcascade_frontalface_default.xml"
)
color = (0, 255, 0) # 定义绘制颜色
# 调用识别人脸
faceRects = classifier.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
if len(faceRects): # 大于0则检测到人脸for faceRect in faceRects: # 单独框出每一张人脸x, y, w, h = faceRect# 框出人脸cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)# 左眼cv2.circle(img, (x + w // 4, y + h // 4 + 30), min(w // 8, h // 8),color)#右眼cv2.circle(img, (x + 3 * w // 4, y + h // 4 + 30), min(w // 8, h // 8),color)#嘴巴cv2.rectangle(img, (x + 3 * w // 8, y + 3 * h // 4),(x + 5 * w // 8, y + 7 * h // 8), color)cv2.imshow("image", img) # 显示图像
c = cv2.waitKey(10)cv2.waitKey(0)
cv2.destroyAllWindows()
2,视频人脸识别
import cv2# 图片识别方法封装
def discern(img):gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)cap = cv2.CascadeClassifier(r"C:\Users\Lavector\Desktop\cv_model\opencv\data\haarcascades\haarcascade_frontalface_default.xml")faceRects = cap.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(50, 50))if len(faceRects):for faceRect in faceRects:x, y, w, h = faceRectcv2.rectangle(img, (x, y), (x + h, y + w), (0, 255, 0), 2) # 框出人脸cv2.imshow("Image", img)# 获取摄像头0表示第一个摄像头
cap = cv2.VideoCapture(0)
while (1): # 逐帧显示ret, img = cap.read()# cv2.imshow("Image", img)discern(img)if cv2.waitKey(1) & 0xFF == ord('q'):break
cap.release() # 释放摄像头
cv2.destroyAllWindows() # 释放窗口资源
face_recognition
https://github.com/ageitgey/face_recognition
import face_recognition
import cv2# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
# 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
# 2. Only detect faces in every other frame of video.# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file(r"F.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]# Load a second sample picture and learn how to recognize it.
# biden_image = face_recognition.load_image_file("biden.jpg")
# biden_face_encoding = face_recognition.face_encodings(biden_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [obama_face_encoding,# biden_face_encoding
]
known_face_names = ["aa",# "Joe Biden"
]# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:# Grab a single frame of videoret, frame = video_capture.read()# Resize frame of video to 1/4 size for faster face recognition processingsmall_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)rgb_small_frame = small_frame[:, :, ::-1]# Only process every other frame of video to save timeif process_this_frame:# Find all the faces and face encodings in the current frame of videoface_locations = face_recognition.face_locations(rgb_small_frame)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:# See if the face is a match for the known face(s)matches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# If a match was found in known_face_encodings, just use the first one.if True in matches:first_match_index = matches.index(True)name = known_face_names[first_match_index]face_names.append(name)process_this_frame = not process_this_frame# Display the resultsfor (top, right, bottom, left), name in zip(face_locations, face_names):# Scale back up face locations since the frame we detected in was scaled to 1/4 sizetop *= 4right *= 4bottom *= 4left *= 4# Draw a box around the facecv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# Draw a label with a name below the facecv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Display the resulting imagecv2.imshow('Video', frame)# Hit 'q' on the keyboard to quit!if cv2.waitKey(1) & 0xFF == ord('q'):break# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
opencv-图像人脸识别和视频人脸识别相关推荐
- OpenCV图像运算+Moviepy实现视频旋转叠加
☞ ░ 前往老猿Python博客 https://blog.csdn.net/LaoYuanPython ░ 一.引言 在<OpenCV-Python常用图像运算:加减乘除幂开方对数及位运算&g ...
- opencv 人脸识别_人工智能-OpenCV+Python实现人脸识别(视频人脸检测)
上期文章我们分享了opencv识别图片中的人脸,OpenCV图片人脸检测,本期我们分享一下如何从视频中检测到人脸 视频人脸检测 OpenCV打开摄像头特别简单,只需要如下一句代码 capture = ...
- 人工智能-OpenCV+Python实现人脸识别(视频人脸检测)
上期文章我们分享了opencv识别图片中的人脸,OpenCV图片人脸检测,本期我们分享一下如何从视频中检测到人脸 视频人脸检测 OpenCV打开摄像头特别简单,只需要如下一句代码 capture = ...
- Java + opencv 实现人脸识别,图片人脸识别、视频人脸识别、摄像头实时人脸识别
搭建环境 opencv官网下载windows安装包 https://opencv.org/releases/ 选择最新版4.1.1 下载完成后是一个opencv-4.1.1-vc14_vc15.exe ...
- 智能识别系统----视频人脸检测(一)
文章目录 项目目录 提取人脸 特征提取 PCA LDA LBPH+直方图特征 训练分类器 SVC 可视化 利用分类器进行视频人像分类 有空的时候把项目部署到github上 项目目录 提取人脸 首先编写 ...
- 人脸识别-在视频中识别人脸
感谢优秀的你打开了小白的文章 "希望在看文章的你今天又进步了一点点,离美好生活更近一步!" 目录 往期回顾 代码 结果展示 VideoCapture类的构造函数: 往期回顾 灰度转 ...
- 视频人脸识别和图片人脸识别的关系
首先解释下视频人脸识别和图片人脸识别的区别,视频人脸识别是基于视频流进行人脸识别,用户的感觉就是直接在视频中就可以识别出人脸,而图片人脸识别,是用户直接上传图片,输出识别结果. 图片人脸识别可以描述为 ...
- JavaCV进阶opencv图像检测识别:ffmpeg视频图像画面人脸检测
JavaCV免费教程目录: JavaCV入门教程(免费JavaCV教程) javacv实战专栏目录(2016年更新至今): JavaCV实战专栏文章目录(2016年更新至今) 2022年最新JavaC ...
- 基于openCV的视频人脸识别——演员的诞生视频人脸识别
1.准备训练数据 网络上下载(训练数据量大时,通过爬虫获取)目标的图片: 运用以下代码将原图中的人脸头像识别.提取.调整大小(这里是150*200),并分别保存. 运行环境:win7 64+VS201 ...
最新文章
- 论设计,需求和编码三者的关系
- 【Java】浅谈关键词transient的使用
- 2018-2019-1 20189221 《构建之法》第 3 周学习总结
- android 应用程序Activity之间数据传递与共享的几种途径
- Python之random.seed()用法
- mp3文件怎么压缩大小
- 计算机技术在生物学中的应用题库,2018年第二军医大学基础医学部816计算机在生物医学中的应用之生物化学考研基础五套测试题...
- ubuntu 16.04安装vscode(visual-studio-code)操作步骤
- 自适应弹出框-垂直居中
- macOS如何编辑hosts
- HeadFirstC笔记_7 高级函数:发挥函数的极限
- 【论文精读】A Survey on Deep Learning for Named Entity Recognition
- Cardboard 凝视
- UcosII移植、调度、功能、运行流程解析
- redis.set方法详解
- CentOS7下使用YUM安装MySQL5.6
- Kafka 的 replica 同步机制(ISR与OSR列表数据相互转换)
- Stable diffusion扩散模型相关
- 封闭式基金最新折价率排行(20060922)[ZT]
- 视频教程-JavaScript从入门到进阶视频课程-JavaScript
热门文章
- 【Linux】centOS 错误记录|无法启动网络Failed to start LSB: Bring up/down networking
- 计算机设备维修保养记录表填写,弱电设备设施保养记录文本表格.doc
- 不知道干些什么,那就看看书吧!
- 迈出高效管理步伐 8thManage助IT分销行以快制胜
- Scipy 之eye方法介绍
- Ubuntu 8.04 AMD64平台下Realplayer 11安装指南
- 海蜘蛛软路由-转载收藏
- 大学生bootstrap框架网页作业成品 web前端大作业期末源码 航海王html+jquery+bootstrap响应式网页制作模板 学生海贼王动漫bootstrap框架网站作品
- 解密中国互联网:17个鲜活案例印证的5大生死逻辑!
- libreoffice安装教程_win10怎么安装libreoffice6.4_win10系统libreoffice6.4安装教程