opencv(9) :我一定一定坚持学完啊! opencv人脸检测

人脸检测

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('D:/code/opencv/face/CrystalLiu1.jpg')
#导入人脸检测器
face_e = cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_frontalface_default.xml')
#导入人眼检测器
eye_e=cv.CascadeClassifier(cv.data.haarcascades+'haarcascade_eye.xml')
# 返回人脸的坐标                               缩放比例         临近检测次数
face = face_e.detectMultiScale(img, scaleFactor=1.3, minNeighbors=5)

if len(face) > 0:
    for (x, y, w, h) in face:
        img=cv.rectangle(img, (x , y), (x + w, y + h), (255, 0, 0), 2)
        # 绘制人脸框
        face_a=img[y:y+h,x:x+w]
        eye_s=eye_e.detectMultiScale(face_a)
        for (ex,ey,ew,eh) in eye_s:
            cv.rectangle(face_a,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)


cv.imshow('img', img)
cv.waitKey(0)
cv.destroyAllWindows()

在这里插入图片描述
竟然把鼻孔当成了眼睛!!
检测视频中的人脸 人眼 笑容

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

cap = cv.VideoCapture(0)
# 导入人脸检测器
face_e = cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_frontalface_default.xml')
# 导入人眼检测器
eye_e = cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_eye.xml')
# 返回人脸的坐标                               缩放比例         临近检测次数
smile_e = cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_smile.xml')
while (True):
    ret, frame = cap.read()
    frame = cv.flip(frame, 1)  # 做镜像
    face = face_e.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=5)
    img = frame
    if len(face) > 0:
        for (x, y, w, h) in face:
            img = cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            # 绘制人脸框
            face_a = img[y:y + h, x:x + w]
            eye_s = eye_e.detectMultiScale(face_a)
            for (ex, ey, ew, eh) in eye_s:
                cv.rectangle(face_a, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 1)
            smile = smile_e.detectMultiScale(face_a, 1.16, 65, minSize=(25, 25), flags=cv.CASCADE_SCALE_IMAGE)
            for (ex, ey, ew, eh) in smile:
                cv.rectangle(face_a, (ex, ey), (ex + ew, ey + eh), (0, 0, 255), 1)
                cv.putText(img, 'Smile', (x, y - 7), 3, 1.2, (0, 0, 255), 2, cv.LINE_AA)

        cv.imshow('img', img)
        c = cv.waitKey(50)  # 50毫秒就
        if c == 27:  # 如果ESC了
            break

cv.release()
cv.destroyAllWindows()

代码看的b站子豪兄

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值