Skip to content

add real time face blurring script #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions Real-Time-Face-Blurring-Tool/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import os
import cv2
import numpy as np

# Load DNN model once to avoid reloading it for each frame
def load_face_detection_model():
"""Loads the pre-trained face detection model."""
prototxt_path = "./protocol/deploy.prototxt.txt"
model_path = "./model/res10_300x300_ssd_iter_140000_fp16.caffemodel"
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded file paths should be made configurable or use relative paths that work from different execution contexts. Consider using os.path.join() for cross-platform compatibility.

Suggested change
prototxt_path = "./protocol/deploy.prototxt.txt"
model_path = "./model/res10_300x300_ssd_iter_140000_fp16.caffemodel"
base_dir = os.path.dirname(__file__)
prototxt_path = os.path.join(base_dir, "protocol", "deploy.prototxt.txt")
model_path = os.path.join(base_dir, "model", "res10_300x300_ssd_iter_140000_fp16.caffemodel")

Copilot uses AI. Check for mistakes.

return cv2.dnn.readNetFromCaffe(prototxt_path, model_path)

# Global variable to store the model (avoids reloading it multiple times)
face_net = load_face_detection_model()
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading the model at module level without error handling could cause the entire module to fail if model files are missing. Consider adding try-catch blocks and proper error handling.

Copilot uses AI. Check for mistakes.


#save video function
Copy link
Preview

Copilot AI Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should follow proper Python comment formatting with a space after the hash and proper capitalization: '# Save video function'.

Suggested change
#save video function
# Save video function

Copilot uses AI. Check for mistakes.

def save_video(video, output_path):

# Get video properties
fps = video.get(cv2.CAP_PROP_FPS)
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
res = (frame_width, frame_height)

# Define the video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # MP4 codec
out = cv2.VideoWriter(output_path, fourcc, fps, res)
return out
Copy link
Preview

Copilot AI Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before 'out' should be removed for consistent formatting.

Suggested change
return out
return out

Copilot uses AI. Check for mistakes.


def blur_faces(image, confidence_threshold=0.5, blur_strength=61):
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 61 for blur_strength should be defined as a named constant to improve code maintainability and make it easier to adjust.

Suggested change
def blur_faces(image, confidence_threshold=0.5, blur_strength=61):
def blur_faces(image, confidence_threshold=0.5, blur_strength=DEFAULT_BLUR_STRENGTH):

Copilot uses AI. Check for mistakes.

"""
Detects and blurs faces in an image.

Parameters:
image (numpy.ndarray): Input image.
confidence_threshold (float): Minimum confidence for face detection.
blur_strength (int): Kernel size for Gaussian blur (must be odd).

Returns:
numpy.ndarray: Image with blurred faces.
"""
(h, w) = image.shape[:2]

# Prepare the image for the deep learning model
blob = cv2.dnn.blobFromImage(
cv2.resize(image, (300, 300)),
1.0,
(300, 300),
(104.0, 177.0, 123.0)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers (300, 300) and (104.0, 177.0, 123.0) should be defined as named constants to improve code readability and maintainability.

Suggested change
cv2.resize(image, (300, 300)),
1.0,
(300, 300),
(104.0, 177.0, 123.0)
cv2.resize(image, INPUT_SIZE),
1.0,
INPUT_SIZE,
MEAN_VALUES

Copilot uses AI. Check for mistakes.

)

# Perform detection
face_net.setInput(blob)
detections = face_net.forward()

# Process detections
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]

if confidence > confidence_threshold:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")

# Ensure coordinates stay within image bounds
startX, startY = max(0, startX), max(0, startY)
endX, endY = min(w, endX), min(h, endY)

# Extract and blur face ROI
face_roi = image[startY:endY, startX:endX]
blurred_face = cv2.GaussianBlur(face_roi, (blur_strength, blur_strength), 0)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't validate that blur_strength is odd, which is required for cv2.GaussianBlur. If an even number is passed, OpenCV will raise an error.

Copilot uses AI. Check for mistakes.

image[startY:endY, startX:endX] = blurred_face
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no validation that the face ROI has non-zero dimensions. If startY >= endY or startX >= endX, this will create an empty array that could cause issues with GaussianBlur.

Suggested change
# Extract and blur face ROI
face_roi = image[startY:endY, startX:endX]
blurred_face = cv2.GaussianBlur(face_roi, (blur_strength, blur_strength), 0)
image[startY:endY, startX:endX] = blurred_face
# Validate ROI dimensions
if endY > startY and endX > startX:
# Extract and blur face ROI
face_roi = image[startY:endY, startX:endX]
blurred_face = cv2.GaussianBlur(face_roi, (blur_strength, blur_strength), 0)
image[startY:endY, startX:endX] = blurred_face

Copilot uses AI. Check for mistakes.


return image


def blur_faces_images(image_path):
"""
Load an image, blurs detected faces in each frame, and saves the output.
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring incorrectly mentions 'each frame' when this function processes a single image, not video frames. It should read 'Load an image, blur detected faces, and save the output.'

Suggested change
Load an image, blurs detected faces in each frame, and saves the output.
Load an image, blur detected faces, and save the output.

Copilot uses AI. Check for mistakes.


Parameters:
image_path (str): Path to the input image.
"""

# Load and process the image
image = cv2.imread(image_path)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't check if cv2.imread successfully loaded the image. If the file doesn't exist or is corrupted, image will be None, causing a crash in blur_faces().

Suggested change
image = cv2.imread(image_path)
image = cv2.imread(image_path)
if image is None:
print(f"Error: Unable to load image from path '{image_path}'. Please check the file path or file integrity.")
return

Copilot uses AI. Check for mistakes.

blurred_image = blur_faces(image)

# Create output folder
name = os.path.basename(image_path)
output_folder = "./output_images/"
os.makedirs(output_folder, exist_ok=True)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded output paths should be configurable. Consider making output directories configurable through parameters or constants to improve maintainability.

Suggested change
output_folder = "./output_images/"
os.makedirs(output_folder, exist_ok=True)
output_folder = OUTPUT_IMAGES_DIR
os.makedirs(OUTPUT_IMAGES_DIR, exist_ok=True)

Copilot uses AI. Check for mistakes.


# Save the processed image in the right folder
output_path = os.path.join(output_folder, name)
cv2.imwrite(output_path, blurred_image)

def blur_faces_videos(video_path):
"""
Processes a video, blurs detected faces in each frame, and saves the output.

Parameters:
video_path (str): Path to the input video.
"""

name = os.path.basename(video_path)
output_folder = "./output_videos/"
os.makedirs(output_folder, exist_ok=True)

# Ensure the output file has a valid extension
output_path = os.path.join(output_folder, os.path.splitext(name)[0] + "_blurred.mp4")
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded output paths should be configurable. Consider making output directories configurable through parameters or constants to improve maintainability.

Suggested change
output_folder = "./output_videos/"
os.makedirs(output_folder, exist_ok=True)
# Ensure the output file has a valid extension
output_path = os.path.join(output_folder, os.path.splitext(name)[0] + "_blurred.mp4")
os.makedirs(OUTPUT_VIDEOS_DIR, exist_ok=True)
# Ensure the output file has a valid extension
output_path = os.path.join(OUTPUT_VIDEOS_DIR, os.path.splitext(name)[0] + "_blurred.mp4")

Copilot uses AI. Check for mistakes.


video, output_path, out= save_video(video_path)
Copy link
Preview

Copilot AI Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The save_video function only takes 2 parameters (video and output_path) but this line expects it to return 3 values. The function call should be 'video = cv2.VideoCapture(video_path)' and 'out = save_video(video, output_path)' on separate lines.

Suggested change
video, output_path, out= save_video(video_path)
video = cv2.VideoCapture(video_path)
out = save_video(video, output_path)

Copilot uses AI. Check for mistakes.


while True:
ret, frame = video.read()
if not ret:
break

blurred_frame = blur_faces(frame) # Apply face blurring
cv2.imshow('Blurred Video', blurred_frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

out.write(blurred_frame) # Save the processed frame

# Release resources
out.release()
video.release()
cv2.destroyAllWindows()
print(f"Video saved at: {output_path}")


def blur_face_webcam():
"""
Captures video from the webcam, applies face blurring in real-time,
and allows stopping the recording by pressing 'q'.
"""

video = cv2.VideoCapture(0) # Open webcam
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't check if the webcam was successfully opened. If no camera is available, this could cause issues in the processing loop.

Suggested change
video = cv2.VideoCapture(0) # Open webcam
video = cv2.VideoCapture(0) # Open webcam
if not video.isOpened():
print("Error: Unable to access the webcam. Please ensure it is connected and not in use.")
return

Copilot uses AI. Check for mistakes.

output_folder = "./output_videos/"
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded output paths should be configurable. Consider making output directories configurable through parameters or constants to improve maintainability.

Suggested change
output_folder = "./output_videos/"
output_folder = OUTPUT_VIDEOS_DIR

Copilot uses AI. Check for mistakes.

os.makedirs(output_folder, exist_ok=True)

# Ensure the output file has a valid extension
output_path = os.path.join(output_folder, ("webcam_blurred.mp4"))
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary parentheses around the string literal. This should be simplified to: os.path.join(output_folder, "webcam_blurred.mp4")

Suggested change
output_path = os.path.join(output_folder, ("webcam_blurred.mp4"))
output_path = os.path.join(output_folder, "webcam_blurred.mp4")

Copilot uses AI. Check for mistakes.


out= save_video(video, output_path)
Copy link
Preview

Copilot AI Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space around assignment operator. Should be 'out = save_video(video, output_path)'.

Suggested change
out= save_video(video, output_path)
out = save_video(video, output_path)

Copilot uses AI. Check for mistakes.

while True:
ret, frame = video.read()
if not ret:
break

blurred_frame = blur_faces(frame)
cv2.imshow('Blurred Webcam Feed', blurred_frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break
out.write(blurred_frame) # Save the processed frame

# Release resources
out.release()
video.release()
cv2.destroyAllWindows()
print(f"Video saved at: {output_path}")

Binary file not shown.
Loading