当前位置:网站首页>基于OpenCV haarcascades的对象检测
基于OpenCV haarcascades的对象检测
2022-07-04 19:44:00 【Just_Paranoid】
OpenCV:https://github.com/opencv/opencv
haarcascades - 该文件夹包含经过训练的用于检测对象的特定类型的分类器,例如 人脸(正面、侧面)、行人等。一些分类器有一个特殊的许可证请查看文件以获取详细信息。
OpenCV已经包含许多用于面部,眼睛,笑脸等的预先分类器。这些XML文件存储在opencv/data/haarcascades/文件夹中
OpenCV随附教练机和探测器。如果你想为汽车,飞机等任何物体训练你自己的分类器,你可以使用OpenCV创建一个。它的全部细节在这里给出:级联分类器培训。
https://docs.opencv.org/2.4/doc/user_guide/ug_traincascade.html
# openCV 常用用法
# 导入opencv库
import os
import cv2
# 识别图片中的人脸:使用 haarcascade_frontalface_default.xml
def face_detect(img_path):
# 初始化模型
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_frontalface_default.xml')
# 初始化窗口
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# 读取图片
img = cv2.imread(img_path)
# 显示图片
# cv2.imshow('image', img)
# 转换为灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 人脸检测
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# 获取人脸框
faces = []
for i, d in enumerate(dets):
x1 = d[0] if d[0] > 0 else 0
y1 = d[1] if d[1] > 0 else 0
x2 = d[0] + d[2] if d[0] + d[2] > 0 else 0
y2 = d[1] + d[3] if d[1] + d[3] > 0 else 0
# 显示人脸框
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 保存图片
cv2.imwrite('../result/face_detect_1.jpg', img)
# 显示图片标记人脸框
# cv2.imshow('image', img)
# 等待关闭窗口
# cv2.waitKey(0)
# 人眼检测:使用 haarcascade_eye_tree_eyeglasses.xml
def eye_detect(img_path):
# 初始化模型
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_eye_tree_eyeglasses.xml')
# 初始化窗口
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# 读取图片
img = cv2.imread(img_path)
# 显示图片
# cv2.imshow('image', img)
# 转换为灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 人眼检测
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# 获取人眼框
faces = []
for i, d in enumerate(dets):
x1 = d[0] if d[0] > 0 else 0
y1 = d[1] if d[1] > 0 else 0
x2 = d[0] + d[2] if d[0] + d[2] > 0 else 0
y2 = d[1] + d[3] if d[1] + d[3] > 0 else 0
# 显示人脸框
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 保存图片
cv2.imwrite('../result/eye_detect_1.jpg', img)
# 显示图片标记人脸框
cv2.imshow('image', img)
# 等待关闭窗口
cv2.waitKey(0)
def eye_detect_save(img_path, filename):
# 初始化模型
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_eye_tree_eyeglasses.xml')
# 读取图片
img = cv2.imread(img_path)
# 转换为灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 人眼检测
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# 获取人眼框
faces = []
for i, d in enumerate(dets):
x1 = d[0] if d[0] > 0 else 0
y1 = d[1] if d[1] > 0 else 0
x2 = d[0] + d[2] if d[0] + d[2] > 0 else 0
y2 = d[1] + d[3] if d[1] + d[3] > 0 else 0
# 显示人脸框
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 保存图片
save_path = '../result/' + filename
cv2.imwrite(save_path, img)
# 人眼检测:使用 haarcascade_eye.xml
def eye_detect_2(img_path):
# 初始化模型
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_eye.xml')
# 初始化窗口
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# 读取图片
img = cv2.imread(img_path)
# 显示图片
# cv2.imshow('image', img)
# 转换为灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 人眼检测
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# 获取人眼框
faces = []
for i, d in enumerate(dets):
x1 = d[0] if d[0] > 0 else 0
y1 = d[1] if d[1] > 0 else 0
x2 = d[0] + d[2] if d[0] + d[2] > 0 else 0
y2 = d[1] + d[3] if d[1] + d[3] > 0 else 0
# 显示人脸框
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 保存图片
cv2.imwrite('../result/eye_detect2_1.jpg', img)
# 显示图片标记人脸框
cv2.imshow('image', img)
# 等待关闭窗口
cv2.waitKey(0)
# 肖像检测:haarcascade_profileface.xml
def profile_face(img_path):
# 初始化模型
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_profileface.xml')
# 初始化窗口
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# 读取图片
img = cv2.imread(img_path)
# 显示图片
# cv2.imshow('image', img)
# 转换为灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 人眼检测
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# 获取人眼框
faces = []
for i, d in enumerate(dets):
x1 = d[0] if d[0] > 0 else 0
y1 = d[1] if d[1] > 0 else 0
x2 = d[0] + d[2] if d[0] + d[2] > 0 else 0
y2 = d[1] + d[3] if d[1] + d[3] > 0 else 0
# 显示人脸框
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 保存图片
cv2.imwrite('../result/profile_face_6.jpg', img)
# 显示图片标记人脸框
cv2.imshow('image', img)
# 等待关闭窗口
cv2.waitKey(0)
# 微笑检测:haarcascade_smile.xml
def smile_detect(img_path):
# 初始化模型
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_smile.xml')
# 初始化窗口
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# 读取图片
img = cv2.imread(img_path)
# 显示图片
# cv2.imshow('image', img)
# 转换为灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 人眼检测
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# 获取人眼框
faces = []
for i, d in enumerate(dets):
x1 = d[0] if d[0] > 0 else 0
y1 = d[1] if d[1] > 0 else 0
x2 = d[0] + d[2] if d[0] + d[2] > 0 else 0
y2 = d[1] + d[3] if d[1] + d[3] > 0 else 0
# 显示人脸框
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 保存图片
cv2.imwrite('../result/smile_detect_1.jpg', img)
# 显示图片标记人脸框
cv2.imshow('image', img)
# 等待关闭窗口
cv2.waitKey(0)
if __name__ == '__main__':
print('PyCharm')
# face_detect('../face/face01.jpg')
# eye_detect('../face/face01.jpg')
# eye_detect_2('../face/face01.jpg')
# profile_face('../face/face06.jpg')
# smile_detect('../face/face08.jpg')
# 遍历face文件夹 输出 人眼检测 输出 标记到 result文件夹
for dirpath, dirnames, filenames in os.walk('../face'):
for filename in filenames:
print(os.path.join(dirpath, filename))
eye_detect_save(os.path.join(dirpath, filename), filename)
边栏推荐
- go语言笔记(2)go一些简单运用
- E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
- Win11无法将值写入注册表项如何解决?
- Practice examples to understand JS strong cache negotiation cache
- 黄金k线图中的三角形有几种?
- Go notes (3) usage of go language FMT package
- Browser render page pass
- word中插入图片后,图片上方有一空行,且删除后布局变乱
- Practical examples of node strong cache and negotiation cache
- So this is the BGP agreement
猜你喜欢
How to solve the problem that win11 cannot write the value to the registry key?
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
What if win11u disk refuses access? An effective solution to win11u disk access denial
九齐NY8B062D MCU规格书/datasheet
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
Leetcode+ 81 - 85 monotone stack topic
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
[in-depth learning] review pytoch's 19 loss functions
随机推荐
What is the development of block hash quiz game system? Hash quiz game system development (case mature)
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
最长的可整合子数组的长度
语义化标签的优势和块级行内元素
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
MySQL - database query - use of aggregate function, aggregate query, grouping query
字节测试工程师十年经验直击UI 自动化测试痛点
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
分析伦敦银走势图的技巧
Form组件常用校验规则-1(持续更新中~)
九齐单片机NY8B062D单按键控制4种LED状态
Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Automatic generation of interface automatic test cases by actual operation
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
LeetCode 871. 最低加油次数
工厂从自动化到数字孪生,图扑能干什么?
What is involution?
GVM use