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:操作数无法与形状一起广播(38,62,3)(62,38,0)
我已尝试进行更改但不起作用。