diff --git a/cc/tracking/Trackers/TrackerNano.cc b/cc/tracking/Trackers/TrackerNano.cc index 3c18e0e6..1c4e555e 100644 --- a/cc/tracking/Trackers/TrackerNano.cc +++ b/cc/tracking/Trackers/TrackerNano.cc @@ -13,14 +13,23 @@ NAN_METHOD(TrackerNano::Init) { FF::TryCatch tryCatch("TrackerNano::Init"); cv::Mat image; cv::Rect2d boundingBox; - if ( - Mat::Converter::arg(0, &image, info) || Rect::Converter::arg(1, &boundingBox, info)) { + + // Check if the arguments are correctly passed + if (Mat::Converter::arg(0, &image, info) || Rect::Converter::arg(1, &boundingBox, info)) { return tryCatch.reThrow(); } - TrackerNano::unwrapThis(info)->getTracker()->init(image, boundingBox); + try { + TrackerNano::unwrapThis(info)->getTracker()->init(image, boundingBox); + + // If no error is thrown, return true + info.GetReturnValue().Set(Nan::True()); + } catch (const std::exception& e) { + return tryCatch.throwError(e.what()); + } } + NAN_METHOD(TrackerNano::Update) { FF::TryCatch tryCatch("TrackerNano::Update"); cv::Mat image; diff --git a/test/tests/tracking/TrackerNano.test.ts b/test/tests/tracking/TrackerNano.test.ts index 988aec25..a652c422 100644 --- a/test/tests/tracking/TrackerNano.test.ts +++ b/test/tests/tracking/TrackerNano.test.ts @@ -2,6 +2,13 @@ import { expect } from 'chai'; import { Mat, TrackerNano } from '../../../typings'; import { getTestContext } from '../model'; import toTest from '../toTest'; +import path from 'path'; // Import path module to handle file paths +import { fileURLToPath } from 'url'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const backbonePath = path.join(__dirname, '/TrackerNanoModels/backbone.onnx'); +const neckheadPath = path.join(__dirname, '/TrackerNanoModels/neckhead.onnx'); +let tracker: TrackerNano if (toTest.tracking) { const { @@ -21,7 +28,7 @@ if (toTest.tracking) { describe('constructor', () => { it('can be constructed', () => { - const tracker = new cv.TrackerNano("./TrackerNanoModels/backbone.onnx", "./TrackerNanoModels/neckhead.onnx"); + tracker = new cv.TrackerNano(backbonePath, neckheadPath); expect(tracker).to.have.property('init').to.be.a('function'); expect(tracker).to.have.property('update').to.be.a('function'); }); @@ -30,11 +37,10 @@ if (toTest.tracking) { describe('init', () => { it('should throw if no args', () => { // @ts-expect-error missing args - expect(() => new cv.TrackerNano().init()).to.throw('Tracker::Init - Error: expected argument 0 to be of type'); + expect(() => tracker.init()).to.throw('TrackerNano::Init - Error: expected argument 0 to be of type'); }); it('can be called with frame and initial box', () => { - const tracker = new cv.TrackerNano(); const ret = tracker.init(testImg, new cv.Rect(0, 0, 10, 10)); expect(ret).to.be.true; }); @@ -43,11 +49,10 @@ if (toTest.tracking) { describe('update', () => { it('should throw if no args', () => { // @ts-expect-error missing args - expect(() => new cv.TrackerNano().update()).to.throw('Tracker::Update - Error: expected argument 0 to be of type'); + expect(() => tracker.update()).to.throw('TrackerNano::Update - Error: expected argument 0 to be of type'); }); it('returns bounding box', () => { - const tracker = new cv.TrackerNano(); tracker.init(testImg, new cv.Rect(0, 0, 10, 10)); const rect = tracker.update(testImg); expect(rect).to.be.instanceOf(cv.Rect); @@ -55,4 +60,4 @@ if (toTest.tracking) { }); }); -} \ No newline at end of file +}