Python+OpenCV实现简单的人脸检测

# 导入openCV库
import cv2

# 加载特征分类器 openCV自带
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# 打开电脑摄像头
capture = cv2.VideoCapture(0)

# 获得摄像头捕捉到的每一帧图片
while True:
    ret, frame = capture.read()
    # 将图片转换为灰度, 提高计算速度
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # 检测图片中的人脸
    faces = face_cascade.detectMultiScale(
        gray,            # 要检测的图片
        scaleFactor=1.15,# 图像缩小的比例
        minNeighbors=7,  # 一个目标至少被检测多少次才会被标记
        minSize=(5, 5)   # 检测目标的最小尺寸
    )
    # 为检测到的每个人脸绘制矩形框
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break
发布了18 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Juicewyh/article/details/98611795
今日推荐