On the backs of the ElasticOCR launch event at SharePoint Conference North America, I found myself with dozens of photos taken by the team that all needed to be sized and cropped. Previously I would have simply spun up Photoshop and created a quick batch solution to accomplish this, but this scenario had some challenges. We had specifically taken photos of people at our booth, and I wanted to attempt to automatically crop the photos around people as to somewhat normalize the photos without manually cropping all of them. Enter Azure Cognitive Services, and the start of CogServe-PS a new project that will become a holistic library for interacting with the Cognitive Services portfolio via PowerShell.
As you can see below, our photos were somewhat consistent, but the person wasn't necessarily perfectly centered, people are obviously different heights, and they were all holding their drawing in different ways.
After using my new CogServe-PS library, I now have 500 x 500 pixel square photos consistently cropped to feature the person in the photo.
My CogServe-PS library is available on GitHub, and version 0.1 includes the operations necessary to replicate what I did here, both with and without the smart cropping. You'll simply need an Azure subscription (or a free trial) with Computer Vision deployed.
Version 0.1 supports the generation of a thumbnail from a single image, or the generation of thumbnails for every image in a specified folder. Note that there is a 4mb limit on images sent to the Computer Vision API, and version 0.1 does not automatically resize the source image. In a future update I'll add that support, but for now images larger than 4mb are simply ignored.
To replicate what I did above, simply download CogServe-PS.Helpers.psm1 and execute the following commands:
Import-Module .\CogServe-PS.Helpers.psm1 Set-CogServePSConfig -VisionKey xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Set-CogServePSConfig -VisionRegion southcentralus Request-CogServePSThumbnails -FilePath .\images -Target .\Thumbnails -Width 500 -Height 500 -SmartCropping