UNPKG

emoji-net

Version:

EmojiNet is an image to emoji recognizer based on MobileNet / Google Emoji Scavenger Hunt

98 lines 4.13 kB
#!/usr/bin/env ts-node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (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 = {}; if (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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty * * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and * Wechaty Contributors <https://github.com/wechaty>. * * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ const path_1 = __importDefault(require("path")); const tstest_1 = require("tstest"); const tfc = __importStar(require("@tensorflow/tfjs-core")); const canvas_utils_1 = require("./canvas-utils"); const emoji_net_1 = require("./emoji-net"); const FIXTURE_IMAGE_LABEL_LIST = [ { file: path_1.default.join(__dirname, '../tests/fixtures/hand.jpg'), name: 'hand', }, { file: path_1.default.join(__dirname, '../tests/fixtures/sofa.jpg'), name: 'sofa', }, ]; tstest_1.test('EmojiNet smoke testing', async (t) => { const emojinet = new emoji_net_1.EmojiNet(); await emojinet.load(); const predictItemList = await Promise.all(FIXTURE_IMAGE_LABEL_LIST .map(x => x.file) .map(file => emojinet.recognize(file))); const emojiList = predictItemList.map(x => x[0].name); const EXPECTED_LIST = FIXTURE_IMAGE_LABEL_LIST.map(x => x.name); t.same(emojiList, EXPECTED_LIST, 'should get the labels right'); await emojinet.dispose(); }); tstest_1.test('EmojiNet predict() & getTopKClasses()', async (t) => { class EmojiNetTest extends emoji_net_1.EmojiNet { predict(input) { return super.predict(input); } getTopKClasses(predictions, topK) { return super.getTopKClasses(predictions, topK); } } const emojinet = new EmojiNetTest(); await emojinet.load(); for (const { file, name } of FIXTURE_IMAGE_LABEL_LIST) { const image = await canvas_utils_1.loadImage(file); const resizedImage = await canvas_utils_1.resizeImage(image, emoji_net_1.MOBILENET_SIZE, emoji_net_1.MOBILENET_SIZE); const canvas = await canvas_utils_1.createCanvas(emoji_net_1.MOBILENET_SIZE, emoji_net_1.MOBILENET_SIZE); const ctx = canvas.getContext('2d'); if (!ctx) { throw new Error('no ctx'); } ctx.putImageData(resizedImage, 0, 0); const pixels = tfc.fromPixels(canvas); const result = await emojinet.predict(pixels); const top2 = emojinet.getTopKClasses(result, 2); t.equal(top2[0].name, name, 'should get the right lable: ' + name); } await emojinet.dispose(); }); //# sourceMappingURL=emoji-net.spec.js.map