forked from justadudewhohacks/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 51
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
TrackerNano implementation #165
Merged
UrielCh
merged 13 commits into
UrielCh:TrackerNano
from
figamore:NanoTrack-Implementation
Sep 26, 2024
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b9948e0
testing
figamore 2d0e711
testing
figamore d3d1083
Added TrackerNano
figamore f108fa6
Added to TrackerNano to node-gyp
figamore a4c4340
Add missing declarations for TrackerNano
figamore 1d8af8c
Added default model names to params
figamore d816ddf
fix param names
figamore 18d560a
Accept params for backbone models in TrackerNano constructor
figamore d883152
fix params passing structure
figamore 903b0c9
-Add unit test
figamore 88d05bb
Fixed tests
0bbca70
Added missing OpenCV version check
3e3f4af
Updated OpenCV version check
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
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,117 @@ | ||
#include "opencv_modules.h" | ||
|
||
#ifdef HAVE_OPENCV_TRACKING | ||
|
||
#include "TrackerNano.h" | ||
|
||
#if CV_VERSION_GREATER_EQUAL(3, 2, 0) | ||
|
||
Nan::Persistent<v8::FunctionTemplate> TrackerNano::constructor; | ||
|
||
#if CV_VERSION_GREATER_EQUAL(4, 5, 2) | ||
|
||
NAN_METHOD(TrackerNano::Clear) { | ||
} | ||
|
||
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)) { | ||
return tryCatch.reThrow(); | ||
} | ||
|
||
TrackerNano::unwrapThis(info)->getTracker()->init(image, boundingBox); | ||
} | ||
|
||
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_METHOD(TrackerNano::GetModel) { | ||
// TBD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBD ??? |
||
} | ||
|
||
#endif | ||
|
||
NAN_MODULE_INIT(TrackerNano::Init) { | ||
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(TrackerNano::New); | ||
v8::Local<v8::ObjectTemplate> instanceTemplate = ctor->InstanceTemplate(); | ||
|
||
#if CV_VERSION_GREATER_EQUAL(4, 5, 2) | ||
Nan::SetPrototypeMethod(ctor, "clear", TrackerNano::Clear); | ||
Nan::SetPrototypeMethod(ctor, "init", TrackerNano::Init); | ||
Nan::SetPrototypeMethod(ctor, "update", TrackerNano::Update); | ||
Nan::SetPrototypeMethod(ctor, "getModel", TrackerNano::GetModel); | ||
#else | ||
Tracker::Init(ctor); | ||
#endif | ||
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(); | ||
#if CV_VERSION_GREATER_EQUAL(3, 3, 0) | ||
// Create tracker with provided ONNX models | ||
cv::TrackerNano::Params params; | ||
params.backbone = backboneModelPath; // Setting the backbone model from argument or default | ||
params.neckhead = neckheadModelPath; // Setting the neck-head model from argument or default | ||
|
||
// Create the tracker instance with these parameters | ||
self->tracker = cv::TrackerNano::create(params); | ||
#else | ||
self->tracker = cv::TrackerNano::createTracker(); | ||
#endif | ||
|
||
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,33 @@ | ||
#include "../Tracker.h" | ||
|
||
#if CV_VERSION_GREATER_EQUAL(3, 2, 0) | ||
figamore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#ifndef __FF_TRACKERNANO_H__ | ||
#define __FF_TRACKERNANO_H__ | ||
|
||
#if CV_VERSION_GREATER_EQUAL(4, 5, 2) | ||
class TrackerNano : public FF::ObjectWrapBase<TrackerNano>, public Nan::ObjectWrap { | ||
#else | ||
class TrackerNano : public Tracker { | ||
#endif | ||
public: | ||
cv::Ptr<cv::TrackerNano> tracker; | ||
|
||
static NAN_MODULE_INIT(Init); | ||
static NAN_METHOD(New); // The New method can accept model paths as parameters | ||
#if CV_VERSION_GREATER_EQUAL(4, 5, 2) | ||
static NAN_METHOD(Clear); | ||
static NAN_METHOD(Init); | ||
static NAN_METHOD(Update); | ||
static NAN_METHOD(GetModel); | ||
#endif | ||
static Nan::Persistent<v8::FunctionTemplate> constructor; | ||
|
||
cv::Ptr<cv::Tracker> getTracker() { | ||
return tracker; | ||
} | ||
}; | ||
|
||
#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,15 @@ | ||
import { Mat } from './Mat.d'; | ||
import { Rect } from './Rect.d'; | ||
|
||
export class TrackerNano { | ||
/** | ||
* Creates a new TrackerNano object. | ||
* @param backboneModelPath Optional path to the backbone ONNX model. | ||
* @param neckheadModelPath Optional path to the neckhead ONNX model. | ||
*/ | ||
constructor(backboneModelPath?: string, neckheadModelPath?: string); | ||
|
||
clear(): void; | ||
init(frame: Mat, boundingBox: Rect): boolean; | ||
update(frame: Mat): Rect; | ||
} |
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
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.
Think the min value for the NanoTracker is 4.7.0