Skip to content

Commit

Permalink
Fixed broken integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Van den Abeele committed Mar 3, 2021
1 parent 93d5387 commit a7b1cd7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 66 deletions.
5 changes: 2 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
"roots": [
"<rootDir>/src",
"<rootDir>/tests"
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
Expand All @@ -17,4 +16,4 @@ module.exports = {
],
"transformIgnorePatterns": [],
"globals": {"ts-jest": {"isolatedModules": true},},
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"compile": "node --max-old-space-size=1024 ./node_modules/typescript/lib/tsc.js",
"homebridge": "tsc && homebridge -C -D -P . -U ./resources/test-config/",
"watch": "nodemon",
"test": "jest --config jest.config.js --collect-coverage"
"test": "npm run compile && jest --config jest.config.js --collect-coverage && cd src/integration-tests && node detection-integration-test.js"
},
"config": {
"unsafe-perm": true
Expand Down
66 changes: 66 additions & 0 deletions src/integration-tests/detection-integration-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {Detection, Detector, Loader} from "../motion/coco/loader";
import {Canvas, Image} from "canvas";
import {ImageUtils} from "../utils/image-utils";

const assert = require("assert");
const path = require('path');
const fs = require('fs');

export class DetectionIntegrationTest {

constructor() {
}

public async run(): Promise<void> {
console.log('Loader-detect-image-full-model-IT');
let modelLoader: Loader = new Loader(this.mockLogging());
let detector: Detector = await modelLoader.loadCoco(true);
await this.verifyDetections(detector);

console.log('Loader-detect-image-lite-model-IT');
modelLoader = new Loader(this.mockLogging());
detector = await modelLoader.loadCoco();
await this.verifyDetections(detector);
}

private async verifyDetections(detector: Detector): Promise<void> {
assert(detector !== null);

//This is needed to run on a machine which does not have a .homebridge folder!
const homebridgeDir = require('os').homedir() + '/.homebridge';
ImageUtils.userStoragePath = homebridgeDir;
if (!fs.existsSync(homebridgeDir)) {
fs.mkdirSync(homebridgeDir);
}

let image: Image = await ImageUtils.createImage(path.resolve('../../resources/images/test.jpg'));
assert(image !== null);

let detections: Detection[] = await detector.detect(image, true);
assert(detections !== null);
assert(detections.length > 0);

const annotatedImage: Canvas = await ImageUtils.generateAnnotatedImage(image, detections);
const fileName: string = await ImageUtils.saveCanvasToFile(annotatedImage);
let stats = fs.statSync(fileName);
assert(stats.isFile() == true);

fs.unlinkSync(fileName);
};

private mockLogging(): any {
return {
info: ((message: string) => {
console.log(message);
}),
debug: ((message: string) => {
console.log(message);
})
}
}
}

(async () => {
const test = new DetectionIntegrationTest();
await test.run();
})();
8 changes: 4 additions & 4 deletions src/motion/coco/coco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*
* Edited for use in Node by Kevin Van den Abeele
*/

import * as tfconv from '@tensorflow/tfjs-converter';
Expand Down Expand Up @@ -49,9 +51,7 @@ export interface ModelConfig {

export async function load(config: ModelConfig = {}) {
if (tf == null) {
throw new Error(
`Cannot find TensorFlow.js. If you are using a <script> tag, please ` +
`also include @tensorflow/tfjs on the page before using this model.`);
throw new Error('TensorFlow not loaded correctly!');
}
const base = config.base || 'lite_mobilenet_v2';
const modelUrl = config.modelUrl;
Expand Down Expand Up @@ -233,4 +233,4 @@ export class ObjectDetection {
this.model.dispose();
}
}
}
}
4 changes: 2 additions & 2 deletions src/motion/coco/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Loader {
constructor(private readonly log: Logging) {
}

public async loadCoco(): Promise<Detector> {
public async loadCoco(useFullModel: boolean = false): Promise<Detector> {
const printProcessDuration: Function = (name: string, start: number) => {
this.log.debug(name + ' processing took: ' + (Date.now() - start) + 'ms');
};
Expand All @@ -22,7 +22,7 @@ export class Loader {
}
};

const model: ObjectDetection = await load();
const model: ObjectDetection = await load({base: useFullModel ? 'mobilenet_v2' : 'lite_mobilenet_v2'});
return {
async detect(image: Image): Promise<Detection[]> {
const start = Date.now();
Expand Down
56 changes: 0 additions & 56 deletions tests/detection-integration-test.test.ts

This file was deleted.

0 comments on commit a7b1cd7

Please sign in to comment.