BLOG • DIGITAL INSIGHTS
Sitefinity's default thumbnail generator can crop out faces. This post shows how to use OpenCV and computer vision to build a custom Sitefinity image processor that detects faces and centres the crop around them automatically.
Sitefinity has a nice feature to auto-generate thumbnails. Although it works well most of the time, there are cases where it falls short - for example, when there are people in the image, the default cropping can cut off heads and faces. To illustrate, here is a photo of part of our team.
Original image:
Default Sitefinity thumbnail crop:
You can see how the heads at the top of the photo are cropped. We could simply resize without cropping, but that gives a 533x300 thumbnail with smaller faces - not ideal when the goal is to focus on the festive smiles from our Christmas photo. The answer is Computer Vision.
If you are not familiar with these fields, here is a quick overview:
We will use the Quantum sample project, available on GitHub with setup instructions: github.com/Sitefinity/Telerik.Sitefinity.Samples.Quantum
For computer vision we will use the popular OpenCV library, which has a .NET wrapper:
Install the required NuGet packages:
Install-Package OpenCvSharp4
Install-Package OpenCvSharp4.runtime.win
We will create a custom Image Processor for Sitefinity. For more details see the Sitefinity Image Processing API documentation.
Once the class is implemented, register it on Bootstrap in Global.asax.cs:
protected void Application_Start(object sender, EventArgs e)
{
Bootstrapper.Initialized += Bootstrapper_Initialized;
}
void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
if (e.CommandName == "Bootstrapped")
{
ObjectFactory.Container.RegisterType<IImageProcessor, FaceRecognitionImageProcessor>(
new ContainerControlledLifetimeManager());
}
}
To configure a thumbnail profile using the custom processor:
Here is the thumbnail generated by our custom image processor - all faces are visible and centred:
And here are thumbnails from the built-in Sitefinity processor at the same 600x300 size - cropped and resized - for comparison:
Note: Keep in mind this is an example of how Computer Vision can be used for better image manipulation. You may need to fine-tune the code or even train a custom model to better suit your specific use case.
Explore more insights and case studies from our team.