I am trying to add mask for every detected face:
import os
import sys
import cv2
import datetime
import numpy as np
os.system("raspistill -w 1920 -h 1280 -o faces_to_detect.jpg")
imagePath = "faces_to_detect.jpg"
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.21,
minNeighbors=3,
minSize=(18, 18),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
print("Faces on pic: {}.").format(len(faces))
faces_img = cv2.imread("/home/pi/Room/faces_to_detect.jpg")
mask = cv2.imread("/home/pi/Room/mask.png")
rows,cols,ch = faces_img.shape
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 235, 0), 2)
rows,cols,ch = faces.shape
pts1 = np.float32([[x, y], [x, h+y], [w+x, y]])
pts2 = np.float32([[0, 0], [x, 0], [0, x]])
M = cv2.getAffineTransform(pts2,pts1)
dst = cv2.warpAffine(mask,M,(cols,rows))
result_img = cv2.addWeighted(dst, 0.8, faces, 1, 0)
#cv2.imshow("Faces found", image)
#cv2.waitKey(0)
print(x,y,w,h)
cv2.imwrite("/home/pi/Room/replaced.png", result_img)
Problem which I am trying to solve: the mask appears for only one face (I want to add a mask to all faces I detected). Any helpful info will be great. Thank you!