Skip to content

Commit

Permalink
feat: remove need for metadata.json
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinanielsen committed Nov 21, 2023
1 parent cfa794e commit 05cdcd7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ type ResultType = {

type ClassifyImageType = (
MODEL_DIR_PATH: string,
IMAGE_FILE_PATH: string,
METADATA: IMetadata
IMAGE_FILE_PATH: string
) => Promise<ResultType[] | Error>;

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`);
Expand Down
9 changes: 4 additions & 5 deletions test/classifyImage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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);
});
Expand Down

0 comments on commit 05cdcd7

Please sign in to comment.