EAC5 / app.py
arriasol's picture
Upload 2 files
4bf9a70 verified
# -*- coding: utf-8 -*-
"""app.py
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/15qyKzD_stNTm2T02dcfigCUwBCWCvhC9
"""
import boto3
import gradio as gr
import os
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
aws_access_key_id='ASIA47CRXKELALROPM2U'
aws_secret_access_key='IIsyEKSpnAVhhDCim2r0L5B/tct2SC5XLfOGpxml'
aws_session_token='IQoJb3JpZ2luX2VjEHMaCXVzLXdlc3QtMiJHMEUCIERw9sSwx1FkzpY+DhHEFDa1WHJ8yftAroLM1/YBdSA8AiEA1DlF4xWdjYfPvfS5i7fvE/PQC3E1BxbuTUucN06QUYUqogIIHBAAGgw4OTEzNzcxMTEzMTgiDDRgtZMcW+XpR2Pjtir/AfZrtchBN5878TnwQvKhnv04r0j1D9ocYaBP+aBV0EgjiQH2NmlIlxSHsA8FiNfLMCqJw0s+NqUScryje3QbQ/65UQA6vKZilR9j8rj3vStqgBeM3v62myPo188jraK7HJXK33fGyceCoNcDeYMotAGIPYRT1XkWhgmTy7n4yMIr1Gw7AVOGoSV3N2k3DjUMpC6r67jQrPTlS4JhevwvKnEHAT2BOhd6NxXgn2PiQh03Bq63Xj1luo0/faYgRV/dzKrkAlAYI/ufXZWd0hHXBU9ylTGeb756luoONpkpaLEGu3eaz/QBiXdew1+6q1GWkp1F8hj6WbewgFFyUdbqKTCplJO6BjqdAX06ET6YwuEwrYeG5MbAozXPMoAn27pBFh195EQEpUu8iBCw/xUl7LuqTtpeBcESYfd1xLZI6k+RzpkK6ceWAN2u1YRm2ofli6G+hMatV381eavprjQ4kHKmc8Svzz/+uNlkCQ/mfnt1aQDiKE8fxevrK3Uz0/VfY8CtIoFJA5Qi7SzvtcZaxpF7iLIW4dAQ+mRMLeGZhdv/o2fIH4Y='
aws_region = 'us-east-1'
bucket_nom = "rekognitioneac5"
bucket_name = bucket_nom
client = boto3.client(
's3',
aws_access_key_id= aws_access_key_id,
aws_secret_access_key= aws_secret_access_key,
aws_session_token= aws_session_token,
region_name = aws_region
)
rekognition = boto3.client(
'rekognition',
aws_access_key_id= aws_access_key_id,
aws_secret_access_key= aws_secret_access_key,
aws_session_token= aws_session_token,
region_name = aws_region
)
def pujar_imatge_procesar_capses_s3(ruta_imatge):
resultats = []
objectName = os.path.basename(ruta_imatge)
client.upload_file(
ruta_imatge,
bucket_name,
objectName
)
response = rekognition.detect_faces(
Image={
'S3Object': {
'Bucket': bucket_name,
'Name': objectName
}
},
Attributes=['ALL']
)
image_file = client.get_object(
Bucket=bucket_name,
Key=objectName)
image_bytes = image_file['Body'].read()
image = Image.open(BytesIO(image_bytes))
dibuix = ImageDraw.Draw(image)
font_text = ImageFont.load_default()
for i, face in enumerate(response['FaceDetails'], 1):
resultats.append(f"\nCara {i}:")
resultats.append(f"Edat: {face['AgeRange']['Low']} - {face['AgeRange']['High']} anys")
resultats.append(f"Sexe: {face['Gender']['Value']}")
resultats.append(f"Emocio: {face['Emotions'][0]['Type']}")
bounding_box = face["BoundingBox"]
left = int(bounding_box["Left"] * image.width)
top = int(bounding_box["Top"] * image.height)
width = int(bounding_box["Width"] * image.width)
height = int(bounding_box["Height"] * image.height)
dibuix.rectangle([left, top, left + width, top + height], outline="red", width=2)
dibuix.text((left, top - 20), f"Cara {i}", font=font_text, fill="blue")
return "\n".join(resultats),image
with gr.Blocks() as demo:
gr.Markdown("# Analisi de cares amb Amazon Rekognition")
with gr.Row():
with gr.Column():
imatge_entrada = gr.Image(type="filepath",label="Carreguem la imatge")
with gr.Column():
text_sortida = gr.Textbox(label="Resultat")
imatge_resultant = gr.Image(type="pil",label="Imatges amb rectangles")
imatge_entrada.change(fn=pujar_imatge_procesar_capses_s3, inputs=imatge_entrada,
outputs=[text_sortida,imatge_resultant])
demo.launch()