Android Question Looking for Help with a Hairstyle AR App Idea

Mehrzad238

Active Member
I’m thinking of creating an app for barbers or hairstylists, and I’d love to include a feature where the user sees themselves live through the camera and can try on different hairstyles in real time — like a virtual hairstyle preview. I’m not sure if it’s even possible or a good idea, but it’s something I really want to do. Any guidance or help would mean a lot to me
 
  • Like
Reactions: stp

zed

Well-Known Member
Licensed User
B4X doesn't natively support computer vision or face tracking.
To detect a head, position, and adapt a hairstyle live, you need a facial recognition library like MediaPipe or OpenCV and a rendering engine to dynamically overlay images.

Services like aihairstyle.ai allow you to send a photo and receive a hairstyle preview.
You could integrate this into your app via an HTTP request and display the result.
 
Upvote 0

zed

Well-Known Member
Licensed User
A short example with OpenCV

To begin, you need to upload an image.
B4A:
Dim bmp As Bitmap = LoadBitmap(File.DirAssets, "face.jpg")
Dim mat As OCVMat = OCV.BitmapToMat(bmp)

For face detection, you need to load a Haar classifier (.xml file)
B4A:
Dim classifier As OCVCascadeClassifier
classifier.Initialize(File.DirAssets, "haarcascade_frontalface_default.xml")
Dim faces As List = classifier.DetectMultiScale(mat, 1.1, 3, 0, OCV.Size(30, 30), OCV.Size(300, 300))
The haarcascade_frontalface_default.xml file is a pre-trained classifier used by OpenCV to detect faces in an image.
It contains a series of mathematical rules based on the Haar Cascade algorithm, which identifies characteristic facial features (eyes, nose, mouth, etc.).

Finally, overlay an image (hairstyle) and display the result.
B4A:
For Each r As OCVRect In faces
    ' Load hairstyle image
    Dim hairBmp As Bitmap = LoadBitmap(File.DirAssets, "hair.png")
    
    ' Resize according to face size
    Dim scaledHair As Bitmap = hairBmp.Resize(r.Width, r.Height, True)
    
    ' Overlay on face
    OCV.OverlayBitmap(mat, scaledHair, r.X, r.Y - r.Height / 2) ' positionner au-dessus
Next

Dim resultBmp As Bitmap = OCV.MatToBitmap(mat)
ImageView1.Bitmap = resultBmp

It's something like that. But creating a complete application will require a lot of work.
 
Upvote 0
Top