|
import gradio as gr |
|
from PIL import Image, ImageDraw, ImageFont |
|
from io import BytesIO |
|
import boto3 |
|
|
|
aws_access_key_id='ASIATKZWHQXRSXHN7GJX' |
|
aws_secret_access_key='ValLFfUJHu/Tb9ygGlzlNn29Dv0333EZSJjOkh4A' |
|
aws_session_token='IQoJb3JpZ2luX2VjEP7//////////wEaCXVzLXdlc3QtMiJGMEQCIHUn9y9RpIvkjEfwdOelaLHM/xdySN/18vIpME7kubHDAiANeoeHqulNlqTJbq1MyJRTbAcc7hdo7c7WIZwg+wrTfiqvAghHEAAaDDIyOTM1NzYxODY1OSIMp8Rm1KUmI0jQ6Ft9KowCH+QpjftlWQzdjQMp2SFysiMSD7VECr3YzGFd4gPueUilMO2S4VpNPRLSyMXu/yKLDQ3CPzAL4CpJ7VE9JMKRK3uO6wZJhjx5C2QipIQnnsfgM8CRyEt8KvICNtAJSYMYGOJzd5LcVJT1ma+vbgNQa+ZqpHs8VZWzPWRxtKf2qs09A9y1qCg1c1hy0wIhlf4Nz5JJrTlO0EGgOaB92vidOc3j8yy19gn1tkcrrzbFFv5J/W6Rgsiq+k1QWSRD7uK6wUbMoYjm6lyf4LjDyIiVBlxGUNOl9jXk+YSRYJojTDiUKO7/aIJLjHA5tnzehEW+81oixhAdd/fnjjsSGoDU7tDqemThf0M/lnYoZDDDmuC3BjqeAeqEwOs+3jQN1KRiChGC+Ge67JKWNBNk7qfEYjwmfYCgLsuY2cBYDF5b5QRWh0XpyT4rm2382bEUXuEKVmPFDNac6zpFeXxsWvWpC+9vpFn4QA2HH80JEb1Wd3lqkYmZd74fd4yB4wR06cLejc+rDNZhKbSAMfF0QB5VufgQ8N/FH54MIo3VJCUG11YPNd/qC7ghrj5JSHv+9bVLJlP1' |
|
|
|
BUCKET_NAME = "ioc1980" |
|
nom_arxiu = 'imatge.jpg' |
|
|
|
s3 = 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='us-east-1' |
|
) |
|
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='us-east-1' |
|
) |
|
|
|
def analitzar_imatge(imatge): |
|
|
|
s3.upload_file(imatge, BUCKET_NAME, nom_arxiu) |
|
|
|
|
|
response = rekognition.detect_faces( |
|
Image={ |
|
'S3Object':{ |
|
'Bucket':BUCKET_NAME, |
|
'Name':nom_arxiu |
|
} |
|
}, |
|
Attributes=['ALL']) |
|
|
|
|
|
image_file = s3.get_object(Bucket=BUCKET_NAME, Key=nom_arxiu) |
|
image_bytes = image_file['Body'].read() |
|
image = Image.open(BytesIO(image_bytes)) |
|
|
|
|
|
draw = ImageDraw.Draw(image) |
|
|
|
|
|
resultats = [] |
|
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" G猫nere: {face['Gender']['Value']}") |
|
resultats.append(f" Emoci贸: {max(face['Emotions'], key=lambda x: x['Confidence'])['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) |
|
|
|
|
|
draw.rectangle([left, top, left + width, top + height], outline="red", width=2) |
|
draw.text((left, top - 25), f"Cara {i}", fill="red") |
|
|
|
|
|
s3.delete_object(Bucket=BUCKET_NAME, Key=nom_arxiu) |
|
|
|
return "\n".join(resultats) if resultats else "No s'han detectat cares a la imatge.", image |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# An脿lisi de cares amb Amazon Rekognition") |
|
|
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
imatge_input = gr.Image(type='filepath', label="Puja una imatge") |
|
with gr.Column(): |
|
resultats_text = gr.Textbox(label="Resultats") |
|
|
|
|
|
imatge_output = gr.Image(label="Imatge processada") |
|
|
|
|
|
imatge_input.change(fn=analitzar_imatge, |
|
inputs=imatge_input, |
|
outputs=[resultats_text, imatge_output]) |
|
|
|
|
|
demo.launch() |