python-opencv 摄像头简单实现人脸检测案例

0.源码:
import cv2 as cv


# 视频人脸检测f
def video_face_detection():
    face_detector = cv.CascadeClassifier("D:/WorkTool/ins_Anaconda/Lib/site-packages/cv2/data"
                                         "/haarcascade_frontalface_alt2.xml")
    capture = cv.VideoCapture(0)
    while True:
        ret, value = capture.read()
        if ret is False:
            break
        value = cv.flip(value, 1)
        faces = face_detector.detectMultiScale(value, 1.06, 25)
        for (x, y, w, h) in faces:
            cv.rectangle(value, (x, y), (x + y, y + h), (0, 255, 0), 2)
        cv.imshow("video_face_detection", value)
        key = cv.waitKey(10)
        if key == ord('q'):  # 按q键退出
            print("over")
            break


if __name__ == "__main__":
    video_face_detection()
    cv.destroyAllWindows()

1.注意事项

cv.CascadeClassifier() : 里面可以填写你python安装的Lib/site-packages/cv2/data/haarcascade_frontalface_alt2.xml 如果你pip有opencv的话

关于 face_detector.detectMultiScale(value, 1.06, 25)
def detectMultiScale(self,
image: Any,
scaleFactor: Any = None,
minNeighbors: Any = None,
flags: Any = None,
minSize: Any = None,
maxSize: Any = None) -> None


1.image:要检测的图片;
2.scaleFactor:比例因子,表示在前后两次相继的扫描中,搜索窗口的比例系数。默认为1.1即每次搜索窗口依次扩大10%;
3.minNeighbors:表示构成检测目标的相邻矩形的最小个数(默认为3个);
4.flags:要么使用默认值,要么使用CV_HAAR_DO_CANNY_PRUNING,如果设置为CV_HAAR_DO_CANNY_PRUNING,那么函数将会使用Canny边缘检测来排除边缘过多或过少的区域,因此这些区域通常不会是人脸所在区域;
5、6:minSize和maxSize用来限制得到的目标区域的范围。

参考来源:https://blog.csdn.net/itismelzp/article/details/50379359

你可能感兴趣的:(opencv,机器视觉,人脸识别)