Update README.md
Browse files
README.md
CHANGED
|
@@ -49,8 +49,35 @@ This is an ONNX version of [abhilash88/age-gender-prediction](https://huggingfac
|
|
| 49 |
## Usage with Transformers.js
|
| 50 |
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
---
|
| 56 |
|
|
|
|
| 49 |
## Usage with Transformers.js
|
| 50 |
|
| 51 |
|
| 52 |
+
```js
|
| 53 |
+
import { AutoModel, AutoProcessor, load_image } from "@huggingface/transformers";
|
| 54 |
+
|
| 55 |
+
// Load model and processor
|
| 56 |
+
const model_id = 'onnx-community/age-gender-prediction-ONNX';
|
| 57 |
+
const model = await AutoModel.from_pretrained(model_id);
|
| 58 |
+
const processor = await AutoProcessor.from_pretrained(model_id);
|
| 59 |
+
|
| 60 |
+
// Load and preprocess image
|
| 61 |
+
const image = await load_image('https://images.pexels.com/photos/614810/pexels-photo-614810.jpeg?w=720');
|
| 62 |
+
const inputs = await processor(image);
|
| 63 |
+
|
| 64 |
+
// Run model
|
| 65 |
+
const { logits } = await model(inputs);
|
| 66 |
+
|
| 67 |
+
// Extract age and gender
|
| 68 |
+
const [ageLogits, genderLogits] = logits.tolist()[0];
|
| 69 |
+
const age = Math.min(Math.max(Math.round(ageLogits), 0), 100);
|
| 70 |
+
const gender = genderLogits >= 0.5 ? 'Female' : 'Male';
|
| 71 |
+
const confidence = Math.max(genderLogits, 1 - genderLogits);
|
| 72 |
+
console.table({ age, gender, confidence });
|
| 73 |
+
// ββββββββββββββ¬βββββββββββββββββββββ
|
| 74 |
+
// β (index) β Values β
|
| 75 |
+
// ββββββββββββββΌβββββββββββββββββββββ€
|
| 76 |
+
// β age β 26 β
|
| 77 |
+
// β gender β 'Male' β
|
| 78 |
+
// β confidence β 0.9973341226577759 β
|
| 79 |
+
// ββββββββββββββ΄βββββββββββββββββββββ
|
| 80 |
+
```
|
| 81 |
|
| 82 |
---
|
| 83 |
|