其他分享
首页 > 其他分享> > openCV Note

openCV Note

作者:互联网

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