profile_photo.utils.aws package

Submodules

profile_photo.utils.aws.client_cache module

class profile_photo.utils.aws.client_cache.ClientCache(region_name: 'str' = 'us-east-1', profile_name: 'str | None' = None, init_client: 'InitVar[bool]' = False, max_pool_connections: 'int | None' = None)[source]

Bases: object

SERVICE_NAME: ClassVar[str] = None
THREAD_SAFE: ClassVar[bool] = False
property client
init_client: InitVar[bool] = False
max_pool_connections: int | None = None
profile_name: str | None = None
region_name: str = 'us-east-1'

profile_photo.utils.aws.rekognition module

class profile_photo.utils.aws.rekognition.Rekognition(region_name: 'str' = 'us-east-1', profile_name: 'str | None' = None, init_client: 'InitVar[bool]' = False, max_pool_connections: 'int | None' = None)[source]

Bases: ClientCache

SERVICE_NAME: ClassVar[str] = 'rekognition'
detect_faces(bucket: str, key: str, im_bytes: bytes | None = None, debug=False, all_attrs=True)[source]

Call the DetectFaces API on an image, or use the DynamoDB cache if needed.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rekognition.html#Rekognition.Client.detect_faces

detect_labels(bucket: str, key: str, im_bytes: bytes | None = None, debug=False, confidence=55)[source]

Call the DetectLabels API on an image, or use the DynamoDB cache if needed.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rekognition.html#Rekognition.Client.detect_labels

recognize_celebrities(bucket: str, key: str, im_bytes: bytes | None = None)[source]

Call the RecognizeCelebrities API on an image, or use the DynamoDB cache if needed.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rekognition.html#Rekognition.Client.recognize_celebrities

profile_photo.utils.aws.rekognition_models module

class profile_photo.utils.aws.rekognition_models.BoundingBox(width: float, height: float, left: float, top: float)[source]

Bases: object

BoundingBox dataclass

height: float
left: float
top: float
width: float
class profile_photo.utils.aws.rekognition_models.CompareFacesResp(source_image_face: SourceImageFace, face_matches: List[FaceMatch], unmatched_faces: List[UnmatchedFace])[source]

Bases: JSONSerializable

Response from the Rekognition CompareFaces API

face_matches: List[FaceMatch]
source_image_face: SourceImageFace
unmatched_faces: List[UnmatchedFace]
class profile_photo.utils.aws.rekognition_models.Coordinates(x1: int, y1: int, x2: int, y2: int)[source]

Bases: object

Coordinates dataclass.

Can be visualized using this diagram:

x1,y1 —————– | | | | | | | | | | —————— x2,y2

classmethod from_box(im, box: BoundingBox, offset=0, y_offset=0)[source]
x1: int
x2: int
y1: int
y2: int
class profile_photo.utils.aws.rekognition_models.DetectFacesResp(face_details: List[FaceDetail])[source]

Bases: JSONSerializable

Response from the Rekognition DetectFaces API

face_details: List[FaceDetail]
get_face() FaceDetail | None[source]

Return the primary face that is detected, if available.

class profile_photo.utils.aws.rekognition_models.DetectLabelsResp(labels: List[Label])[source]

Bases: JSONSerializable

Response from Rekognition DetectLabels API

get_person_box(face: FaceDetail | None) BoundingBox | None[source]

Get the bounding box for the ‘Person’ label.

If there are multiple instances of a ‘Person’, then the argument for face helps pinpoint a matching person.

property label_name_to_value: DictWithLowerStore[str, Label]

Return a case-insensitive mapping of label name to label.

labels: List[Label]
property person_boxes: list[BoundingBox] | None
class profile_photo.utils.aws.rekognition_models.Face(bounding_box: BoundingBox, confidence: float, landmarks: List[Landmark], pose: Pose, quality: Quality)[source]

Bases: object

Face dataclass

bounding_box: BoundingBox
confidence: float
landmarks: List[Landmark]
pose: Pose
quality: Quality
class profile_photo.utils.aws.rekognition_models.FaceDetail(bounding_box: BoundingBox, age_range: AgeRange, smile: Smile, eyeglasses: Eyeglasses, sunglasses: Sunglasses, gender: Gender, beard: Beard, mustache: Mustache, eyes_open: EyesOpen, mouth_open: MouthOpen, emotions: List[Emotion], landmarks: List[Landmark], pose: Pose, quality: Quality, confidence: float)[source]

Bases: object

FaceDetail dataclass

age_range: AgeRange
beard: Beard
bounding_box: BoundingBox
confidence: float
property emotion_to_confidence: DictWithLowerStore[str, float]

Return a case-insensitive mapping of emotion name to its confidence.

emotions: List[Emotion]
eyeglasses: Eyeglasses
eyes_open: EyesOpen
gender: Gender
landmarks: List[Landmark]
mouth_open: MouthOpen
mustache: Mustache
pose: Pose
quality: Quality
smile: Smile
sunglasses: Sunglasses
class profile_photo.utils.aws.rekognition_models.Label(name: str, confidence: float, instances: List[Instance], parents: List[Parent])[source]

Bases: object

Label dataclass

confidence: float
instances: List[Instance]
name: str
parents: List[Parent]
class profile_photo.utils.aws.rekognition_models.Landmark(type: str, x: float, y: float)[source]

Bases: object

Landmark dataclass

type: str
x: float
y: float
class profile_photo.utils.aws.rekognition_models.RecognizeCelebritiesResp(celebrity_faces: List[CelebrityFace], unrecognized_faces: List[Face], orientation_correction: Literal['ROTATE_0', 'ROTATE_90', 'ROTATE_180', 'ROTATE_270'] = 'ROTATE_0')[source]

Bases: JSONSerializable

Response from the Rekognition RecognizeCelebrities API

celebrity_faces: List[CelebrityFace]
orientation_correction: Literal['ROTATE_0', 'ROTATE_90', 'ROTATE_180', 'ROTATE_270'] = 'ROTATE_0'
unrecognized_faces: List[Face]

profile_photo.utils.aws.rekognition_utils module

class profile_photo.utils.aws.rekognition_utils.FillColor(value)[source]

Bases: Enum

An enumeration.

BLACK = '#000000'
BLUE = '#0000ff'
GREEN = '#00d400'
RED = '#ff0000'
WHITE = '#FFFFFF'
YELLOW = '#ffff00'
property bgr

Returns the BGR representation of the color.

profile_photo.utils.aws.rekognition_utils.best_fit_coordinates(im, face_box: BoundingBox, *boxes: BoundingBox | None, fit=0.2857142857142857, x_offset=0.17, y_offset=0.17, constrain_width=True) Coordinates[source]
profile_photo.utils.aws.rekognition_utils.draw_box(image: Image, box: BoundingBox, color: FillColor = FillColor.GREEN, line_width=3, offset=0)[source]

Draw a bounding box on the image at the specified coordinates box and with the specified border color and line_width. An optional offset can be provided to change box coordinates. For example, an offset of line_width + line_width can be used to create an “inner” bounding box.

Displays the image after the box has been drawn.

Ref: https://docs.aws.amazon.com/rekognition/latest/dg/images-displaying-bounding-boxes.html

Usage:

>>> im = Image.open('path/to/file.jpeg')
>>> draw_box(im, data['FaceDetails'][0]['BoundingBox'], FillColor.YELLOW)
profile_photo.utils.aws.rekognition_utils.draw_circle(im, landmark: Landmark, color: FillColor = FillColor.YELLOW, radius: int = 3, filled=True, font_scale=0.4, font_face=4, text: str | None = None, text_color: FillColor = FillColor.WHITE)[source]

Draw a (filled) circle on the image centered around the specified (x, y) coordinates for landmark and with the specified border color and radius radius.

Ref: https://www.pyimagesearch.com/2021/01/27/drawing-with-opencv/

Usage:

>>> img = cv.imread('path/to/file.jpeg')
>>> draw_circle(img, data['FaceDetails'][0]['Landmarks'][0],                         FillColor.YELLOW, 3, False)
profile_photo.utils.aws.rekognition_utils.draw_rectangle(im, coords_or_box: Coordinates | BoundingBox, color: FillColor = FillColor.GREEN, offset=0, y_offset=0, line_width=3)[source]

Draw a bounding box on the image at the specified coordinates box and with the specified border color and line_width.

An optional offset can be provided to enhance or “zoom out” on the box coordinates.

Ref: https://stackoverflow.com/questions/23720875/how-to-draw-a-rectangle-around-a-region-of-interest-in-python

Usage:

>>> img = cv.imread('path/to/file.jpeg')
>>> draw_rectangle(img, data['FaceDetails'][0]['BoundingBox'], FillColor.YELLOW)
profile_photo.utils.aws.rekognition_utils.show_image(name, img, area=0.5, window_h=0, window_w=0)[source]

Displays an image after resizing it to the specified dimensions, while also ensuring that we retain its aspect ratio.

Credits: https://stackoverflow.com/a/67718163/10237506

profile_photo.utils.aws.s3 module

class profile_photo.utils.aws.s3.S3Helper(region_name='us-east-1', profile_name=None, access_key: str | None = None, secret_key: str | None = None, use_sig_v4=False, init_client=False, max_pool_connections=None)[source]

Bases: ClientCache

Helper class for interacting with the boto3 S3 client.

SERVICE_NAME: ClassVar[str] = 's3'
get_object_bytes(bucket, key) bytes[source]

Retrieve an object (raw bytes) from S3.

Module contents