Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8 feature error missing parameters #9

Merged
merged 2 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.2",
"version": "1.0.3",
"description": "A simple image classifier using tfjs and running in node.js",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ type ClassifyImageType = (
MODEL_DIR_PATH: string,
IMAGE_FILE_PATH: string,
METADATA: IMetadata
) => Promise<ResultType[]>;
) => Promise<ResultType[] | Error>;

const classifyImage: ClassifyImageType = async (
MODEL_DIR_PATH: string,
IMAGE_FILE_PATH: string,
METADATA: IMetadata
) => {
let labels: string[] = METADATA.labels;
if (!MODEL_DIR_PATH || !IMAGE_FILE_PATH || !METADATA) {
return new Error("MISSING_PARAMETER");
}

let labels: string[] = METADATA["labels"];

const model = await tf.loadLayersModel(`${MODEL_DIR_PATH}/model.json`);

Expand Down
42 changes: 31 additions & 11 deletions test/classifyImage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,41 @@ const imageNoHand =
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Black_colour.jpg/640px-Black_colour.jpg";

describe("classifyImage function", async () => {
it("returns hand when shown a picture of a hand", async () => {
const result = await classifyImage(model, imageHand, metadata);
describe("returns", async () => {
it("returns hand when shown a picture of a hand", async () => {
const result = await classifyImage(model, imageHand, metadata);
if (result instanceof Error) {
return new Error();
} else {
assert.equal(result[0].label, "Hand");
}
});

assert.equal(result[0].label, "Hand");
});
it("returns 'No hand' when shown a picture not including hand", async () => {
const result = await classifyImage(model, imageNoHand, metadata);

it("returns 'No hand' when shown a picture not including hand", async () => {
const result = await classifyImage(model, imageNoHand, metadata);
if (result instanceof Error) {
return new Error();
} else {
assert.equal(result[0].label, "No hand");
}
});

assert.equal(result[0].label, "No hand");
it("returns a probability level", async () => {
const result = await classifyImage(model, imageNoHand, metadata);
if (result instanceof Error) {
return new Error();
} else {
assert.notEqual(result[0].probability, null);
}
});
});
describe("Error boundries", async () => {
it("returns an error when missing a parameter", async () => {
//@ts-expect-error
const result = await classifyImage(imageNoHand, metadata);

it("returns a probability level", async () => {
const result = await classifyImage(model, imageNoHand, metadata);

assert.notEqual(result[0].probability, null);
assert.ok(result instanceof Error);
});
});
});