当前位置:网站首页>基于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)
边栏推荐
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
- Why is the maximum speed the speed of light
- Go language notes (4) go common management commands
- Flet教程之 07 PopupMenuButton基础入门(教程含源码)
- Automatic generation of interface automatic test cases by actual operation
- Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
- Qt五子棋人机对战画棋子之QPainter的使用误区总结
- RFID仓储管理系统解决方案的优点
- RFID仓库管理系统解决方案有哪些功能模块
猜你喜欢
LeetCode+ 81 - 85 单调栈专题
See how Tencent does interface automation testing
Idea configuration standard notes
How to adapt your games to different sizes of mobile screen
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Qt五子棋人机对战画棋子之QPainter的使用误区总结
Form组件常用校验规则-1(持续更新中~)
伦敦银走势图分析的新方法
《动手学深度学习》(三) -- 卷积神经网络 CNN
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
随机推荐
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
记录线上bug解决list(未完待续7/4)
Advantages of semantic tags and block level inline elements
托管式服务网络:云原生时代的应用体系架构进化
JS closure
电脑怎么保存网页到桌面上使用
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
Hash quiz game system development how to develop hash quiz game system development (multiple cases)
GVM使用
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Understand the reading, writing and creation of files in go language
Automatic generation of interface automatic test cases by actual operation
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
How does win11 search for wireless displays? Win11 method of finding wireless display device
Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
js 闭包
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
卷积神经网络在深度学习中新发展的5篇论文推荐
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion