我想在嘴唇上涂上胡子过滤器

2024-04-20 06:30:01 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here导入cv2 将numpy作为np导入

def blend_transparent(face_img, overlay_t_img):
    # Split out the transparency mask from the colour info
    overlay_img = overlay_t_img[:,:,:3] # Grab the BRG planes
    overlay_mask = overlay_t_img[:,:,3:]  # And the alpha plane

    # Again calculate the inverse mask
    background_mask = 255 - overlay_mask

    # Turn the masks into three channel, so we can use them as weights
    #overlay_mask = cv2.cvtColor(overlay_mask, cv2.COLOR_GRAY2BGR)
    #background_mask = cv2.cvtColor(background_mask, cv2.COLOR_GRAY2BGR)

    # Create a masked out face image, and masked out overlay
    # We convert the images to floating point in range 0.0 - 1.0
    face_part = (face_img * (1 / 255.0)) * (background_mask * (1 / 255.0))
    overlay_part = (overlay_img * (1 / 255.0)) * (overlay_mask * (1 / 255.0))

    #And finally just add them together, and rescale it back to an 8bit integer image
    return np.uint8(cv2.addWeighted(face_part, 255.0, overlay_part, 255.0, 0.0))

nose_cascade=cv2.CascadeClassifier('Nariz.xml')
cap=cv2.VideoCapture(0)
beard=cv2.imread('beard.png')
while True:
    ret,frame=cap.read()
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    nose=nose_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in nose:
        #cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        overlay_image = cv2.resize(beard, (h, w))
        frame[y:y + h, x:x + w, :] = blend_transparent(frame[y:y + h, x:x + w, :], overlay_image)
    cv2.imshow('frame',frame)
    k = cv2.waitKey(30) & 0xFF
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()

我得到以下信息

错误:

ValueError: operands could not be broadcast together with shapes (38,62,3) (62,38,0)

我试过做些改变,但不起作用。你知道吗


Tags: theimageimgmaskoutcv2framenose