【发布时间】:2021-06-03 21:42:33
【问题描述】:
我想在cv2 检测到我眼睛的位置放置一个透明图像。我已经完成了主要的两个步骤,现在我需要将它们结合起来。
例如,这里是带有image transparency working 的输出,这里是带有eye detection working 的输出。脚本和图片在下面,我不知道该怎么办。
图片
app.py
>import os
import numpy
import cv2
from PIL import Image
from os.path import join, dirname, realpath
def upload_files():
#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')
dot = cv2.imread('dot_transparent.png', cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray_to_place = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_h, img_w = gray.shape
img_to_place_h, img_to_place_w = gray_to_place.shape
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
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:
dot = cv2.resize(dot, (eh, ew))
# Prepare pixel-wise alpha blending
dot_alpha = dot[..., :3] / 255.0
dot_alpha = numpy.repeat(dot_alpha[..., numpy.newaxis], 3, axis=2)
dot = dot[..., :3]
resized_img = cv2.resize(dot, (eh, ew), interpolation = cv2.INTER_AREA)
resized_img_h, resized_img_w, _ = resized_img.shape
#pointsOnFace = []
#integersToAppend = eh
#pointsOnFace.append(integersToAppend)
#print(pointsOnFace)
roi_color[ey:ey+resized_img_h, ex:ex+resized_img_w, :] = resized_img
cv2.imwrite('out.png', img)
【问题讨论】:
-
相关:stackoverflow.com/q/66339930/11089932 对于每个
(ex, ey, ew, eh),即在该循环内,您需要获取dot的副本,并调整其大小。ew, eh,然后执行我之前回答中的 alpha 混合步骤。您只是在当前代码中省略了该部分!?
标签: python opencv python-imaging-library opencv3.0