|
import os |
|
import gradio as gr |
|
from services.aws_service import AwsService |
|
from dotenv import load_dotenv |
|
from image_crop import crop_faces |
|
import cv2 |
|
import numpy as np |
|
from PIL import ImageOps |
|
|
|
load_dotenv() |
|
|
|
def crop_photoshoot_images(photo_shoot_id): |
|
folder = "PhotoShoots/" + str(photo_shoot_id) + "/Inputs" |
|
files = AwsService.get_files_from_s3(os.environ.get('AWS_S3_BUCKET'), folder) |
|
|
|
for file in files: |
|
s3_object = AwsService.get_image_from_s3(os.environ.get('AWS_S3_BUCKET'), file['Key']) |
|
image = ImageOps.exif_transpose(s3_object["pil"]) |
|
key = s3_object["key"] |
|
image.save(f"temp/{key}.jpg") |
|
cv2_img = cv2.imread(f"temp/{key}.jpg") |
|
faces = crop_faces(cv2_img) |
|
i = 1 |
|
for face in faces: |
|
filename = f'{key}-{i}.jpg' |
|
cv2.imwrite(filename, face) |
|
AwsService.send_image_to_s3(filename, os.environ.get('AWS_S3_BUCKET'), f'PhotoShoots/{photo_shoot_id}/Croppeds/{key}-{i}.jpg') |
|
os.remove(filename) |
|
i += 1 |
|
os.remove(f"temp/{key}.jpg") |
|
|
|
return {"message": f"{len(files)} images cropped successfully"} |
|
|
|
iface = gr.Interface( |
|
fn=crop_photoshoot_images, |
|
inputs=[gr.Textbox(lines=1, placeholder="Photo Shoot ID")], |
|
outputs=["text"] |
|
) |
|
|
|
iface.launch() |