工具:
C:\Python27>pip install opencv-python
Collecting opencv-python
Downloading https://files.pythonhosted.org/packages/69/61/2ea50ab91bc9b03c85f0cc5e4e421aea6db264991342c86fa667c7fc3ece/opencv_python-3.4.5.20-cp27-cp27m-win32.whl (25.9MB)
100% |████████████████████████████████| 25.9MB 260kB/s
Requirement already satisfied: numpy>=1.11.1 in c:\python27\lib\site-packages (from opencv-python) (1.15.4)
Installing collected packages: opencv-python
Successfully installed opencv-python-3.4.5.20
代码:
#encoding:utf-8
import cv2
import numpy as np
# filename='my.jpg'
imagepath=r"C:\Users\deaokylin\PycharmProjects\pyecharts\opencv\pictures\my.jpg"
path=r'D:\opencv2.4.9\sources\data\haarcascades'
def detect(filename):
face_cascade = cv2.CascadeClassifier(path+'haarcascade_frontalface_default.xml')
face_cascade.load(path+'\haarcascade_frontalface_default.xml')
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),1)
cv2.namedWindow("vikings detected")
cv2.imshow("vikings detected",img)
cv2.waitKey(0)
detect(imagepath)
效果:

错误处理:
python opecv TypeError: Incorrect type of self (must be 'CascadeClassifier'
解决办法:
安装opencv 2.4指向安装资源xml
D:\opencv2.4.9\sources\data\haarcascades
本文详细介绍了使用Python和OpenCV进行人脸检测的过程。通过安装OpenCV库并利用级联分类器,实现对图像中的人脸进行定位和绘制矩形框。文章提供了完整的代码示例,包括如何加载级联分类器、读取图像、转换为灰度图像以及应用人脸检测算法。
374

被折叠的 条评论
为什么被折叠?



