-
-
Notifications
You must be signed in to change notification settings - Fork 364
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
2807975
06edf46
9a30978
e452ff2
8266a36
4a5ee0c
ea141c4
a9e69cf
4eaf971
2ede0b3
a82cbce
882022f
6554608
b7e283b
319f580
b11fbf6
a92b016
dea5923
3fc57af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||||||||||||||||||||||
|
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.
There was a problem hiding this comment.
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.
Outdated
There was a problem hiding this comment.
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'.
#save video function | |
# Save video function |
Copilot uses AI. Check for mistakes.
ChrisEssomba marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
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.
return out | |
return out |
Copilot uses AI. Check for mistakes.
Outdated
There was a problem hiding this comment.
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.
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.
Outdated
There was a problem hiding this comment.
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.
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.
Outdated
There was a problem hiding this comment.
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.
Outdated
There was a problem hiding this comment.
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.
# 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.
Outdated
There was a problem hiding this comment.
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.'
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.
Outdated
There was a problem hiding this comment.
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().
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.
Outdated
There was a problem hiding this comment.
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.
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.
Outdated
There was a problem hiding this comment.
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.
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.
Outdated
There was a problem hiding this comment.
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.
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.
Outdated
There was a problem hiding this comment.
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.
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.
Outdated
There was a problem hiding this comment.
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.
output_folder = "./output_videos/" | |
output_folder = OUTPUT_VIDEOS_DIR |
Copilot uses AI. Check for mistakes.
Outdated
There was a problem hiding this comment.
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")
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.
Outdated
There was a problem hiding this comment.
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)'.
out= save_video(video, output_path) | |
out = save_video(video, output_path) |
Copilot uses AI. Check for mistakes.
Uh oh!
There was an error while loading. Please reload this page.