Skip to content

Commit c6a81da

Browse files
committed
added blurring faces in images/videos tutorial
1 parent 4d9d332 commit c6a81da

11 files changed

+1967
-1
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4242
- [How to Recognize Optical Characters in Images in Python](https://www.thepythoncode.com/article/optical-character-recognition-pytesseract-python). ([code](machine-learning/optical-character-recognition))
4343
- [How to Use K-Means Clustering for Image Segmentation using OpenCV in Python](https://www.thepythoncode.com/article/kmeans-for-image-segmentation-opencv-python). ([code](machine-learning/kmeans-image-segmentation))
4444
- [How to Perform YOLO Object Detection using OpenCV and PyTorch in Python](https://www.thepythoncode.com/article/yolo-object-detection-with-opencv-and-pytorch-in-python). ([code](machine-learning/object-detection))
45+
- [How to Blur Faces in Images using OpenCV in Python](https://www.thepythoncode.com/article/blur-faces-in-images-using-opencv-in-python). ([code](machine-learning/blur-faces))
4546
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
4647
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
4748
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).

Diff for: machine-learning/blur-faces/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# [How to Blur Faces in Images using OpenCV in Python](https://www.thepythoncode.com/article/blur-faces-in-images-using-opencv-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- To blur faces of the image `father-and-daughter.jpg`:
5+
```
6+
python blur_faces.py a-man-with-little-girl.jpg
7+
```
8+
This should show the blurred image and save it of the name `image_blurred.jpg` in your current directory.
9+
10+
- To blur faces using your live camera:
11+
```
12+
python blur_faces_live.py
13+
```
14+
- To blur faces of a video:
15+
```
16+
python blur_faces_video.py video.3gp
17+
```

Diff for: machine-learning/blur-faces/blur_faces.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import cv2
2+
import numpy as np
3+
import sys
4+
5+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
6+
prototxt_path = "weights/deploy.prototxt.txt"
7+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
8+
model_path = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
9+
10+
# load Caffe model
11+
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
12+
# get the image file name from the command line
13+
image_file = sys.argv[1]
14+
# read the desired image
15+
image = cv2.imread(image_file)
16+
# get width and height of the image
17+
h, w = image.shape[:2]
18+
# gaussian blur kernel size depends on width and height of original image
19+
kernel_width = (w // 7) | 1
20+
kernel_height = (h // 7) | 1
21+
# preprocess the image: resize and performs mean subtraction
22+
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
23+
# set the image into the input of the neural network
24+
model.setInput(blob)
25+
# perform inference and get the result
26+
output = np.squeeze(model.forward())
27+
for i in range(0, output.shape[0]):
28+
confidence = output[i, 2]
29+
# get the confidence
30+
# if confidence is above 40%, then blur the bounding box (face)
31+
if confidence > 0.4:
32+
# get the surrounding box cordinates and upscale them to original image
33+
box = output[i, 3:7] * np.array([w, h, w, h])
34+
# convert to integers
35+
start_x, start_y, end_x, end_y = box.astype(np.int)
36+
# get the face image
37+
face = image[start_y: end_y, start_x: end_x]
38+
# apply gaussian blur to this face
39+
face = cv2.GaussianBlur(face, (kernel_width, kernel_height), 0)
40+
# put the blurred face into the original image
41+
image[start_y: end_y, start_x: end_x] = face
42+
cv2.imshow("image", image)
43+
cv2.waitKey(0)
44+
cv2.imwrite("image_blurred.jpg", image)

Diff for: machine-learning/blur-faces/blur_faces_live.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import cv2
2+
import numpy as np
3+
import time
4+
5+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
6+
prototxt_path = "weights/deploy.prototxt.txt"
7+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
8+
model_path = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
9+
10+
# load Caffe model
11+
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
12+
13+
cap = cv2.VideoCapture(0)
14+
while True:
15+
start = time.time()
16+
_, image = cap.read()
17+
# get width and height of the image
18+
h, w = image.shape[:2]
19+
kernel_width = (w // 7) | 1
20+
kernel_height = (h // 7) | 1
21+
# preprocess the image: resize and performs mean subtraction
22+
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
23+
# set the image into the input of the neural network
24+
model.setInput(blob)
25+
# perform inference and get the result
26+
output = np.squeeze(model.forward())
27+
for i in range(0, output.shape[0]):
28+
confidence = output[i, 2]
29+
# get the confidence
30+
# if confidence is above 40%, then blur the bounding box (face)
31+
if confidence > 0.4:
32+
# get the surrounding box cordinates and upscale them to original image
33+
box = output[i, 3:7] * np.array([w, h, w, h])
34+
# convert to integers
35+
start_x, start_y, end_x, end_y = box.astype(np.int)
36+
# get the face image
37+
face = image[start_y: end_y, start_x: end_x]
38+
# apply gaussian blur to this face
39+
face = cv2.GaussianBlur(face, (kernel_width, kernel_height), 0)
40+
# put the blurred face into the original image
41+
image[start_y: end_y, start_x: end_x] = face
42+
cv2.imshow("image", image)
43+
if cv2.waitKey(1) == ord("q"):
44+
break
45+
time_elapsed = time.time() - start
46+
fps = 1 / time_elapsed
47+
print("FPS:", fps)
48+
49+
cv2.destroyAllWindows()
50+
cap.release()

Diff for: machine-learning/blur-faces/blur_faces_video.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import cv2
2+
import numpy as np
3+
import time
4+
import sys
5+
6+
# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
7+
prototxt_path = "weights/deploy.prototxt.txt"
8+
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel
9+
model_path = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
10+
11+
# load Caffe model
12+
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
13+
# get video file from command line
14+
video_file = sys.argv[1]
15+
# capture frames from video
16+
cap = cv2.VideoCapture(video_file)
17+
fourcc = cv2.VideoWriter_fourcc(*"XVID")
18+
_, image = cap.read()
19+
print(image.shape)
20+
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (image.shape[1], image.shape[0]))
21+
while True:
22+
start = time.time()
23+
captured, image = cap.read()
24+
# get width and height of the image
25+
if not captured:
26+
break
27+
h, w = image.shape[:2]
28+
kernel_width = (w // 7) | 1
29+
kernel_height = (h // 7) | 1
30+
# preprocess the image: resize and performs mean subtraction
31+
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
32+
# set the image into the input of the neural network
33+
model.setInput(blob)
34+
# perform inference and get the result
35+
output = np.squeeze(model.forward())
36+
for i in range(0, output.shape[0]):
37+
confidence = output[i, 2]
38+
# get the confidence
39+
# if confidence is above 40%, then blur the bounding box (face)
40+
if confidence > 0.4:
41+
# get the surrounding box cordinates and upscale them to original image
42+
box = output[i, 3:7] * np.array([w, h, w, h])
43+
# convert to integers
44+
start_x, start_y, end_x, end_y = box.astype(np.int)
45+
# get the face image
46+
face = image[start_y: end_y, start_x: end_x]
47+
# apply gaussian blur to this face
48+
face = cv2.GaussianBlur(face, (kernel_width, kernel_height), 0)
49+
# put the blurred face into the original image
50+
image[start_y: end_y, start_x: end_x] = face
51+
cv2.imshow("image", image)
52+
if cv2.waitKey(1) == ord("q"):
53+
break
54+
time_elapsed = time.time() - start
55+
fps = 1 / time_elapsed
56+
print("FPS:", fps)
57+
out.write(image)
58+
59+
cv2.destroyAllWindows()
60+
cap.release()
61+
out.release()

Diff for: machine-learning/blur-faces/father-and-daughter.jpg

63.9 KB
Loading

Diff for: machine-learning/blur-faces/image_blurred.jpg

47.9 KB
Loading

Diff for: machine-learning/blur-faces/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python
2+
numpy

0 commit comments

Comments
 (0)