OWASP-BLT / BLT

OWASP BLT is tool with the purpose of making the web a safer place. We have many facets to the project.
https://blt.owasp.org
GNU Affero General Public License v3.0
136 stars 138 forks source link

Automatically detect and cover faces in images #2527

Open DonnieBLT opened 3 months ago

DonnieBLT commented 3 months ago

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:

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 commented 3 months ago

/bounty $10

sagnik3788 commented 1 month ago

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