当前位置:网站首页>Object detection based on OpenCV haarcascades
Object detection based on OpenCV haarcascades
2022-07-04 23:03:00 【Just_ Paranoid】
OpenCV:https://github.com/opencv/opencv
haarcascades - This folder contains a specific type of classifier trained to detect objects , for example Face ( positive 、 The side )、 Pedestrians, etc . Some classifiers have a special license. Please check the file for details .
OpenCV Already contains many for the face , eyes , Pre classifier of smiling face, etc . these XML Files are stored in opencv/data/haarcascades/ In the folder
OpenCV The trainer and detector are attached . If you want a car , Aircraft and any other objects train your own classifier , You can use OpenCV Create a . Its full details are given here : Cascade classifier training .
https://docs.opencv.org/2.4/doc/user_guide/ug_traincascade.html
# openCV Common usage
# Import opencv library
import os
import cv2
# Recognize faces in pictures : Use haarcascade_frontalface_default.xml
def face_detect(img_path):
# Initialize model
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_frontalface_default.xml')
# Initialize window
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# Read the picture
img = cv2.imread(img_path)
# display picture
# cv2.imshow('image', img)
# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Face detection
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# Get face frame
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
# Show face box
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Save the picture
cv2.imwrite('../result/face_detect_1.jpg', img)
# Display picture tag face box
# cv2.imshow('image', img)
# Wait for the window to close
# cv2.waitKey(0)
# Eye detection : Use haarcascade_eye_tree_eyeglasses.xml
def eye_detect(img_path):
# Initialize model
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_eye_tree_eyeglasses.xml')
# Initialize window
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# Read the picture
img = cv2.imread(img_path)
# display picture
# cv2.imshow('image', img)
# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Eye detection
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# Get eye frame
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
# Show face box
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Save the picture
cv2.imwrite('../result/eye_detect_1.jpg', img)
# Display picture tag face box
cv2.imshow('image', img)
# Wait for the window to close
cv2.waitKey(0)
def eye_detect_save(img_path, filename):
# Initialize model
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_eye_tree_eyeglasses.xml')
# Read the picture
img = cv2.imread(img_path)
# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Eye detection
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# Get eye frame
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
# Show face box
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Save the picture
save_path = '../result/' + filename
cv2.imwrite(save_path, img)
# Eye detection : Use haarcascade_eye.xml
def eye_detect_2(img_path):
# Initialize model
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_eye.xml')
# Initialize window
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# Read the picture
img = cv2.imread(img_path)
# display picture
# cv2.imshow('image', img)
# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Eye detection
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# Get eye frame
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
# Show face box
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Save the picture
cv2.imwrite('../result/eye_detect2_1.jpg', img)
# Display picture tag face box
cv2.imshow('image', img)
# Wait for the window to close
cv2.waitKey(0)
# Portrait detection :haarcascade_profileface.xml
def profile_face(img_path):
# Initialize model
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_profileface.xml')
# Initialize window
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# Read the picture
img = cv2.imread(img_path)
# display picture
# cv2.imshow('image', img)
# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Eye detection
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# Get eye frame
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
# Show face box
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Save the picture
cv2.imwrite('../result/profile_face_6.jpg', img)
# Display picture tag face box
cv2.imshow('image', img)
# Wait for the window to close
cv2.waitKey(0)
# Smile detection :haarcascade_smile.xml
def smile_detect(img_path):
# Initialize model
detector = cv2.CascadeClassifier('../haarcascades/haarcascade_smile.xml')
# Initialize window
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
# Read the picture
img = cv2.imread(img_path)
# display picture
# cv2.imshow('image', img)
# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Eye detection
dets = detector.detectMultiScale(img_gray, 1.1, 5)
# Get eye frame
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
# Show face box
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Save the picture
cv2.imwrite('../result/smile_detect_1.jpg', img)
# Display picture tag face box
cv2.imshow('image', img)
# Wait for the window to close
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')
# Traverse face Folder Output Eye detection Output Mark to result Folder
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)
边栏推荐
- Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
- Attack and Defense World MISC Advanced Area Erik baleog and Olaf
- Notepad++ -- editing skills
- 9 - class
- 金融市场,资产管理与投资基金
- Redis démarrer le tutoriel complet: Pipeline
- OSEK标准ISO_17356汇总介绍
- [Lua] Int64 support
- 攻防世界 misc 高手进阶区 a_good_idea
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
猜你喜欢
Redis入门完整教程:慢查询分析
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
[roommate learned to use Bi report data processing in the time of King glory in one game]
浅聊一下中间件
Talk about Middleware
Analysis of the self increasing and self decreasing of C language function parameters
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
攻防世界 MISC 高手进阶区 001 normal_png
EditPlus--用法--快捷键/配置/背景色/字体大小
Redis introduction complete tutorial: slow query analysis
随机推荐
[OpenGL] note 29 anti aliasing (MSAA)
How can enterprises cross the digital divide? In cloud native 2.0
Serial port data frame
企业如何跨越数字化鸿沟?尽在云原生2.0
Async await used in map
Common methods in string class
Redis入门完整教程:初识Redis
A complete tutorial for getting started with redis: getting to know redis for the first time
Hit the core in the advanced area of misc in the attack and defense world
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Breakpoint debugging under vs2019 c release
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
SHP data making 3dfiles white film
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
位运算符讲解
Redis入门完整教程:Redis使用场景
The difference between Max and greatest in SQL
【剑指Offer】6-10题
Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
Attack and defense world misc advanced grace-50