georg-jung / FaceAiSharp

State-of-the-art face detection and face recognition for .NET.
https://nuget.org/packages/FaceAiSharp/
MIT License
60 stars 9 forks source link

Image profile crop example #24

Closed johan-developer closed 11 months ago

johan-developer commented 11 months ago

Hi Georg,

Your project it's really good. Congratz.

Do you have any example of cropping image for profile I saw your page but didn't see the code of this example.

Thank you,

Johan

georg-jung commented 11 months ago

Thanks for your positive words! With the FaceAiSharp.Bundle package, cropping a profile picture works like this:

using FaceAiSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

// adjust these lines to load your own image
using var hc = new HttpClient();
var groupPhoto = await hc.GetByteArrayAsync(
    "https://raw.githubusercontent.com/georg-jung/FaceAiSharp/master/examples/obama_family.jpg");
var img = Image.Load<Rgb24>(groupPhoto);

var det = FaceAiSharpBundleFactory.CreateFaceDetector();
FaceAiSharp.Applications.CropProfilePicture(det, img);
img.Save("obama_family_profilepic.jpg");

For more context see also this StackOverflow answer.

Additionally, you can always take a look at the code of my tutorial project, in this case specifically here: https://github.com/georg-jung/explain-face-rec/blob/fe8a4e5d783b2a8eb22c33f8fcb9c8d3a89665eb/src/BlazorFace/Pages/Applications/ProfilePicture.razor#L54

If you have any further questions, feel free to ask or otherwise close this issue.