1

Friends, I need to implement a code, which blur faces from given images (I am not a dev so this is really difficult to me). I found out that I can use OpenCV and cvlib to do that and found a sample code (repository from cvlib) which does part of the job.

I understood that I need to get the subfaces and apply the face blur to them and I could do it but now I don't know how to add the blurred face to original image. Could someone help me with that?

import cvlib as cv
import sys
from cv2 import cv2
import os 

# read input image
image = cv2.imread('path')

# apply face detection
faces, confidences = cv.detect_face(image)

print(faces)
print(confidences)

# loop through detected faces
for face,conf in zip(faces,confidences):

    (startX,startY) = face[0],face[1]
    (endX,endY) = face[2],face[3]

    subFace = image[startY:endY,startX:endX]
    subFace = cv2.GaussianBlur(subFace,(23, 23), 30)
# display output
# press any key to close window           
cv2.imshow("face_detection", image)
cv2.waitKey()

cv2.imshow("face_detection", subFace)


# release resources
cv2.destroyAllWindows()
marcelo
  • 373
  • 6
  • 16

1 Answers1

1

I finally figured out how to do it:

import cvlib as cv
import sys
from cv2 import cv2
import os 

# read input image
image = cv2.imread('path')

# apply face detection
faces, confidences = cv.detect_face(image)

# print the array with the coordinates and the confidence
print(faces)
print(confidences)

# loop through detected faces
for face,conf in zip(faces,confidences):

    (startX,startY) = face[0],face[1]
    (endX,endY) = face[2],face[3]
    
    # get the subface
    subFace = image[startY:endY,startX:endX]
    # apply gaussian blur over subfaces
    subFace = cv2.GaussianBlur(subFace,(23, 23), 30)
    # add the subfaces to de original image
    image[startY:startY+subFace.shape[0], startX:startX+subFace.shape[1]] = subFace
         
cv2.imshow("face_detection", image)
cv2.waitKey()

# save output
cv2.imwrite("face_detection.jpg", image)

# release resources
cv2.destroyAllWindows()
marcelo
  • 373
  • 6
  • 16