初次提交

This commit is contained in:
2025-03-06 14:38:32 +08:00
commit b0e71d0ebb
46 changed files with 1926 additions and 0 deletions

View File

@ -0,0 +1,40 @@
import { cv } from "../../cv";
import { ImageCropOption, ImageSource, Model } from "../common/model";
import { convertImage } from "../common/processors";
interface GenderAgePredictOption extends ImageCropOption {
}
export interface GenderAgePredictResult {
gender: "M" | "F"
age: number
}
export class GenderAge extends Model {
private async doPredict(image: cv.Mat, option?: GenderAgePredictOption): Promise<GenderAgePredictResult> {
const input = this.input;
const output = this.output;
if (option?.crop) image = image.crop(option.crop.sx, option.crop.sy, option.crop.sw, option.crop.sh);
image = image.resize(input.shape[3], input.shape[2]);
const nchwImage = convertImage(image.data, { sourceImageFormat: "bgr", targetColorFormat: "rgb", targetShapeFormat: "nchw", targetNormalize: { mean: [0], std: [1] } });
const result = await this.session.run({
[input.name]: {
shape: [1, 3, input.shape[2], input.shape[3]],
data: nchwImage,
type: "float32",
}
}).then(res => res[output.name]);
return {
gender: result[0] > result[1] ? "F" : "M",
age: parseInt(result[2] * 100 as any),
}
}
public predict(image: ImageSource, option?: GenderAgePredictOption) {
return Model.resolveImage(image, im => this.doPredict(im, option));
}
}

View File

@ -0,0 +1 @@
export { GenderAge as GenderAgeDetector } from "./gender-age";