Opencv, blur faces and licence plates from video stream

Viewed 36

I am trying to anonymize road camera streams. I would like to blur faces (front and sides), and licence plates.

The code below seems to do the trick BUT it is really slow.. I am really not used to opencv and my bet is that my code is not optimized at all. If i blur only the frontal faces for example, it runs nice and smooth. Any way to run this smoother while blurring face, profile and plates?

My xml files come from this repo : https://github.com/opencv/opencv/tree/master/data/haarcascades

Full code below :

import cv2

cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cascade2 = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_profileface.xml')
cascade3 = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_licence_plate_rus_16stages.xml')
cascade4 = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_russian_plate_number.xml')



def find_and_blur_face(bw, color): 
    # detect al faces
    faces = cascade.detectMultiScale(bw, 1.1, 4)
    # get the locations of the faces
    for (x, y, w, h) in faces:
        # select the areas where the face was found
        roi_color = color[y:y+h, x:x+w]
        # blur the colored image
        blur = cv2.GaussianBlur(roi_color, (101,101), 0)
        # Insert ROI back into image
        color[y:y+h, x:x+w] = blur            
    
    # return the blurred image
    return color

def find_and_blur_profile(bw, color): 
    # detect al profiles
    profiles = cascade2.detectMultiScale(bw, 1.1, 4)
    # get the locations of the profiles
    for (x, y, w, h) in profiles:
        # select the areas where the profiles were found
        roi_color = color[y:y+h, x:x+w]
        # blur the colored image
        blur = cv2.GaussianBlur(roi_color, (101,101), 0)
        # Insert ROI back into image
        color[y:y+h, x:x+w] = blur            
    
    # return the blurred image
    return color



def find_and_blur_plate(bw, color): 
    # detect licence plates
    plates = cascade3.detectMultiScale(bw, 1.1, 4)
    # get the locations of the plates
    for (x, y, w, h) in plates:
        # select the areas where the plates were found
        roi_color = color[y:y+h, x:x+w]
        # blur the colored image
        blur = cv2.GaussianBlur(roi_color, (101,101), 0)
        # Insert ROI back into image
        color[y:y+h, x:x+w] = blur            
    
    # return the blurred image
    return color

def find_and_blur_number(bw, color): 
    # detect licence plate numberes
    numbers = cascade4.detectMultiScale(bw, 1.1, 4)
    # get the locations of the numbers
    for (x, y, w, h) in numbers:
        # select the areas where the numbers were found
        roi_color = color[y:y+h, x:x+w]
        # blur the colored image
        blur = cv2.GaussianBlur(roi_color, (101,101), 0)
        # Insert ROI back into image
        color[y:y+h, x:x+w] = blur            
    
    # return the blurred image
    return color



# turn camera on
video_capture = cv2.VideoCapture(0)

while True:
    # get last recorded frame
    _, color = video_capture.read()
    # transform color -> grayscale
    bw = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)
    # detect the face and blur it
   # blur_face = find_and_blur_face(bw, color)
   # blur_profile = find_and_blur_profile(bw, color)
    blur_plate = find_and_blur_plate(bw, color)
    blur_number = find_and_blur_number(bw, color)
    
    # display output
    #cv2.imshow('Video', blur_face)
    #cv2.imshow('Video', blur_profile)
    cv2.imshow('Video', blur_plate)
    cv2.imshow('Video', blur_number)
    # break if q is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# turn camera off        
video_capture.release()
# close camera  window
cv2.destroyAllWindows()
0 Answers
Related