当前位置:网站首页>基于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)
边栏推荐
- Quelques suggestions pour la conception de l'interface
- [ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
- Browser render page pass
- Play the music of youth
- Related concepts of federal learning and motivation (1)
- JS closure
- What if win11u disk refuses access? An effective solution to win11u disk access denial
- 九齐NY8B062D MCU规格书/datasheet
- BFC interview Brief
- 二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
猜你喜欢
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Managed service network: application architecture evolution in the cloud native Era
Related concepts of federal learning and motivation (1)
Flet教程之 06 TextButton基础入门(教程含源码)
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
idea配置标准注释
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
字节测试工程师十年经验直击UI 自动化测试痛点
测试员的算法面试题-找众数
随机推荐
[in-depth learning] review pytoch's 19 loss functions
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
idea配置标准注释
How to solve the problem that win11 cannot write the value to the registry key?
Selected review | machine learning technology for Cataract Classification / classification
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
实操自动生成接口自动化测试用例
哈希(Hash)竞猜游戏系统开发功能分析及源码
Jiuqi ny8b062d MCU specification /datasheet
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
托管式服务网络:云原生时代的应用体系架构进化
From automation to digital twins, what can Tupo do?
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
Go notes (1) go language introduction and characteristics
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
MySQL statement execution details
acwing 3302. 表达式求值