Skip to content

Commit 3f7fc30

Browse files
committed
091~092 haar cascade
1 parent 3963e36 commit 3f7fc30

12 files changed

+96
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,5 @@ code_087 | [Video Analysis-Frame Difference Moving Object Analysis](python/code_
129129
code_088 | [Video Analysis-Meanshift](python/code_088) | ✏️
130130
code_089 | [Video Analysis-CamShift](python/code_089) | ✏️
131131
code_090 | [Video Analysis-Object Movement Trajectory Drawing](python/code_090) | ✔️
132+
code_090 | [Object Detection-HAAR Cascade Classification ](python/code_091) | ✔️
133+
code_090 | [Object Detection-HAAR Feature Analysis](python/code_092) | ✔️

README_CN.md

+2
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ code_087 | [视频分析-帧差移动对象分析](python/code_087/opencv_087.py
128128
code_088 | [视频分析-均值迁移](python/code_088) | ✏️
129129
code_089 | [视频分析-连续自适应均值迁移](python/code_089) | ✏️
130130
code_090 | [视频分析-对象移动轨迹绘制](python/code_090) | ✔️
131+
code_090 | [对象检测-HAAR级联分类器](python/code_091) | ✔️
132+
code_090 | [对象检测-HAAR特征分析](python/code_092) | ✔️

python/code_091/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [人脸检测-Haar级联](https://zhuanlan.zhihu.com/p/72709866)

python/code_091/face.jpg

66.3 KB
Loading

python/code_091/opencv_091.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import cv2 as cv
2+
3+
capture = cv.VideoCapture(0)
4+
detector = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_alt.xml")
5+
6+
image = cv.imread('people.jpg')
7+
faces = detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=1,
8+
minSize=(50, 50), maxSize=(500, 500))
9+
for x, y, width, height in faces:
10+
cv.rectangle(image, (x, y), (x+width, y+height), (0, 0, 255), 2, cv.LINE_8, 0)
11+
cv.imshow("faces", image)
12+
cv.waitKey(0)
13+
cv.imwrite('face.jpg', image)
14+
15+
'''
16+
while True:
17+
ret, image = capture.read()
18+
19+
if ret is True:
20+
cv.imshow("frame", image)
21+
faces = detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=1,
22+
minSize=(30, 30), maxSize=(500, 500))
23+
for x, y, width, height in faces:
24+
cv.rectangle(image, (x, y), (x+width, y+height), (0, 0, 255), 2, cv.LINE_8, 0)
25+
cv.imshow("faces", image)
26+
c = cv.waitKey(50)
27+
if c == 27:
28+
break
29+
else:
30+
break
31+
32+
cv.destroyAllWindows()
33+
'''
34+
35+

python/code_091/people.jpg

32.3 KB
Loading

python/code_092/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [人脸检测-Haar级联](https://zhuanlan.zhihu.com/p/72709866)

python/code_092/face.jpg

74.4 KB
Loading

python/code_092/face_eyes.jpg

71 KB
Loading

python/code_092/face_smile_eye.jpg

80.2 KB
Loading

python/code_092/opencv_092.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import cv2 as cv
2+
3+
capture = cv.VideoCapture(1)
4+
face_detector = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_alt2.xml")
5+
eye_detector = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_eye.xml")
6+
smile_detector = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_smile.xml")
7+
8+
image = cv.imread('people.jpg')
9+
faces = face_detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=3,
10+
minSize=(50, 50), maxSize=(300, 300))
11+
for x, y, width, height in faces:
12+
cv.rectangle(image, (x, y), (x+width, y+height), (0, 0, 255), 2, cv.LINE_8, 0)
13+
roi = image[y:y+height,x:x+width]
14+
15+
eyes = eye_detector.detectMultiScale(roi, scaleFactor=1.1, minNeighbors=5,
16+
minSize=(20, 20), maxSize=(30,30))
17+
smiles = smile_detector.detectMultiScale(roi, scaleFactor=1.05, minNeighbors=2,
18+
minSize=(20, 20))
19+
20+
for ex, ey, ew, eh in eyes:
21+
cv.rectangle(roi, (ex, ey), (ex + ew, ey + eh), (255, 0, 0), 2)
22+
23+
for sx, sy, sw, sh in smiles:
24+
cv.rectangle(roi, (sx, sy), (sx + sw, sy + sh), (0, 255, 0), 2)
25+
cv.putText(image, 'Smile', (x+10, y-7), cv.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
26+
27+
cv.imshow("faces", image)
28+
cv.imwrite("face_smile_eye.jpg", image)
29+
cv.waitKey(0)
30+
'''
31+
while True:
32+
ret, image = capture.read()
33+
if ret is True:
34+
cv.imshow("frame", image)
35+
faces = face_detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=3,
36+
minSize=(30, 30), maxSize=(300, 300))
37+
for x, y, width, height in faces:
38+
cv.rectangle(image, (x, y), (x+width, y+height), (0, 0, 255), 2, cv.LINE_8, 0)
39+
roi = image[y:y+height,x:x+width]
40+
smiles = smile_detector.detectMultiScale(roi, scaleFactor=1.7, minNeighbors=3,
41+
minSize=(15, 15), maxSize=(100, 100))
42+
for sx, sy, sw, sh in smiles:
43+
cv.rectangle(roi, (sx, sy), (sx + sw, sy + sh), (0, 255, 0), 1)
44+
45+
cv.imshow("faces", image)
46+
c = cv.waitKey(50)
47+
if c == 27:
48+
break
49+
else:
50+
break
51+
'''
52+
cv.destroyAllWindows()
53+

python/code_092/people.jpg

32.3 KB
Loading

0 commit comments

Comments
 (0)