-
Notifications
You must be signed in to change notification settings - Fork 260
C++ Demo - Object Tracking (VitTrack) #240
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6e73aa5
Preliminary attempt at C++ demo.
ryan1288 ad19119
Update README documentation
ryan1288 8a10724
Fixed text spacing and removed unused arguments. Cleaned up to not us…
ryan1288 7b8c179
Update offsets to match C++ to prevent overlapping text
ryan1288 cb6d08e
Add help functionality
ryan1288 e19f388
Add using namespace for standalone C++ demo file for readability.
ryan1288 b3d0cd3
Update formatting and add save/visualization functionality
ryan1288 11beab7
More formatting changes
ryan1288 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 hidden or 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,32 @@ | ||
| cmake_minimum_required(VERSION 3.24) | ||
| set(project_name "opencv_zoo_object_tracking_vittrack") | ||
|
|
||
| PROJECT (${project_name}) | ||
|
|
||
| set(OPENCV_VERSION "4.9.0") | ||
| set(OPENCV_INSTALLATION_PATH "" CACHE PATH "Where to look for OpenCV installation") | ||
| find_package(OpenCV ${OPENCV_VERSION} REQUIRED HINTS ${OPENCV_INSTALLATION_PATH}) | ||
| # Find OpenCV, you may need to set OpenCV_DIR variable | ||
| # to the absolute path to the directory containing OpenCVConfig.cmake file | ||
| # via the command line or GUI | ||
|
|
||
| file(GLOB SourceFile | ||
| "demo.cpp") | ||
| # If the package has been found, several variables will | ||
| # be set, you can find the full list with descriptions | ||
| # in the OpenCVConfig.cmake file. | ||
| # Print some message showing some of them | ||
| message(STATUS "OpenCV library status:") | ||
| message(STATUS " config: ${OpenCV_DIR}") | ||
| message(STATUS " version: ${OpenCV_VERSION}") | ||
| message(STATUS " libraries: ${OpenCV_LIBS}") | ||
| message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") | ||
|
|
||
| # Declare the executable target built from your sources | ||
| add_executable(${project_name} ${SourceFile}) | ||
|
|
||
| # Set C++ compilation standard to C++11 | ||
| set(CMAKE_CXX_STANDARD 11) | ||
|
|
||
| # Link your application with OpenCV libraries | ||
| target_link_libraries(${project_name} PRIVATE ${OpenCV_LIBS}) |
This file contains hidden or 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 hidden or 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,155 @@ | ||
| #include <iostream> | ||
| #include <opencv2/opencv.hpp> | ||
|
|
||
| using namespace std; | ||
| using namespace cv; | ||
|
|
||
| struct TrackingResult { | ||
| bool isLocated; | ||
| Rect bbox; | ||
| float score; | ||
| }; | ||
|
|
||
| class VitTrack { | ||
| public: | ||
|
|
||
| VitTrack(const string& model_path, int backend_id = 0, int target_id = 0) { | ||
| params.net = model_path; | ||
| params.backend = backend_id; | ||
| params.target = target_id; | ||
| model = TrackerVit::create(params); | ||
| } | ||
|
|
||
| void init(const Mat& image, const Rect& roi) { | ||
| model->init(image, roi); | ||
| } | ||
|
|
||
| TrackingResult infer(const Mat& image) { | ||
| TrackingResult result; | ||
| result.isLocated = model->update(image, result.bbox); | ||
| result.score = model->getTrackingScore(); | ||
| return result; | ||
| } | ||
|
|
||
| private: | ||
| TrackerVit::Params params; | ||
| Ptr<TrackerVit> model; | ||
| }; | ||
|
|
||
| Mat visualize(const Mat& image, const Rect& bbox, float score, bool isLocated, double fps = -1.0, | ||
| const Scalar& box_color = Scalar(0, 255, 0), const Scalar& text_color = Scalar(0, 255, 0), | ||
| double fontScale = 1.0, int fontSize = 1) { | ||
| Mat output = image.clone(); | ||
| int h = output.rows; | ||
| int w = output.cols; | ||
|
|
||
| if (fps >= 0) { | ||
| putText(output, "FPS: " + to_string(fps), Point(0, 30), FONT_HERSHEY_DUPLEX, fontScale, text_color, fontSize); | ||
| } | ||
|
|
||
| if (isLocated && score >= 0.3) { | ||
| rectangle(output, bbox, box_color, 2); | ||
| putText(output, format("%.2f", score), Point(bbox.x, bbox.y + 25), | ||
| FONT_HERSHEY_DUPLEX, fontScale, text_color, fontSize); | ||
| } else { | ||
| Size text_size = getTextSize("Target lost!", FONT_HERSHEY_DUPLEX, fontScale, fontSize, nullptr); | ||
| int text_x = (w - text_size.width) / 2; | ||
| int text_y = (h - text_size.height) / 2; | ||
| putText(output, "Target lost!", Point(text_x, text_y), FONT_HERSHEY_DUPLEX, fontScale, Scalar(0, 0, 255), fontSize); | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| int main(int argc, char** argv) { | ||
| CommandLineParser parser(argc, argv, | ||
| "{help h | | Print help message. }" | ||
| "{input i | |Set path to the input video. Omit for using default camera.}" | ||
| "{model_path |object_tracking_vittrack_2023sep.onnx |Set model path}" | ||
| "{backend_target bt |0 |Choose backend-target pair: 0 - OpenCV implementation + CPU, 1 - CUDA + GPU (CUDA), 2 - CUDA + GPU (CUDA FP16), 3 - TIM-VX + NPU, 4 - CANN + NPU}" | ||
| "{save s |false |Specify to save a file with results. Invalid in case of camera input.}" | ||
| "{vis v |false |Specify to open a new window to show results. Invalid in case of camera input.}"); | ||
| if (parser.has("help")) | ||
| { | ||
| parser.printMessage(); | ||
| return 0; | ||
| } | ||
|
|
||
| string input_path = parser.get<string>("input"); | ||
| string model_path = parser.get<string>("model_path"); | ||
| int backend_target = parser.get<int>("backend_target"); | ||
|
|
||
| vector<vector<int>> backend_target_pairs = { | ||
| {dnn::DNN_BACKEND_OPENCV, dnn::DNN_TARGET_CPU}, | ||
| {dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA}, | ||
| {dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA_FP16}, | ||
| {dnn::DNN_BACKEND_TIMVX, dnn::DNN_TARGET_NPU}, | ||
| {dnn::DNN_BACKEND_CANN, dnn::DNN_TARGET_NPU} | ||
| }; | ||
|
|
||
| int backend_id = backend_target_pairs[backend_target][0]; | ||
| int target_id = backend_target_pairs[backend_target][1]; | ||
|
|
||
| // Create VitTrack tracker | ||
| VitTrack tracker(model_path, backend_id, target_id); | ||
|
|
||
| // Open video capture | ||
| VideoCapture video; | ||
| if (input_path.empty()) { | ||
| video.open(0); // Default camera | ||
| } else { | ||
| video.open(input_path); | ||
| } | ||
|
|
||
| if (!video.isOpened()) { | ||
| cerr << "Error: Could not open video source" << endl; | ||
| return -1; | ||
| } | ||
|
|
||
| // Select an object | ||
| Mat first_frame; | ||
| video >> first_frame; | ||
|
|
||
| if (first_frame.empty()) { | ||
| cerr << "No frames grabbed!" << endl; | ||
| return -1; | ||
| } | ||
|
|
||
| Mat first_frame_copy = first_frame.clone(); | ||
| putText(first_frame_copy, "1. Drag a bounding box to track.", Point(0, 25), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0)); | ||
| putText(first_frame_copy, "2. Press ENTER to confirm", Point(0, 50), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0)); | ||
| Rect roi = selectROI("VitTrack Demo", first_frame_copy); | ||
|
|
||
| if (roi.area() == 0) { | ||
| cerr << "No ROI is selected! Exiting..." << endl; | ||
| return -1; | ||
| } else { | ||
| cout << "Selected ROI: " << roi << endl; | ||
| } | ||
|
|
||
| // Initialize tracker with ROI | ||
| tracker.init(first_frame, roi); | ||
|
|
||
| // Track frame by frame | ||
| TickMeter tm; | ||
| while (waitKey(1) < 0) { | ||
| video >> first_frame; | ||
| if (first_frame.empty()) { | ||
| cout << "End of video" << endl; | ||
| break; | ||
| } | ||
|
|
||
| // Inference | ||
| tm.start(); | ||
| TrackingResult result = tracker.infer(first_frame); | ||
| tm.stop(); | ||
|
|
||
| // Visualize | ||
| Mat frame = first_frame.clone(); | ||
| frame = visualize(frame, result.bbox, result.score, result.isLocated, tm.getFPS()); | ||
| imshow("VitTrack Demo", frame); | ||
| tm.reset(); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or 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
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.
Seems like both of them are not used anywhere.
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.
They are now used! 👍 In both C++ and Python.