0

i have a code : that kind of fixes the red-eye

the output is somewhat correct but it applying redeye over other parts of face including the eye. i want it to be just in the eyes.

i have tried out few online reference non worked in my case any assistance will be appreciated

output image : output

import cv2
import numpy as np

def fillHoles(mask):
    maskFloodFill = mask.copy()
    h, w = maskFloodFill.shape[:2]
    maskTemp = np.zeros((h+2, w+2), np.uint8)
    cv2.floodFill(maskFloodFill, maskTemp, (0, 0), 255)
    mask2 = cv2.bitwise_not(maskFloodFill)
    return mask2 | mask


def removeRedEye(img, red_eye_threshold):
    height, width = img.shape[:2]
    img_fixed = img.copy()

    face_cascade_name = "./haarcascade_face.xml"
    eyes_cascade_name = "./haarcascade_eye.xml"

    eyes_cascade = cv2.CascadeClassifier()

    assert eyes_cascade.load(eyes_cascade_name), "Eyes detection model loading failed."

    eyes = eyes_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=3, flags=0,
                                         minSize=(height//50, width//50), maxSize=(height//2, width//2))

    for (x, y, w, h) in eyes:
        eye = img[y:y+h, x:x+w]

        b, g, r = cv2.split(eye)

        mask = (r > red_eye_threshold) & (r > (b + g))

        mask = mask.astype(np.uint8)*255
        fillHoles(mask)

        mask = cv2.dilate(mask, None, iterations=3)
        mean = np.mean(eye[:, :, :2], axis=2)
        mean = mean[:, :, np.newaxis]

        eye_fixed = np.dstack((mean, mean, mean))

        img_fixed[y:y+h, x:x+w] = np.where(mask[:, :, np.newaxis], eye_fixed, img_fixed[y:y+h, x:x+w])

    return img_fixed

Need some quick fixes for it. Guide where am going wrong !

Maximus
  • 1
  • 1

0 Answers0