Package: profile-photo
Profile PhotoCenter+CropImage to create a Profile Pic orHeadshot.If this project has helped you, please consider making adonation.InstallLocal tests:The[all]extrainstallsboto3, which is excluded by default - this assumes an AWS
environment.$pipinstallprofile-photo[all]AWS Lambdadeployment:$pipinstallprofile-photoFeaturesExports a helper function,create_headshot,
to create a
close-up or headshot of the primary face in a photo or image.LeveragesAmazon
Rekognitionto detect bounding boxes of a person'sFaceand relevant Labels,
such asPerson.Exposes helper methods to save the result image (cropped) as well
as API responses to a local folder.UsageBasic usage, with asample
image:fromurllib.requestimporturlopenfromprofile_photoimportcreate_headshot# Set the $AWS_PROFILE environment variable insteadaws_profile='my-profile'im_url='https://raw.githubusercontent.com/rnag/profile-photo/main/examples/woman-2.jpeg'im_bytes=urlopen(im_url).read()photo=create_headshot(im_bytes,profile=aws_profile)photo.show()An example with a local image, and saving the result image and API
responses to a folder:from__future__importannotationsfromprofile_photoimportcreate_headshot# customize local file location for API responsesdefget_filename(file_name:str|None,api:str):returnf'responses/{file_name}_{api}.json'photo=create_headshot('/path/to/image')# this saves image and API responses to a results/ folder# can also be achieved by passing `output_dir` abovephoto.save_all('results',get_response_filename=get_filename)# display before-and-after imagesphoto.show()Lastly, an example with an image on S3, and passing in cachedRekognition
APIresponses for the image:frompathlibimportPathfromprofile_photoimportcreate_headshots3_image_path=Path('path/to/image.jpg')responses_dir=Path('./my/responses')_photo=create_headshot(bucket='my-bucket',key=str(s3_image_path),profile='my-profile',faces=responses_dir/f'{s3_image_path.stem}_DetectFaces.json',labels=responses_dir/f'{s3_image_path.stem}_DetectLabels.json',debug=True)ExamplesCheck outexample
imageson
GitHub for sample use cases and results.How It WorksThis library currently makes calls to theAmazon
RekognitionAPIs to detect bounding boxes on a Face and Person in a photo.It then uses custom, in-house logic to determine the X/Y coordinates for
cropping. This mainly involves "blowing up" or enlarging the Face
bounding box, but then correcting the coordinates as needed by the
Person box. This logic has been fine-tuned based on what I have found
provide the best overall results for generic images (not necessary
profile photos).In the future, other ideas other thanRekognitionmight be considered
-- such as existing machine learning approaches or even a solution with
theopencvlibrary in Python alone.Future IdeasSupport background removal withrembg.Investigate other (alternate) approaches toRekognitionfor
detecting a face and person in a photo.CreditsThis package was created withCookiecutterand thernag/cookiecutter-pypackageproject template.Buy me a coffeeLiked some of my work? Buy me a coffee (or more likely a beer)LicenseCopyright (c) 2023-presentRitvik NagLicensed underMIT LicenseHistory0.1.3 (2023-03-13)Update documentation to include sponsor links, and complete install instructions.0.1.2 (2023-03-09)Embed images in documentation.Convert.rstfiles to.mdformat.0.1.1 (2023-03-08)Update documentation.0.1.0 (2023-03-08)First release on PyPI.