Skip to content
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

Created and implemented Overlay face function #3241

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

tsu-ki
Copy link
Contributor

@tsu-ki tsu-ki commented Jan 19, 2025

Closes: #2527
#3243
I've also made changes to delete_issue function to reduce time complexity and better optimisation

Screen.Recording.2025-01-19.at.6.10.46.PM.mov

@DonnieBLT
Copy link
Collaborator

I'm concerned about the memory usage of numpy, is there a way to know how much more memory this will consume?

@tsu-ki
Copy link
Contributor Author

tsu-ki commented Jan 26, 2025

@DonnieBLT sir, I'm unsure of why pre-commit test is failing continuously even though I'm running pre-commit everytime before committing the changes. Please suggest what should I do to resolve this, thank you!

Screenshot 2025-01-26 at 5 17 02 PM

@tsu-ki
Copy link
Contributor Author

tsu-ki commented Jan 26, 2025

Collaborator

I've added tracemalloc to track and display the memory consumption during execution of overlay_function. I feel the memory consumption won't increase significantly but if needed, I can make further changes to decrease it.

@DonnieBLT
Copy link
Collaborator

Can you please add an automated test to this so that it starts with a face, then upload it to an issue and then check the issue and make sure that it does not detect a face

@DonnieBLT
Copy link
Collaborator

@DonnieBLT sir, I'm unsure of why pre-commit test is failing continuously even though I'm running pre-commit everytime before committing the changes. Please suggest what should I do to resolve this, thank you!

Screenshot 2025-01-26 at 5 17 02 PM

It’s precommit formatting

@tsu-ki
Copy link
Contributor Author

tsu-ki commented Jan 26, 2025

Can you please add an automated test to this so that it starts with a face, then upload it to an issue and then check the issue and make sure that it does not detect a face

sure sir, I'll do that. thank you!

@tsu-ki
Copy link
Contributor Author

tsu-ki commented Feb 4, 2025

Can you please add an automated test to this so that it starts with a face, then upload it to an issue and then check the issue and make sure that it does not detect a face

@DonnieBLT sir, can you please explain what was required to be done here?

@DonnieBLT
Copy link
Collaborator

Can you please add an automated test to this so that it starts with a face, then upload it to an issue and then check the issue and make sure that it does not detect a face

@DonnieBLT sir, can you please explain what was required to be done here?

Create a test

@tsu-ki
Copy link
Contributor Author

tsu-ki commented Feb 5, 2025

@DonnieBLT sir, please review the changes. thank you!

Screen.Recording.2025-02-05.at.5.46.25.PM.mov

@tsu-ki tsu-ki requested a review from DonnieBLT February 5, 2025 14:33
Copy link
Collaborator

@DonnieBLT DonnieBLT left a comment

Choose a reason for hiding this comment

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

Please remove unused, dependencies, and separate the functionality into a new PR if it’s still there

@tsu-ki
Copy link
Contributor Author

tsu-ki commented Feb 5, 2025

@DonnieBLT
before:

Screenshot 2025-02-05 at 10 08 41 PM

after downloading dependencies: (opencv-python, numpy):

Screenshot 2025-02-05 at 10 10 12 PM

@tsu-ki tsu-ki requested a review from DonnieBLT February 7, 2025 12:40
Copy link
Collaborator

@DonnieBLT DonnieBLT left a comment

Choose a reason for hiding this comment

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

lets do this without 3rd party dependencies - something like this

import os
import urllib.request
from xml.etree import ElementTree
from PIL import Image, ImageDraw

# Load the Haar cascade file (pre-trained model for face detection)
HAAR_CASCADE_URL = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml"
HAAR_CASCADE_PATH = "haarcascade_frontalface_default.xml"

# Download Haar cascade if not exists
if not os.path.exists(HAAR_CASCADE_PATH):
    urllib.request.urlretrieve(HAAR_CASCADE_URL, HAAR_CASCADE_PATH)

# Parse Haar cascade XML file
def parse_haar_cascade(xml_path):
    """Parses Haar cascade XML and extracts feature rectangles."""
    tree = ElementTree.parse(xml_path)
    root = tree.getroot()
    features = []
    for stage in root.findall(".//stages/_/trees/_/feature/rects/_"):
        feature_rects = []
        for rect in stage.text.strip().split("\n"):
            rect = rect.split()
            x, y, w, h = map(int, rect[:4])
            feature_rects.append((x, y, w, h))
        features.append(feature_rects)
    return features

# Face detection function using Haar features
def detect_faces(image):
    """Detect faces in an image using Haar features."""
    width, height = image.size
    features = parse_haar_cascade(HAAR_CASCADE_PATH)
    detected_faces = []
    
    for x in range(0, width - 24, 2):  # Scanning the image
        for y in range(0, height - 24, 2):
            for feature in features:
                if all(x + w < width and y + h < height for (_, _, w, h) in feature):
                    detected_faces.append((x, y, 24, 24))  # Assume minimum size of 24x24 pixels
    return detected_faces

# Function to blur detected faces
def blur_faces(image, faces):
    """Apply a blur effect on detected faces."""
    draw = ImageDraw.Draw(image)
    for x, y, w, h in faces:
        for i in range(5):  # Create blur effect using rectangles
            draw.rectangle([x + i, y + i, x + w - i, y + h - i], fill=(200, 200, 200, 128))
    return image

# Load the image
image_path = "test_image.jpg"  # Replace with your image path
image = Image.open(image_path).convert("RGB")

# Detect faces
faces = detect_faces(image)

# Apply blur
processed_image = blur_faces(image, faces)

# Save or show the processed image
processed_image.show()  # Display the image
processed_image.save("blurred_faces.jpg")  # Save the processed image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically detect and cover faces in images
2 participants