-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.py
64 lines (48 loc) · 2.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from pathlib import Path
import boto3
from mypy_boto3_rekognition.type_defs import (
CelebrityTypeDef,
RecognizeCelebritiesResponseTypeDef,
)
from PIL import Image, ImageDraw, ImageFont
client = boto3.client("rekognition")
def get_path(file_name: str) -> str:
return str(Path(__file__).parent / "images" / file_name)
def recognize_celebrities(photo: str) -> RecognizeCelebritiesResponseTypeDef:
with open(photo, "rb") as image:
return client.recognize_celebrities(Image={"Bytes": image.read()})
def draw_boxes(image_path: str, output_path: str, face_details: list[CelebrityTypeDef]):
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("Ubuntu-R.ttf", 20)
width, height = image.size
for face in face_details:
box = face["Face"]["BoundingBox"] # type: ignore
left = int(box["Left"] * width) # type: ignore
top = int(box["Top"] * height) # type: ignore
right = int((box["Left"] + box["Width"]) * width) # type: ignore
bottom = int((box["Top"] + box["Height"]) * height) # type: ignore
confidence = face.get("MatchConfidence", 0)
if confidence > 90:
draw.rectangle([left, top, right, bottom], outline="red", width=3)
text = face.get("Name", "")
position = (left, top - 20)
bbox = draw.textbbox(position, text, font=font)
draw.rectangle(bbox, fill="red")
draw.text(position, text, font=font, fill="white")
image.save(output_path)
print(f"Imagem salva com resultados em : {output_path}")
if __name__ == "__main__":
photo_paths = [
get_path("bbc.jpg"),
get_path("msn.jpg"),
get_path("neymar-torcedores.jpg"),
]
for photo_path in photo_paths:
response = recognize_celebrities(photo_path)
faces = response["CelebrityFaces"]
if not faces:
print(f"Não foram encontrados famosos para a imagem: {photo_path}")
continue
output_path = get_path(f"{Path(photo_path).stem}-resultado.jpg")
draw_boxes(photo_path, output_path, faces)