DEV Community

El Bruno for Microsoft Azure

Posted on • Originally published at elbruno.com on

7 1

#OpenCV – Detect and blur faces 😁 using haar cascades in Python 🐍

Hi !

Quick post today: detect a face using haar cascades and blur the face area. Easy to implement, and also easy to read:

face blur on a camera view

Code below

# Copyright (c) 2022
# Author : Bruno Capuano
# Create Time : 2022 Feb
# Change Log :
# - Open a camera feed from a local webcam and analyze each frame to detect faces using haar cascades
# - When a face is detected, the app will blur the face zone
# - Press [Q] to quit the app
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import cv2
import time
video_capture = cv2.VideoCapture(0)
time.sleep(2)
# enable face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# open
while True:
try:
_, frameOrig = video_capture.read()
frame = cv2.resize(frameOrig, (640, 480))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (top, right, bottom, left) in faces:
cv2.rectangle(frame,(top,right),(top+bottom,right+left),(0,0,255),2)
face = frame[right:right+left, top:top+bottom]
face = cv2.GaussianBlur(face,(23, 23), 30)
# merge this blurry rectangle to our final image
frame[right:right+face.shape[0], top:top+face.shape[1]] = face
cv2.imshow('@elbruno - Face Blur', frame)
except Exception as e:
print(f'exc: {e}')
pass
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

Important: Haar cascades are easy to implement and learn, however, not recommented as a good solution to detect faces.

Face Recognition and Face Detection series in Python

  1. Detecting Faces with 20 lines in Python
  2. Face Recognition with 20 lines in Python
  3. Detecting Facial Features with 20 lines in Python
  4. Facial Features and Face Recognition with 20 lines in Python
  5. Performance improvements with code
  6. Resize the camera input with OpenCV
  7. Working with Haar Cascades and OpenCV
  8. Detect and blur faces using haar cascades

Original Post

Happy coding!

Greetings

El Bruno


AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (3)

Collapse
 
64j0 profile image
Vinícius Gajo

Hey, is it possible to write this with F#?

Collapse
 
elbruno profile image
El Bruno

Hi Vinicius, yes sure !
I need to update my F# skills, however, I'll give it a try

Collapse
 
64j0 profile image
Vinícius Gajo

Cool, thanks for the reply. I'm currently studying opencv using Python since it is easy to find books and docs, but my final goal is to implement it with F#

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more