本文共 1358 字,大约阅读时间需要 4 分钟。
此次试验在Anaconda-Jupyter环境下完成
安装opencv-python
pip install opencv-python (注意:需要包:'Python路径下/Lib/site-packages/cv2/data/haarcascade_frontalface _default.xml’以及图片test1.jpg) 创建Python文件import cv2def detect(filename): face_cascade = cv2.CascadeClassifier('D:/anaconda/Lib/site-packages/cv2/data/haarcascade_eye.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), 2) cv2.imshow('Person Detected!', img) cv2.waitKey(0) cv2.destroyAllWindows()if __name__ == '__main__': detect('test1.jpg')
结果如下
选择眼部识别haarcascade_eye.xml再次测试需要dlib包,同样能够输出图像。
创建Python文件import cv2import dlibpath = "test1.jpg"img = cv2.imread(path)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#人脸分类器detector = dlib.get_frontal_face_detector()# 获取人脸检测器predictor = dlib.shape_predictor('D:/anaconda/Lib/site-packages/shape_predictor_68_face_landmarks.dat/shape_predictor_68_face_landmarks.dat')dets = detector(gray, 1)for face in dets: shape = predictor(img, face) # 寻找人脸的68个标定点 # 遍历所有点,打印出其坐标,并圈出来 for pt in shape.parts(): pt_pos = (pt.x, pt.y) cv2.circle(img, pt_pos, 2, (0, 255, 0), 1) cv2.imshow("image", img)cv2.waitKey(0)cv2.destroyAllWindows()
结果如下
转载地址:http://xorwi.baihongyu.com/