forked from justadudewhohacks/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from figamore/NanoTrack-Implementation
TrackerNano implementation
- Loading branch information
Showing
14 changed files
with
6,478 additions
and
5 deletions.
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
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
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,108 @@ | ||
#include "opencv_modules.h" | ||
|
||
#ifdef HAVE_OPENCV_TRACKING | ||
|
||
#include "TrackerNano.h" | ||
|
||
// Ensure that this code is only compiled if OpenCV is 4.7.0 or greater | ||
#if CV_VERSION_GREATER_EQUAL(4, 7, 0) | ||
|
||
Nan::Persistent<v8::FunctionTemplate> TrackerNano::constructor; | ||
|
||
NAN_METHOD(TrackerNano::Init) { | ||
FF::TryCatch tryCatch("TrackerNano::Init"); | ||
cv::Mat image; | ||
cv::Rect2d boundingBox; | ||
|
||
// Check if the arguments are correctly passed | ||
if (Mat::Converter::arg(0, &image, info) || Rect::Converter::arg(1, &boundingBox, info)) { | ||
return tryCatch.reThrow(); | ||
} | ||
|
||
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; | ||
if (Mat::Converter::arg(0, &image, info)) { | ||
return tryCatch.reThrow(); | ||
} | ||
|
||
cv::Rect rect; | ||
bool ret = false; | ||
|
||
try { | ||
ret = TrackerNano::unwrapThis(info)->getTracker()->update(image, rect); | ||
} catch (std::exception& e) { | ||
return tryCatch.throwError(e.what()); | ||
} | ||
|
||
if (ret) { | ||
info.GetReturnValue().Set(Rect::Converter::wrap(rect)); | ||
} else { | ||
info.GetReturnValue().Set(Nan::Null()); | ||
} | ||
} | ||
|
||
NAN_MODULE_INIT(TrackerNano::Init) { | ||
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(TrackerNano::New); | ||
v8::Local<v8::ObjectTemplate> instanceTemplate = ctor->InstanceTemplate(); | ||
|
||
Nan::SetPrototypeMethod(ctor, "init", TrackerNano::Init); | ||
Nan::SetPrototypeMethod(ctor, "update", TrackerNano::Update); | ||
|
||
constructor.Reset(ctor); | ||
ctor->SetClassName(FF::newString("TrackerNano")); | ||
instanceTemplate->SetInternalFieldCount(1); | ||
|
||
Nan::Set(target, FF::newString("TrackerNano"), FF::getFunction(ctor)); | ||
}; | ||
|
||
NAN_METHOD(TrackerNano::New) { | ||
FF::TryCatch tryCatch("TrackerNano::New"); | ||
FF_ASSERT_CONSTRUCT_CALL(); | ||
|
||
// Default model paths | ||
std::string backboneModelPath = "backbone.onnx"; | ||
std::string neckheadModelPath = "neckhead.onnx"; | ||
|
||
// Check if the user passed model paths as arguments | ||
if (info.Length() > 0) { | ||
if (FF::StringConverter::arg(0, &backboneModelPath, info)) { | ||
return tryCatch.reThrow(); | ||
} | ||
} | ||
|
||
if (info.Length() > 1) { | ||
if (FF::StringConverter::arg(1, &neckheadModelPath, info)) { | ||
return tryCatch.reThrow(); | ||
} | ||
} | ||
|
||
// Initialize TrackerNano with provided or default models | ||
TrackerNano* self = new TrackerNano(); | ||
|
||
// Create tracker with provided ONNX models | ||
cv::TrackerNano::Params params; | ||
params.backbone = backboneModelPath; | ||
params.neckhead = neckheadModelPath; | ||
|
||
// Create the tracker instance with these parameters | ||
self->tracker = cv::TrackerNano::create(params); | ||
|
||
self->Wrap(info.Holder()); | ||
info.GetReturnValue().Set(info.Holder()); | ||
} | ||
|
||
#endif | ||
|
||
#endif |
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,29 @@ | ||
#include "../Tracker.h" | ||
|
||
#if CV_VERSION_GREATER_EQUAL(4, 7, 0) | ||
|
||
#ifndef __FF_TRACKERNANO_H__ | ||
#define __FF_TRACKERNANO_H__ | ||
|
||
// Ensure that TrackerNano is only defined for OpenCV 4.7.0 or greater | ||
#if CV_VERSION_GREATER_EQUAL(4, 7, 0) | ||
class TrackerNano : public FF::ObjectWrapBase<TrackerNano>, public Nan::ObjectWrap { | ||
public: | ||
cv::Ptr<cv::TrackerNano> tracker; | ||
|
||
static NAN_MODULE_INIT(Init); | ||
static NAN_METHOD(New); | ||
static NAN_METHOD(Init); | ||
static NAN_METHOD(Update); | ||
|
||
static Nan::Persistent<v8::FunctionTemplate> constructor; | ||
|
||
cv::Ptr<cv::Tracker> getTracker() { | ||
return tracker; | ||
} | ||
}; | ||
#endif | ||
|
||
#endif | ||
|
||
#endif |
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
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,63 @@ | ||
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 { | ||
cv, | ||
cvVersionGreaterEqual, | ||
getTestImg, | ||
} = getTestContext(); | ||
|
||
const hasNano = cvVersionGreaterEqual(4, 7, 0); | ||
|
||
(hasNano ? describe : describe.skip)('TrackerNano', () => { | ||
let testImg: Mat; | ||
|
||
before(() => { | ||
testImg = getTestImg(); | ||
}); | ||
|
||
describe('constructor', () => { | ||
it('can be constructed', () => { | ||
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'); | ||
}); | ||
}); | ||
|
||
describe('init', () => { | ||
it('should throw if no args', () => { | ||
// @ts-expect-error missing args | ||
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 ret = tracker.init(testImg, new cv.Rect(0, 0, 10, 10)); | ||
expect(ret).to.be.true; | ||
}); | ||
}); | ||
|
||
describe('update', () => { | ||
it('should throw if no args', () => { | ||
// @ts-expect-error missing args | ||
expect(() => tracker.update()).to.throw('TrackerNano::Update - Error: expected argument 0 to be of type'); | ||
}); | ||
|
||
it('returns bounding box', () => { | ||
tracker.init(testImg, new cv.Rect(0, 0, 10, 10)); | ||
const rect = tracker.update(testImg); | ||
expect(rect).to.be.instanceOf(cv.Rect); | ||
}); | ||
}); | ||
|
||
}); | ||
} |
Oops, something went wrong.