@avatune/hair-length-predictor
Source:
packages/hair-length-predictor/README.md
Browser-based hair length prediction using TensorFlow.js. Classifies hair into 3 categories: short, medium, long.
Tiny model (~110KB) with blazingly fast loading and inference in the browser.
Installation
Section titled “Installation”npm install @avatune/hair-length-predictor @tensorflow/tfjsyarn add @avatune/hair-length-predictor @tensorflow/tfjspnpm add @avatune/hair-length-predictor @tensorflow/tfjsbun add @avatune/hair-length-predictor @tensorflow/tfjsimport { createHairLengthPredictor } from '@avatune/hair-length-predictor'
// Uses jsDelivr CDN by default - no setup required!const predictor = createHairLengthPredictor()await predictor.loadModel()
const result = await predictor.predictFromImage(imageElement)console.log(result)// {// length: 'medium',// confidence: 0.91,// probabilities: { short: 0.04, medium: 0.91, long: 0.05 },// faceDetected: true// }Model Files
Section titled “Model Files”By default, models are loaded from jsDelivr CDN (https://cdn.jsdelivr.net/npm/@avatune/hair-length-predictor@1.2.2/dist/model). No setup required!
Self-hosting (Optional)
Section titled “Self-hosting (Optional)”If you prefer to self-host the model files, copy them from dist/model/ to your public directory:
model.json- Model architecture and weights manifestclasses.json- Class labelsgroup1-shard1of1.bin- Model weights
Then pass the path to the predictor:
const predictor = createHairLengthPredictor('/models/hair-length')Setup with Vite (Optional)
Section titled “Setup with Vite (Optional)”import { copyFileSync, mkdirSync, readdirSync } from 'node:fs'import { join } from 'node:path'import { defineConfig } from 'vite'
export default defineConfig({ plugins: [ { name: 'copy-tfjs-models', buildStart() { const srcDir = join(__dirname, 'node_modules', '@avatune', 'hair-length-predictor', 'dist', 'model') const destDir = join(__dirname, 'public', 'models', 'hair-length')
mkdirSync(destDir, { recursive: true })
copyFileSync(join(srcDir, 'model.json'), join(destDir, 'model.json')) copyFileSync(join(srcDir, 'classes.json'), join(destDir, 'classes.json'))
const files = readdirSync(srcDir) for (const file of files) { if (file.endsWith('.bin')) { copyFileSync(join(srcDir, file), join(destDir, file)) } }
console.log('✓ Copied hair-length model to public/models') }, }, ],})Structure
Section titled “Structure”createHairLengthPredictor(modelDir?: string)Parameters:
modelDir(optional) - Path to directory containing model files. Defaults to jsDelivr CDN
Functions
Section titled “Functions”loadModel(): Promise<void>
Section titled “loadModel(): Promise<void>”Loads the TFJS model and class labels. Call this once before making predictions.
predict(imageTensor: tf.Tensor3D): Promise<HairLengthResult>
Section titled “predict(imageTensor: tf.Tensor3D): Promise<HairLengthResult>”Predicts hair length from an image tensor.
Parameters:
imageTensor- Normalized RGB image tensor [H, W, 3] with values in range [0, 1]
Returns:
{ length: string // Predicted class: 'short' | 'medium' | 'long' confidence: number // Confidence score [0, 1] probabilities: Record<string, number> // Scores for all classes}Model Details
Section titled “Model Details”- Architecture: MobileNetV2-based CNN
- Input: 128x128 RGB images
- Classes: 3 (short, medium, long)
- Training: CelebAMask-HQ dataset
- Accuracy: ~82%
- Model size: ~110KB (uint8 quantized)
- Format: TensorFlow.js with uint8 quantization
License
Section titled “License”See LICENSE.md for license information.