All files / node-efficientnet/dist index.js

49.55% Statements 55/111
71.43% Branches 35/49
44.12% Functions 15/34
50.52% Lines 49/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149  1x 69x 69x         1x 1x       1x 2x 1x 79x 1x 1x   1x 2x 2x 2x   4x 2x     1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x     1x 1x   1x 1x 1x 1x                 1x                               1x     1x 1x     1x 1x 1x                                                                                                             1x  
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    Eif (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    Eif (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EfficientnetResult = exports.EfficientnetModel = exports.EfficientnetCheckPoint = exports.EfficientnetCheckPointFactory = void 0;
const tfnode = __importStar(require("@tensorflow/tfjs-node"));
const Jimp = __importStar(require("jimp"));
const labels_map_json_1 = __importDefault(require("./misc/labels_map.json"));
const NUM_OF_CHANNELS = 3;
var EfficientnetCheckPoint;
(function (EfficientnetCheckPoint) {
    EfficientnetCheckPoint[EfficientnetCheckPoint["B0"] = 0] = "B0";
    EfficientnetCheckPoint[EfficientnetCheckPoint["B1"] = 1] = "B1";
    EfficientnetCheckPoint[EfficientnetCheckPoint["B2"] = 2] = "B2";
    EfficientnetCheckPoint[EfficientnetCheckPoint["B3"] = 3] = "B3";
    EfficientnetCheckPoint[EfficientnetCheckPoint["B4"] = 4] = "B4";
    EfficientnetCheckPoint[EfficientnetCheckPoint["B5"] = 5] = "B5";
    EfficientnetCheckPoint[EfficientnetCheckPoint["B6"] = 6] = "B6";
    EfficientnetCheckPoint[EfficientnetCheckPoint["B7"] = 7] = "B7";
})(EfficientnetCheckPoint || (EfficientnetCheckPoint = {}));
exports.EfficientnetCheckPoint = EfficientnetCheckPoint;
class EfficientnetCheckPointFactory {
    static create(checkPoint) {
        return __awaiter(this, void 0, void 0, function* () {
            switch (checkPoint) {
                case EfficientnetCheckPoint.B0: {
                    const modelPath = "https://raw.githubusercontent.com/ntedgi/efficientnet/main/lib/tfjs/web_model/model.json";
                    const model = new EfficientnetModel(modelPath, 244);
                    yield model.load();
                    return model;
                }
                default: {
                    throw Error(`${checkPoint} - Not Implemented Yet!`);
                }
            }
        });
    }
}
exports.EfficientnetCheckPointFactory = EfficientnetCheckPointFactory;
class EfficientnetResult {
    constructor(values) {
        this.result = [];
        const arr = Array.from(values);
        const topValues = values.sort((a, b) => b - a).slice(0, 3);
        const indexes = topValues.map((e) => arr.indexOf(e));
        const sum = topValues.reduce((a, b) => {
            return a + b;
        }, 0);
        indexes.forEach((value, index) => {
            // @ts-ignore
            this.result.push({ label: labels_map_json_1.default[value], precision: topValues[index] / sum * 100 });
        });
    }
}
exports.EfficientnetResult = EfficientnetResult;
class EfficientnetModel {
    constructor(modelPath, imageSize) {
        this.modelPath = modelPath;
        this.imageSize = imageSize;
    }
    load() {
        return __awaiter(this, void 0, void 0, function* () {
            const model = yield tfnode.loadGraphModel(this.modelPath);
            this.model = model;
        });
    }
    createTensor(image) {
        return __awaiter(this, void 0, void 0, function* () {
            let values = new Float32Array(this.imageSize * this.imageSize * NUM_OF_CHANNELS);
            let i = 0;
            image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
                const pixel = Jimp.intToRGBA(image.getPixelColor(x, y));
                pixel.r = ((pixel.r - 1) / 127.0) >> 0;
                pixel.g = ((pixel.g - 1) / 127.0) >> 0;
                pixel.b = ((pixel.b - 1) / 127.0) >> 0;
                values[i * NUM_OF_CHANNELS + 0] = pixel.r;
                values[i * NUM_OF_CHANNELS + 1] = pixel.g;
                values[i * NUM_OF_CHANNELS + 2] = pixel.b;
                i++;
            });
            const outShape = [this.imageSize, this.imageSize, NUM_OF_CHANNELS];
            // @ts-ignore
            let imageTensor = tfnode.tensor3d(values, outShape, "float32");
            imageTensor = imageTensor.expandDims(0);
            return imageTensor;
        });
    }
    cropAndResize(image) {
        return __awaiter(this, void 0, void 0, function* () {
            const width = image.bitmap.width;
            const height = image.bitmap.height;
            const cropPadding = 32;
            const paddedCenterCropSize = ((this.imageSize / (this.imageSize + cropPadding)) *
                Math.min(height, width)) >>
                0;
            const offsetHeight = ((height - paddedCenterCropSize + 1) / 2) >> 0;
            const offsetWidth = (((width - paddedCenterCropSize + 1) / 2) >> 0) + 1;
            yield image.crop(offsetWidth, offsetHeight, paddedCenterCropSize, paddedCenterCropSize);
            yield image.resize(this.imageSize, this.imageSize, Jimp.RESIZE_BICUBIC);
            return image;
        });
    }
    predict(tensor) {
        return __awaiter(this, void 0, void 0, function* () {
            const objectArray = yield this.model.predict(tensor);
            const values = objectArray.dataSync();
            return new EfficientnetResult(values);
        });
    }
    inference(imgPath) {
        return __awaiter(this, void 0, void 0, function* () {
            let image = yield Jimp.read(imgPath);
            image = yield this.cropAndResize(image);
            const tensor = yield this.createTensor(image);
            return this.predict(tensor);
        });
    }
}
exports.EfficientnetModel = EfficientnetModel;