-
Notifications
You must be signed in to change notification settings - Fork 29
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
Set up testing #82
Merged
Merged
Set up testing #82
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f0053d
add jest test scripts
ziyuan-linn 0572559
setup jest
ziyuan-linn 9e2aec6
Merge remote-tracking branch 'upstream/main' into setup-testing-envir…
lindapaiste 7aa8cf8
Merge remote-tracking branch 'upstream/jest-test-scripts' into setup-…
lindapaiste 62b56f5
Passing tests for BodyPix
lindapaiste 9cc883d
Merge remote-tracking branch 'upstream/main' into feat/testing
lindapaiste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* For a detailed explanation regarding each configuration property, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
/** @type {import('jest').Config} */ | ||
const config = { | ||
collectCoverage: true, | ||
coverageDirectory: "coverage", | ||
coverageProvider: "v8", | ||
globalSetup: "./setupTests.js", | ||
passWithNoTests: true, | ||
testEnvironment: "jsdom", | ||
testEnvironmentOptions: { | ||
resources: "usable", // Load image resources | ||
}, | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { ImageData } = require("canvas"); | ||
import '@tensorflow/tfjs-node'; // loads the tensorflow/node backend to the registry | ||
import crossFetch from 'cross-fetch'; | ||
import * as tf from '@tensorflow/tfjs'; | ||
|
||
async function setupTests() { | ||
|
||
console.log("Beginning setup"); | ||
|
||
await tf.setBackend('tensorflow'); | ||
tf.env().set('IS_BROWSER', false); | ||
tf.env().set('IS_NODE', true); | ||
|
||
// Use the node-canvas ImageData polyfill | ||
if (!global.ImageData) { | ||
global.ImageData = ImageData; | ||
} | ||
|
||
// Use cross-fetch as a polyfill for the browser fetch | ||
if (!global.fetch) { | ||
global.fetch = crossFetch; | ||
} | ||
|
||
console.log("Setup complete"); | ||
} | ||
|
||
module.exports = setupTests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,63 @@ | ||
// Copyright (c) 2018 ml5 | ||
// Copyright (c) 2018-2024 ml5 | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
import { asyncLoadImage } from "../utils/testingUtils"; | ||
import poseNet from "./index"; | ||
import bodyPose from "./index"; | ||
import crossFetch from "cross-fetch"; | ||
|
||
const POSENET_IMG = | ||
"https://github.com/ml5js/ml5-adjacent/raw/master/02_ImageClassification_Video/starter.png"; | ||
|
||
const POSENET_DEFAULTS = { | ||
architecture: "MobileNetV1", | ||
outputStride: 16, | ||
flipHorizontal: false, | ||
minConfidence: 0.5, | ||
maxPoseDetections: 5, | ||
scoreThreshold: 0.5, | ||
nmsRadius: 20, | ||
detectionType: "multiple", | ||
inputResolution: 256, | ||
multiplier: 0.75, | ||
quantBytes: 2, | ||
}; | ||
|
||
describe("PoseNet", () => { | ||
let net; | ||
describe("bodypose", () => { | ||
let myBodyPose; | ||
let image; | ||
|
||
beforeAll(async () => { | ||
jest.setTimeout(10000); | ||
net = await poseNet(); | ||
|
||
// TODO: this should not be necessary! Should already be handled by setupTests.js. | ||
if (!global.fetch) { | ||
global.fetch = crossFetch; | ||
} | ||
|
||
myBodyPose = bodyPose(); | ||
await myBodyPose.ready; | ||
|
||
image = await asyncLoadImage(POSENET_IMG); | ||
}); | ||
|
||
it("instantiates poseNet", () => { | ||
expect(net.architecture).toBe(POSENET_DEFAULTS.architecture); | ||
expect(net.outputStride).toBe(POSENET_DEFAULTS.outputStride); | ||
expect(net.inputResolution).toBe(POSENET_DEFAULTS.inputResolution); | ||
expect(net.multiplier).toBe(POSENET_DEFAULTS.multiplier); | ||
expect(net.quantBytes).toBe(POSENET_DEFAULTS.quantBytes); | ||
it("instantiates bodyPose", () => { | ||
expect(myBodyPose).toBeDefined() | ||
expect(myBodyPose.model).toBeDefined(); | ||
}); | ||
|
||
it("detects poses in image", async () => { | ||
const image = await asyncLoadImage(POSENET_IMG); | ||
|
||
// Result should be an array with a single object containing pose and skeleton. | ||
const result = await net.singlePose(image); | ||
// Result should be an array with a single object containing the detection. | ||
const result = await myBodyPose.detect(image); | ||
expect(result).toHaveLength(1); | ||
expect(result[0]).toHaveProperty("pose"); | ||
expect(result[0]).toHaveProperty("skeleton"); | ||
expect(result[0]).toHaveProperty("box"); | ||
expect(result[0]).toHaveProperty("score"); | ||
expect(result[0].keypoints.length).toBeGreaterThanOrEqual(5); | ||
|
||
// Verify a known outcome. | ||
const nose = result[0].pose.keypoints.find( | ||
(keypoint) => keypoint.part === "nose" | ||
const nose = result[0].keypoints.find( | ||
(keypoint) => keypoint.name === "nose" | ||
); | ||
// Should be {"name": "nose", "score": 0.7217329144477844, "x": 454.1112813949585, "y": 256.606980448618} | ||
expect(nose).toBeTruthy(); | ||
expect(nose.position.x).toBeCloseTo(448.6, 0); | ||
expect(nose.position.y).toBeCloseTo(255.9, 0); | ||
expect(nose.score).toBeCloseTo(0.999); | ||
expect(nose.x).toBeCloseTo(454.1, 0); | ||
expect(nose.y).toBeCloseTo(256.6, 0); | ||
expect(nose.score).toBeCloseTo(0.721, 2); | ||
}); | ||
|
||
it("calls the user's callback",(done) => { | ||
expect.assertions(1); | ||
const callback = (result) => { | ||
expect(result).toHaveLength(1); // don't need to repeat the rest | ||
done(); | ||
} | ||
myBodyPose.detect(image, callback); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { createImageData, ImageData } from "canvas"; | ||
|
||
export const asyncLoadImage = async (src) => { | ||
const img = new Image(); | ||
if (src.startsWith("http")) { | ||
img.crossOrigin = "true"; | ||
} | ||
img.src = src; | ||
await new Promise((resolve) => { | ||
img.onload = resolve; | ||
}); | ||
return img; | ||
}; | ||
|
||
export const getRobin = async () => { | ||
return asyncLoadImage( | ||
"https://cdn.jsdelivr.net/gh/ml5js/ml5-library@main/assets/bird.jpg" | ||
); | ||
}; | ||
|
||
export const randomImageData = (width = 200, height = 100) => { | ||
const length = width * height * 4; // 4 channels - RGBA | ||
// Create an array of random pixel values | ||
const array = Uint8ClampedArray.from({ length }, () => | ||
Math.floor(Math.random() * 256) | ||
); | ||
// Initialize a new ImageData object | ||
return createImageData(array, width, height); | ||
}; | ||
|
||
export const polyfillImageData = () => { | ||
if (!global.ImageData) { | ||
global.ImageData = ImageData; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an aside - It is slightly concerning that the old model had a
0.999
confidence score for detecting the nose and the new model is only0.721
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering if the confidence score will change when running it with the tfjs runtime rather than mediapipe 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, maybe it would make more sense to just validate that a floating point confidence score is outputted rather than a specific value? But we can address this later!