About the face blurring process

def blur_rois(image, coords):


Blur Regions Of Interest



for coord in coords:

    roi = image[coord[1]:coord[3], coord[0]:coord[2]]

    if (roi.shape[0] * roi.shape[1]) > 0:

        roi_blurred = cv2.GaussianBlur(roi, (101, 101), 0)

        image[coord[1]:coord[3], coord[0]:coord[2]] = roi_blurred

return image

Hello, my name is Yuta.

I would like to speed up the process of blurring faces.
I would like to know how to speed up the pasting program.

Currently, it is taking about 0.3 seconds.
I would like to process about 100,000 images, so if there is a way to speed up the process, please let me know.

I’m wondering if I’m using “for” statements and that’s the problem.

Hi Yuta!

Your code seems OK.

First, the large aperture of the gaussian kernel can make the processing slow. Try cv2.blur() or cv2.boxFilter() and see if it’s faster.

You can try to blur all the image and copy it over the original using a mask; something like (untested code):

img_blurred = cv2.blur(image,(101,101))
mask = np.zeros(...same shape as image...) # create a black mask
for coord in coords:
    mask[coord[1]:coord[3], coord[0]:coord[2]]=1 # set the ROIs to 1
img_blurred.copyTo(image,mask) # copy the blurred image on top the original using the mask

(not sure if this will speed up the processing, but still worth trying).

If it’s still slow, try to speed up the blurring using GPU. It should have a big impact on the speed. Here’s the code for CUDA (on Nvidia cards); for AMD cards you can try with OpenCL:

gpu_image = cv2.cuda_GpuMat()
gpu_blurred = cv2.cuda.blur(...)
img_blurred = gpu_blurred.download()
...same code as above...

yes, all at once, not per-ROI (and then copy regions as needed). I’d recommend that definitely.