ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

openCV Note

2021-08-25 12:59:16  阅读:210  来源: 互联网

标签:eye normal fX fY frame Note openCV mouth


Cascade classifier class for object detection.
在这里插入图片描述

def harr_cascade(cascade_dir, frame):
    # frame = imutils.resize(frame, width=231)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # initialize a dictionary that maps the name of the haar cascades to
    # their filenames
    detectorPaths = {
        "face": "haarcascade_frontalface_default.xml",
        "eyes": "haarcascade_eye.xml",
        "smile": "haarcascade_smile.xml",
    }

    # initialize a dictionary to store our haar cascade detectors
    print("[INFO] loading haar cascades...")
    detectors = {}
    # loop over our detector paths
    for (name, path) in detectorPaths.items():
        # load the haar cascade from disk and store it in the detectors
        # dictionary
        path = os.path.sep.join([cascade_dir, path])
        detectors[name] = cv2.CascadeClassifier(path)

    # perform face detection using the appropriate haar cascade
    faceRects = detectors["face"].detectMultiScale(
        gray, scaleFactor=1.05, minNeighbors=16, minSize=(100-20, 120-20),
        # maxSize=(100+20,120+20),
        flags=cv2.CASCADE_SCALE_IMAGE)

    confidence = []
    # loop over the face bounding boxes
    for (fX, fY, fW, fH) in faceRects:
        # extract the face ROI
        faceROI = gray[fY:fY + fH, fX:fX + fW]
        # draw the face bounding box on the frame
        cv2.rectangle(frame, (fX, fY), (fX + fW, fY + fH),
                      (0, 255, 0), 2)
        c = 0

        # apply eyes detection to the face ROI
        normal_eye_w = 22
        normal_eye_h = 10
        slack_eye  = 8
        eyeRects = detectors["eyes"].detectMultiScale(
            faceROI, scaleFactor=1.03, minNeighbors=6,
            minSize=(normal_eye_w - slack_eye, normal_eye_h - slack_eye),
            maxSize=(normal_eye_w + 2*slack_eye, normal_eye_h + 2*slack_eye),
            flags=cv2.CASCADE_SCALE_IMAGE)
        if len(eyeRects) > 0:
            c += 1
        if len(eyeRects) == 2:
            c += 1
        # loop over the eye bounding boxes and draw the eye bounding box on the frame
        for (eX, eY, eW, eH) in eyeRects:
            # draw the eye bounding box
            ptA = (fX + eX, fY + eY)
            ptB = (fX + eX + eW, fY + eY + eH)
            cv2.rectangle(frame, ptA, ptB, (0, 0, 255), 2)

        # apply smile(mouth) detection to the face ROI
        normal_mouth_w = 41
        normal_mouth_h = 11
        slack_mouth =8
        smileRects = detectors["smile"].detectMultiScale(
            faceROI, scaleFactor=1.03, minNeighbors=56,
            minSize=(normal_mouth_w - slack_mouth, normal_mouth_h - slack_mouth ),
            maxSize=(normal_mouth_w + 2*slack_mouth, normal_mouth_h + 2*slack_mouth),
            flags=cv2.CASCADE_SCALE_IMAGE)
        if len(smileRects) > 0:
            c += 1
        if len(smileRects) == 1:
            c += 1
            # loop over the smile bounding boxes
        for (sX, sY, sW, sH) in smileRects:
            # draw the smile bounding box
            ptA = (fX + sX, fY + sY)
            ptB = (fX + sX + sW, fY + sY + sH)
            cv2.rectangle(frame, ptA, ptB, (255, 0, 0), 2)

        confidence.append(c)

    idx = sorted(range(len(confidence)), key= lambda k:confidence[k], reverse=True)

    if(len(idx) > 0):
        box = faceRects[idx[0]]
        fX, fY, fW, fH = box
        cv2.rectangle(frame, (fX, fY), (fX + fW, fY + fH),
                  (255, 255, 255), 2)

        return box

标签:eye,normal,fX,fY,frame,Note,openCV,mouth
来源: https://blog.csdn.net/m0_37606837/article/details/119908782

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有