0

My goal is to paste an image on top of where cv2 detected my eyes. View cv2 eye detection photo output here

But instead of outputting the cv2 rectangles around my eyes, I'd like to replace it with an image. Is there a way to paste an image on top of where cv2 detected my eyes?

I have read through multiple StackOverflow questions that are similar, IE overlay a smaller image on a larger image python OpenCv but nothing has helped...

Background image

Background image

Foreground image

Foreground

I want to take this concept, image on image (Image output)

import os
import numpy as np
import cv2
from PIL import Image
from os.path import join, dirname, realpath

def upload_files():
    background = Image.open("image.jpg")
    foreground = Image.open("dot_transparent.png")
    background.paste(foreground, (0, 0), foreground)

    background.save("out2.png")

And place the image where these rectangles are (Image output)

import os
import numpy as np
import cv2
from PIL import Image
from os.path import join, dirname, realpath
import json

def upload_files():
    uploaded_file = request.files['file']
    filename = secure_filename(uploaded_file.filename)
    if filename != '':
        file_ext = os.path.splitext(filename)[1]
        if file_ext not in app.config['UPLOAD_EXTENSIONS'] or \
                file_ext != validate_image(uploaded_file.stream):
            abort(400)
        uploaded_file.save('new.png')
   
    #https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
    face_cascade = cv2.CascadeClassifier('/Users/matt/Python/LazerEyes/haarcascade_eye.xml')

    #https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
    eye_cascade = cv2.CascadeClassifier('/Users/matt/Python/LazerEyes/haarcascade_eye.xml')

    img = cv2.imread('new.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
         cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
         roi_gray = gray[y:y+h, x:x+w]
         roi_color = img[y:y+h, x:x+w]
         eyes = eye_cascade.detectMultiScale(roi_gray)
         for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    cv2.imwrite("out.png", img)

mattwelter
  • 29
  • 5
  • The detection code appears to be mostly irrelevant for a [example]. (by the way it's best if you learn Python/OpenCV properly instead of just copy paste code from tutorial sites and stack overflow) – user202729 Feb 22 '21 at 04:06

0 Answers0