Skip to content

Commit

Permalink
adding the blur_faces function
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanna7codes committed Feb 25, 2025
1 parent bb32eaa commit 0a14e01
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion open_prices/proofs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
from mimetypes import guess_extension
from pathlib import Path

from google.cloud import vision
import io

from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
from PIL import Image, ImageOps

from PIL import Image, ImageOps, ImageDraw

from open_prices.prices.constants import TYPE_CATEGORY, TYPE_PRODUCT
from open_prices.prices.models import Price
Expand Down Expand Up @@ -92,6 +96,25 @@ def generate_thumbnail(
return image_thumb_path


def blur_faces(image_path):
client = vision.ImageAnnotatorClient()
with io.open(image_path, 'rb') as image_file:
content = image_file.read()

image = vision.Image(content=content)
response = client.face_detection(image=image)
faces = response.face_annotations

im = Image.open(image_path)
draw = ImageDraw.Draw(im)

for face in faces:
box = [(vertex.x, vertex.y) for vertex in face.bounding_poly.vertices]
draw.rectangle(box, fill=(0, 0, 0, 128)) # Adjust as necessary

im.save(image_path)


def store_file(
file: InMemoryUploadedFile | TemporaryUploadedFile,
) -> tuple[str, str, str | None]:
Expand All @@ -115,6 +138,11 @@ def store_file(
# write the content of the file to the new file
with file_full_path.open("wb") as f:
f.write(file.file.read())

# Blur faces
blur_faces(file_full_path)


# create a thumbnail
image_thumb_path = generate_thumbnail(
current_dir, current_dir.name, file_stem, extension, mimetype
Expand Down

0 comments on commit 0a14e01

Please sign in to comment.