current position:Home>Blurring and anonymizing faces using OpenCV and python

Blurring and anonymizing faces using OpenCV and python

2022-01-31 02:51:53 Haiyong

「 This is my participation 11 The fourth of the yuegengwen challenge 4 God , Check out the activity details :2021 One last more challenge

Author URI : Hai Yong
Author's brief introduction :HDZ Core group members 、 High quality creators in the whole stack field 、 Reelection C Standing in the top ten of the weekly list
Fan benefits : Into the Fans group Send four books a week ( Everyone has ), Send all kinds of small gifts every month ( Nuggets enamel cup 、 Pillow 、 Mouse pad 、 Mugs, etc )

In this paper , We will learn how to use OpenCV and Python Blur and anonymize faces .

So , We will use cascaded classifiers to detect faces . Make sure to download the same from this link xml file :

drive.google.com/file/d/1PPO…

Method

  • First , We use the built-in face detection algorithm , Face detection from real-time video or image . ad locum , We will use a cascaded classifier method to extract data from real-time video ( Use a webcam ) Face detection in .
  • then , Read frames from real-time video . Store the latest frame and convert it to grayscale , To better understand the characteristics .
  • Now? , In order to make the output beautiful , We will make a color border rectangle around the detected face . however , We want the detected face to be blurred , So we use the median fuzzy function to do the same thing , And mention the area where the face should be blurred .
  • and , Now we want to show the blurred face , Use imshow The frame read by the function , We want it to be shown , Until we press a key .

Step by step implementation :

step 1: Import face detection algorithm , It is called cascade classifier .

import cv2
#  Face detection 
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
 Copy code 

step 2: Capture frames from video , To detect faces from frames

video_capture = cv2.VideoCapture(0)
while True:

	#  Capture the latest frame from the video 
	check, frame = video_capture.read()
 Copy code 

step 3: Change the captured frame to grayscale .

#  Convert frames to grayscale ( Black and white shadows )
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = cascade.detectMultiScale(gray_image,
								scaleFactor=2.0,
								minNeighbors=4)
 Copy code 

step 4: Draw a color rectangle around the detected face .

for x, y, w, h in face:

	#  Draw a border around the detected face 
	# ( The border color here is green , The thickness is 3)
	image = cv2.rectangle(frame, (x, y),
						(x+w, y+h),
						(0, 255, 0), 3)
 Copy code 

step 5: Blur the part inside the rectangle ( Contains the detected face ).

#  Blur the face in the rectangle 
image[y:y+h, x:x+w] = cv2.medianBlur(image[y:y+h, x:x+w], 35)
 Copy code 

step 6: Show final output , That is, the detected face ( In rectangle ) It's fuzzy .

#  Show blurred faces in the video 
cv2.imshow('face blurred', frame)
key = cv2.waitKey(1)
 Copy code 

Here is the complete implementation :

import cv2

#  Face detection 
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

# VideoCapture  It's a function , Used to capture video from cameras connected to the system 
#  You can pass on  0  or  1
# 0  For laptop webcam 
# 1  For external webcam 
video_capture = cv2.VideoCapture(0)

#  One while Loop runs infinite times , Capture an unlimited number of frames for video , Because video is a combination of frames 
while True:

	#  Capture the latest frame from the video 
	check, frame = video_capture.read()

	#  Convert frames to grayscale ( Black and white shadows )
	gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

	#  Multiple faces are detected in the captured frame 
        # scaleFactor: Parameter specifies how much the image size is reduced at each image scale .
        # minNeighbors:  Parameter specifies how many neighbors each rectangle should have to keep it .
        #  The rectangle contains the detection object .
        #  The object here is the face .
	face = cascade.detectMultiScale(
		gray_image, scaleFactor=2.0, minNeighbors=4)

	for x, y, w, h in face:

		#  Draw a border around the detected face .
                #( The border color here is green , The thickness is 3)
		image = cv2.rectangle(frame, (x, y), (x+w, y+h),
							(0, 255, 0), 3)

		#  Blur the face in the rectangle 
		image[y:y+h, x:x+w] = cv2.medianBlur(image[y:y+h, x:x+w],
											35)

	#  Show blurred faces in the video 
	cv2.imshow('face blurred', frame)
	key = cv2.waitKey(1)

	#  This statement runs only once per frame .
        #  Basically , If we get a key , And that key is a  q
	if key == ord('q'):
		break

#  We will pause and exit  while  loop ,
#  And then run :
video_capture.release()
cv2.destroyAllWindows()
 Copy code 

Output :

image.png

Write in the last

I've been writing a technology blog for a long time , And mainly through the Nuggets , This is one of my articles OpenCV and Python Blur and anonymize faces . I like to share technology and happiness through articles . You can visit my blog : juejin.cn/user/204034… To learn more . I hope you will like !

You are welcome to put forward your opinions and suggestions in the comment area !

copyright notice
author[Haiyong],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310251516010.html

Random recommended