diff --git a/package.json b/package.json index 951d2da..44b5143 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tfjs-image-node", - "version": "1.0.5", + "version": "1.0.6", "description": "A simple image classifier using tfjs and running in node.js", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/index.ts b/src/index.ts index c81757f..01e7728 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,19 +12,20 @@ type ResultType = { type ClassifyImageType = ( MODEL_DIR_PATH: string, - IMAGE_FILE_PATH: string, - METADATA: IMetadata + IMAGE_FILE_PATH: string ) => Promise; const classifyImage: ClassifyImageType = async ( MODEL_DIR_PATH: string, - IMAGE_FILE_PATH: string, - METADATA: IMetadata + IMAGE_FILE_PATH: string ) => { - if (!MODEL_DIR_PATH || !IMAGE_FILE_PATH || !METADATA) { + if (!MODEL_DIR_PATH || !IMAGE_FILE_PATH) { return new Error("MISSING_PARAMETER"); } + const res = await fetch(`${MODEL_DIR_PATH}/metadata.json`); + const METADATA: IMetadata = await res.json(); + let labels: string[] = METADATA["labels"]; const model = await tf.loadLayersModel(`${MODEL_DIR_PATH}/model.json`); diff --git a/test/classifyImage.test.ts b/test/classifyImage.test.ts index 1295267..379eb5b 100644 --- a/test/classifyImage.test.ts +++ b/test/classifyImage.test.ts @@ -2,7 +2,6 @@ import { describe } from "mocha"; import classifyImage from "../src/index"; import assert from "assert"; -const metadata = require("./testMetadata.json"); const model = "https://teachablemachine.withgoogle.com/models/jAIOHvmge"; const imageHand = "https://www.stgeorges.nhs.uk/wp-content/uploads/2014/03/hand-2.jpeg"; @@ -12,7 +11,7 @@ const imageNoHand = describe("classifyImage function", async () => { describe("returns", async () => { it("returns hand when shown a picture of a hand", async () => { - const result = await classifyImage(model, imageHand, metadata); + const result = await classifyImage(model, imageHand); if (result instanceof Error) { return new Error(); } else { @@ -21,7 +20,7 @@ describe("classifyImage function", async () => { }); it("returns 'No hand' when shown a picture not including hand", async () => { - const result = await classifyImage(model, imageNoHand, metadata); + const result = await classifyImage(model, imageNoHand); if (result instanceof Error) { return new Error(); @@ -31,7 +30,7 @@ describe("classifyImage function", async () => { }); it("returns a probability level", async () => { - const result = await classifyImage(model, imageNoHand, metadata); + const result = await classifyImage(model, imageNoHand); if (result instanceof Error) { return new Error(); } else { @@ -42,7 +41,7 @@ describe("classifyImage function", async () => { describe("Error boundries", async () => { it("returns an error when missing a parameter", async () => { //@ts-expect-error - const result = await classifyImage(imageNoHand, metadata); + const result = await classifyImage(imageNoHand); assert.ok(result instanceof Error); });