初次提交
This commit is contained in:
40
src/deploy/faceattr/gender-age.ts
Normal file
40
src/deploy/faceattr/gender-age.ts
Normal 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));
|
||||
}
|
||||
}
|
1
src/deploy/faceattr/index.ts
Normal file
1
src/deploy/faceattr/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export { GenderAge as GenderAgeDetector } from "./gender-age";
|
Reference in New Issue
Block a user