Detect and Blurr Photos for Photo De-identification

Privacy for George Floyd Protesters

Finding and blurring faces in photos

Matt Davis
3 min readJun 8, 2020

--

We are living in an interesting time. The world is the most connected it has ever been however, we need to acknowledge the consequences of that. The bias that is inherent to humans needs new approaches as they are increasingly being pointed out through social media and the cameras in our phones.

While we cannot change people, we can protect those who are risking their lives to point out these atrocities. That is to say, we should be able to stop the identification of individuals in photo’s but we cannot change habits of posting our own videos to our own social media. Thus we will focus on enabling the technology that will help to protect those people, while they will need to focus on taking their own precautions.

What needs to be done:

  • Find all identifiable features in a photo
  • Scrub those features

We will focus on “Direct Identifiers” and for this article, we will focus on facial identification and blurring.

Libraries: OpenCV(Open Source Computer Vision Library)

OpenCV is a well developed and supported library that has a lot of features. A number of experiments have been developed using this library, including a number of facial recognition/identification. An existing config is available for facial identification. The code below identifies the faces.

#Config the pipeline and get the face location dataface_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')img = cv2.imread(allfiles[0])#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces = face_cascade.detectMultiScale(img, 1.3, 5)print(faces)

This will print out the quad box coordinates of the faces within the photo. In my testing, it works reasonably well for front-facing identification. There does seem to be some issues when the faces are not totally visible, but we will take a closer look at that for version 2.

I turned myse

I turned blue to demo a fun experiment:

We can see the bounding box around the face here. This bounding box is all we need to blur the image from identification.

The bounding box has now been blurred and we use some simple functions to do the blurring while allowing some configuration.

def anonymize_face(image, blocks=6):(h, w) = image.shape[:2]xSteps = np.linspace(0, w, blocks + 1, dtype='int')ySteps = np.linspace(0, h, blocks + 1, dtype='int')for i in range(1, len(ySteps)):for j in range(1, len(xSteps)):startX = xSteps[j - 1]startY = ySteps[i - 1]endX = xSteps[j]endY = ySteps[i]roi = image[startY: endY, startX:endX](B, G, R) = [int(x) for x in cv2.mean(roi)[:3]]cv2.rectangle(image, (startX, startY), (endX, endY), (B, G, R), -1)return imagedef find_and_blur(bw, color):cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')faces = cascade.detectMultiScale(bw, 1.1, 4)for (x, y, w, h) in faces:color[y:y+h, x:x+w] = anonymize_face(color[y:y+h, x:x+w])

Now we have a simple approach for blurring the photos. Next, we will need to scrub the data for identification and will expand it into hidden in plain site so that we can utilize the de-identification of photos also for machine learning to remove bias. We have implemented it into an easier to use the library and will expand it’s functionality with the end goal of privacy and removing bias from data so that we can train machine learning without inheriting any unethical bias.

Github Repo: https://github.com/Deamoner/privyfilter

Colab Notebook: https://github.com/Deamoner/privyfilter/blob/master/Notebooks/Privy_Filter_Face_Detection_and_Bluring_in_Photos.ipynb

Privy Article: Privy filter — filtering privacy and bias from photos

--

--