Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc6a81da

Browse files
committed
added blurring faces in images/videos tutorial
1 parent4d9d332 commitc6a81da

File tree

11 files changed

+1967
-1
lines changed

11 files changed

+1967
-1
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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).

‎machine-learning/blur-faces/README.md

Lines changed: 17 additions & 0 deletions
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+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
importcv2
2+
importnumpyasnp
3+
importsys
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+
foriinrange(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+
ifconfidence>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)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
importcv2
2+
importnumpyasnp
3+
importtime
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+
whileTrue:
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+
foriinrange(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+
ifconfidence>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+
ifcv2.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()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
importcv2
2+
importnumpyasnp
3+
importtime
4+
importsys
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+
whileTrue:
22+
start=time.time()
23+
captured,image=cap.read()
24+
# get width and height of the image
25+
ifnotcaptured:
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+
foriinrange(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+
ifconfidence>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+
ifcv2.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()
Loading
47.9 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python
2+
numpy

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp