I am using MTCNN to detect face in an image, FACENET to extract and save features from each detected face and OpenCV Gaussian Blur filter to mask the detected faces. My end goal is to find a target face in the masked image using saved features and unmask target face only. Any idea or advice ?
def face_and_features(img):
boxes, _ = mtcnn.detect(img)
print(boxes)
for i, box in enumerate(boxes):
# Crop the face from the image
a,b,c,d = box
x1,y1,x2,y2 = int(a), int(b), int(c), int(d)
face = img[y1:y2, x1:x2]
cv2.imwrite(f"face{i}.jpg",face)
face = cv2.resize(face, (160, 160))
face = face.transpose((2, 0, 1))
face = torch.from_numpy(face).float()
face = face.unsqueeze(0)
features = facenet(face)
filename = "face_{}.npy".format(i)
np.save(filename, features.detach().numpy())
with open("bounding_boxes.txt", "a") as f:
f.write("{}, {}, {}, {}, {}, {}, {} \n".format(x1, y1, x2, y2, filename, datetime.datetime.now(), frame_number))
return features
def masking(img):
filename = "bounding_boxes.txt"
masked_img = img.copy()
with open(filename,'r') as file:
for line in file:
x,y,w,h,f_name,time, f_no = line.split(",")
x,y,w,h = int(x), int(y), int(w), int(h)
roi_color = masked_img[y:h, x:w]
masked_img[y:h, x:w] = cv2.GaussianBlur(roi_color, (51,51), 0)
return masked_img, f_name, time, f_no