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

Automatically detect and cover faces in images #2527

Open
DonnieBLT opened this issue Jul 31, 2024 · 7 comments · May be fixed by #3241
Open

Automatically detect and cover faces in images #2527

DonnieBLT opened this issue Jul 31, 2024 · 7 comments · May be fixed by #3241

Comments

@DonnieBLT
Copy link
Collaborator

DonnieBLT commented Jul 31, 2024

We'll create a new function called overlay_faces that overlays a solid color on detected faces.

import cv2
import numpy as np
from django.core.files.base import ContentFile
from django.shortcuts import render, redirect
from .forms import ImageUploadForm  # Assuming you have a form for image upload
from .models import UploadedImage  # Assuming you have a model to store uploaded images

def overlay_faces(image, color=(0, 0, 0)):
    # Convert image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Load OpenCV's pre-trained Haar Cascade face detection model
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

    # Detect faces in the image
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

    # Overlay a solid color on each face found
    for (x, y, w, h) in faces:
        # Draw a filled rectangle over the face region with the specified color
        cv2.rectangle(image, (x, y), (x+w, y+h), color, thickness=cv2.FILLED)

    return image

def image_upload_view(request):
    if request.method == 'POST':
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid():
            image_instance = form.save(commit=False)

            # Read the uploaded image file
            image_file = request.FILES['image']
            np_img = np.frombuffer(image_file.read(), np.uint8)
            img = cv2.imdecode(np_img, cv2.IMREAD_COLOR)

            # Overlay faces in the image with a solid color (default is black)
            img_with_overlayed_faces = overlay_faces(img, color=(0, 0, 0))

            # Convert the image back to a format that can be saved in Django model
            _, buffer = cv2.imencode('.jpg', img_with_overlayed_faces)
            image_instance.image.save(image_file.name, ContentFile(buffer.tobytes()), save=False)

            # Save the image instance
            image_instance.save()
            return redirect('success_url')  # Replace 'success_url' with your actual success URL

    else:
        form = ImageUploadForm()
    return render(request, 'upload.html', {'form': form})

Explanation of Changes

  1. overlay_faces(image, color=(0, 0, 0)) Function:

    • Similar to the previous blur_faces function, but instead of applying a blur, it draws a filled rectangle over each detected face.
    • The color parameter defines the color of the rectangle. The default is black (0, 0, 0) in BGR format.
  2. Usage:

    • The overlay_faces function is called within the image_upload_view with a color of your choice to overlay the faces with that color.

Customizing the Color

You can customize the color parameter when calling overlay_faces. The color should be in BGR format (Blue, Green, Red). For example:

  • Black: (0, 0, 0)
  • White: (255, 255, 255)
  • Red: (0, 0, 255)
  • Green: (0, 255, 0)
  • Blue: (255, 0, 0)

Example:

To overlay the faces with a solid red color, modify the call in image_upload_view like this:

img_with_overlayed_faces = overlay_faces(img, color=(0, 0, 255))  # Red color

This setup will now overlay a solid color on any detected faces in the uploaded images.

@DonnieBLT
Copy link
Collaborator Author

/bounty $10

@DonnieBLT DonnieBLT added this to the 🐞 New Issue Detail Page milestone Jul 31, 2024
@sagnik3788
Copy link
Contributor

sagnik3788 commented Oct 8, 2024

Right now if we upload images its not visible So do we need this feature ?
@DonnieBLT

@neelabh2710
Copy link

Hi @DonnieBLT, I am new to this community, but I think I can fix this issue, so can you please assign this to me if you still need this feature

Copy link
Contributor

Hello @neelabh2710! You've been assigned to OWASP-BLT/BLT. You have 24 hours to complete a pull request. To place a bid and potentially earn some BCH, type /bid [amount in BCH] [BCH address].

@github-actions github-actions bot assigned neelabh2710 and unassigned neelabh2710 Dec 10, 2024
Copy link
Contributor

⏰ This issue has been automatically unassigned due to 24 hours of inactivity.
The issue is now available for anyone to work on again.

@tsu-ki
Copy link
Contributor

tsu-ki commented Dec 29, 2024

@DonnieBLT sir, does this still needs to be implemented? If yes, can you please elaborate where this would be implemented.
thank you!

@DonnieBLT
Copy link
Collaborator Author

When images are uploaded for bugs

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

Successfully merging a pull request may close this issue.

4 participants