Quincy Adams
Quincy Adams

Reputation: 1

How can I implement a laser eyes overlay using OpenCV and Python?

As a personal project venturing into computer vision, I am currently trying to code a python script that allows me to take an input image, detect the eyes and overlay a "deep fried" laser eyes overlay onto them. An example of this can be found here:.

I was inspired to this project by this website which does what I would like my python script but I am running into roadblocks in implementation, specifically with overlaying the laser eyes accurately.

So far I have this code, which can accurately detect the eyes, and does place a laser eye image at that point, but the image gets compressed down to the size of the eye and turns into a blank red dot and lacks the same lens flare like effect. Any help would be greatly appreciated!

import cv2
import numpy as np
from PIL import Image

# Load the input image
img = cv2.imread('input_image.jpg')

# Load the laser eye overlay image
laser_eye = cv2.imread('laser_eye.png', cv2.IMREAD_UNCHANGED)

# Convert the input image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Load the Haar cascade classifier for eye detection
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

# Detect eyes in the grayscale image
eyes = eye_cascade.detectMultiScale(gray, 1.3, 5)

# Iterate through the detected eyes and apply the laser eye overlay
for (x,y,w,h) in eyes:
    # Resize the laser eye overlay image to match the size of the eye region
    resized_laser_eye = cv2.resize(laser_eye, (w,h), interpolation=cv2.INTER_AREA)
    # Extract the alpha channel of the laser eye overlay
    alpha = resized_laser_eye[:,:,3]/255.0
    alpha = alpha[0][0]
    # Extract the RGB channels of the laser eye overlay
    laser_eye_rgb = resized_laser_eye[:,:,0:3]
    # Extract the eye region from the input image
    eye_region = img[y:y+h, x:x+w]
    # Blend the laser eye overlay with the eye region using alpha blending
    blended_eye = cv2.addWeighted(laser_eye_rgb, 1-alpha, eye_region, alpha, 0)
    # Replace the eye region in the input image with the blended image
    img[y:y+h, x:x+w] = blended_eye

# Display the output image
cv2.imshow('Output', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

EDIT: Input images are here, laser eyes, input face

Upvotes: 0

Views: 122

Answers (0)

Related Questions