Skip to content

Commit 6e8015d

Browse files
Merge pull request #11 from Em-Vi/add-face-blur-tool
Add: [AI-ML] faceBlurTool
2 parents b8a2f19 + c1349a0 commit 6e8015d

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

1.2 KB

Contributor: Em-V

# Face Blur Tool

A simple Python tool that automatically detects and blurs faces in an image using OpenCV.

## Features
- Detects human faces using Haar Cascades.
- Blurs faces with a Gaussian blur.
- Saves the blurred image automatically.

## Installation
```bash
pip install -r requirements.txt

Usage

python main.py

Then enter the path of the image (e.g., photo.jpg).

The blurred image will be saved as blurred_photo.jpg.

Example

Input: original

Output: blurred

14.4 KB
Loading
45.4 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import cv2
2+
from cv2 import data
3+
4+
face_cascade = cv2.CascadeClassifier(data.haarcascades + "haarcascade_frontalface_default.xml")
5+
6+
image_path = input("Enter image path: ")
7+
image = cv2.imread(image_path)
8+
9+
if image is None:
10+
print("Image not found")
11+
exit()
12+
13+
# grayscale for better detection
14+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
15+
16+
# lets find faces
17+
# cascade detector runs on th eimage and returns a rectangle coordinate x,y,w,h
18+
# scalefactor repatedly scales image by value to detet faces in differnet sizes
19+
# minneighbour decides how many rectabgles should be overlapped to trigger face detection
20+
# minsize ignores detected objects smaller tha 30x30 pixels
21+
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30))
22+
23+
#looping over detected face coords
24+
for (x,y,w,h) in faces:
25+
# extacts the subarray containing detected face
26+
face = image[y:y+h,x:x+w]
27+
# numpy doesnt use a copy but views into image
28+
# lets apply guassian blur
29+
blurred_face = cv2.GaussianBlur(face,(99,99),30)
30+
# (99, 99) is the kernel size. stronger blr
31+
# 30 is sigmaX (standard deviation in the X direction). used to make rexziabke with face sizes
32+
# we can write blurred_face in original image
33+
image[y:y+h, x:x+w] = blurred_face
34+
35+
output_path = "blurred_" + image_path.split("/")[-1]
36+
cv2.imwrite(output_path,image)
37+
# show image...comment out if not needed
38+
cv2.imshow("Blurred Image", image)
39+
cv2.waitKey(0)
40+
cv2.destroyAllWindows()
41+
28 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)