Skip to content

Commit bcbbb63

Browse files
committed
Merge pull request #425 from berak:python_tracking
2 parents c8053da + 59e9a9a commit bcbbb63

File tree

6 files changed

+108
-43
lines changed

6 files changed

+108
-43
lines changed

modules/tracking/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
set(the_description "Tracking API")
2-
ocv_define_module(tracking opencv_imgproc opencv_core opencv_video opencv_highgui opencv_datasets)
2+
ocv_define_module(tracking opencv_imgproc opencv_core opencv_video opencv_highgui opencv_datasets WRAP python)

modules/tracking/include/opencv2/tracking/onlineMIL.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace cv
5858

5959
class ClfOnlineStump;
6060

61-
class ClfMilBoost
61+
class CV_EXPORTS ClfMilBoost
6262
{
6363
public:
6464
struct CV_EXPORTS Params

modules/tracking/include/opencv2/tracking/tracker.hpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ namespace cv
7474

7575
/** @brief Abstract base class for TrackerFeature that represents the feature.
7676
*/
77-
class CV_EXPORTS_W TrackerFeature
77+
class CV_EXPORTS TrackerFeature
7878
{
7979
public:
8080
virtual ~TrackerFeature();
@@ -131,7 +131,7 @@ TrackerFeatureSet is an aggregation of TrackerFeature
131131
TrackerFeature
132132
133133
*/
134-
class CV_EXPORTS_W TrackerFeatureSet
134+
class CV_EXPORTS TrackerFeatureSet
135135
{
136136
public:
137137

@@ -209,7 +209,7 @@ class CV_EXPORTS_W TrackerFeatureSet
209209
/** @brief Abstract base class for TrackerSamplerAlgorithm that represents the algorithm for the specific
210210
sampler.
211211
*/
212-
class CV_EXPORTS_W TrackerSamplerAlgorithm
212+
class CV_EXPORTS TrackerSamplerAlgorithm
213213
{
214214
public:
215215
/**
@@ -261,7 +261,7 @@ TrackerSampler is an aggregation of TrackerSamplerAlgorithm
261261
@sa
262262
TrackerSamplerAlgorithm
263263
*/
264-
class CV_EXPORTS_W TrackerSampler
264+
class CV_EXPORTS TrackerSampler
265265
{
266266
public:
267267

@@ -334,7 +334,7 @@ See @cite AAM \f$\hat{x}^{i}_{k}\f$ all the states candidates.
334334
Inherits this class with your Target state, In own implementation you can add scale variation,
335335
width, height, orientation, etc.
336336
*/
337-
class CV_EXPORTS_W TrackerTargetState
337+
class CV_EXPORTS TrackerTargetState
338338
{
339339
public:
340340
virtual ~TrackerTargetState()
@@ -403,7 +403,7 @@ See @cite AAM State estimator
403403
404404
See @cite AMVOT Statistical modeling (Fig. 3), Table III (generative) - IV (discriminative) - V (hybrid)
405405
*/
406-
class CV_EXPORTS_W TrackerStateEstimator
406+
class CV_EXPORTS TrackerStateEstimator
407407
{
408408
public:
409409
virtual ~TrackerStateEstimator();
@@ -449,7 +449,7 @@ See @cite AAM Ak
449449
450450
Inherits this with your TrackerModel
451451
*/
452-
class CV_EXPORTS_W TrackerModel
452+
class CV_EXPORTS TrackerModel
453453
{
454454
public:
455455

@@ -539,7 +539,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
539539
540540
@return True if initialization went succesfully, false otherwise
541541
*/
542-
bool init( const Mat& image, const Rect2d& boundingBox );
542+
CV_WRAP bool init( const Mat& image, const Rect2d& boundingBox );
543543

544544
/** @brief Update the tracker, find the new most likely bounding box for the target
545545
@param image The current frame
@@ -550,7 +550,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
550550
current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
551551
missing from the frame (say, out of sight)
552552
*/
553-
bool update( const Mat& image, Rect2d& boundingBox );
553+
CV_WRAP bool update( const Mat& image, CV_OUT Rect2d& boundingBox );
554554

555555
/** @brief Creates a tracker by its name.
556556
@param trackerType Tracker type
@@ -560,7 +560,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
560560
- "MIL" -- TrackerMIL
561561
- "BOOSTING" -- TrackerBoosting
562562
*/
563-
static Ptr<Tracker> create( const String& trackerType );
563+
CV_WRAP static Ptr<Tracker> create( const String& trackerType );
564564

565565
virtual void read( const FileNode& fn )=0;
566566
virtual void write( FileStorage& fs ) const=0;
@@ -587,7 +587,7 @@ class CV_EXPORTS_W Tracker : public virtual Algorithm
587587

588588
/** @brief TrackerStateEstimator based on Boosting
589589
*/
590-
class CV_EXPORTS_W TrackerStateEstimatorMILBoosting : public TrackerStateEstimator
590+
class CV_EXPORTS TrackerStateEstimatorMILBoosting : public TrackerStateEstimator
591591
{
592592
public:
593593

@@ -664,7 +664,7 @@ class CV_EXPORTS_W TrackerStateEstimatorMILBoosting : public TrackerStateEstimat
664664

665665
/** @brief TrackerStateEstimatorAdaBoosting based on ADA-Boosting
666666
*/
667-
class CV_EXPORTS_W TrackerStateEstimatorAdaBoosting : public TrackerStateEstimator
667+
class CV_EXPORTS TrackerStateEstimatorAdaBoosting : public TrackerStateEstimator
668668
{
669669
public:
670670
/** @brief Implementation of the target state for TrackerAdaBoostingTargetState
@@ -774,7 +774,7 @@ class CV_EXPORTS_W TrackerStateEstimatorAdaBoosting : public TrackerStateEstimat
774774
/**
775775
* \brief TrackerStateEstimator based on SVM
776776
*/
777-
class CV_EXPORTS_W TrackerStateEstimatorSVM : public TrackerStateEstimator
777+
class CV_EXPORTS TrackerStateEstimatorSVM : public TrackerStateEstimator
778778
{
779779
public:
780780
TrackerStateEstimatorSVM();
@@ -789,7 +789,7 @@ class CV_EXPORTS_W TrackerStateEstimatorSVM : public TrackerStateEstimator
789789

790790
/** @brief TrackerSampler based on CSC (current state centered), used by MIL algorithm TrackerMIL
791791
*/
792-
class CV_EXPORTS_W TrackerSamplerCSC : public TrackerSamplerAlgorithm
792+
class CV_EXPORTS TrackerSamplerCSC : public TrackerSamplerAlgorithm
793793
{
794794
public:
795795
enum
@@ -847,7 +847,7 @@ class CV_EXPORTS_W TrackerSamplerCSC : public TrackerSamplerAlgorithm
847847

848848
/** @brief TrackerSampler based on CS (current state), used by algorithm TrackerBoosting
849849
*/
850-
class CV_EXPORTS_W TrackerSamplerCS : public TrackerSamplerAlgorithm
850+
class CV_EXPORTS TrackerSamplerCS : public TrackerSamplerAlgorithm
851851
{
852852
public:
853853
enum
@@ -914,7 +914,7 @@ It should be noted, that the definition of "similarity" between two rectangles i
914914
their histograms. As experiments show, tracker is *not* very succesfull if target is assumed to
915915
strongly change its dimensions.
916916
*/
917-
class CV_EXPORTS_W TrackerSamplerPF : public TrackerSamplerAlgorithm
917+
class CV_EXPORTS TrackerSamplerPF : public TrackerSamplerAlgorithm
918918
{
919919
public:
920920
/** @brief This structure contains all the parameters that can be varied during the course of sampling
@@ -949,7 +949,7 @@ class CV_EXPORTS_W TrackerSamplerPF : public TrackerSamplerAlgorithm
949949
/**
950950
* \brief TrackerFeature based on Feature2D
951951
*/
952-
class CV_EXPORTS_W TrackerFeatureFeature2d : public TrackerFeature
952+
class CV_EXPORTS TrackerFeatureFeature2d : public TrackerFeature
953953
{
954954
public:
955955

@@ -976,7 +976,7 @@ class CV_EXPORTS_W TrackerFeatureFeature2d : public TrackerFeature
976976
/**
977977
* \brief TrackerFeature based on HOG
978978
*/
979-
class CV_EXPORTS_W TrackerFeatureHOG : public TrackerFeature
979+
class CV_EXPORTS TrackerFeatureHOG : public TrackerFeature
980980
{
981981
public:
982982

@@ -995,7 +995,7 @@ class CV_EXPORTS_W TrackerFeatureHOG : public TrackerFeature
995995
/** @brief TrackerFeature based on HAAR features, used by TrackerMIL and many others algorithms
996996
@note HAAR features implementation is copied from apps/traincascade and modified according to MIL
997997
*/
998-
class CV_EXPORTS_W TrackerFeatureHAAR : public TrackerFeature
998+
class CV_EXPORTS TrackerFeatureHAAR : public TrackerFeature
999999
{
10001000
public:
10011001
struct CV_EXPORTS Params
@@ -1057,7 +1057,7 @@ class CV_EXPORTS_W TrackerFeatureHAAR : public TrackerFeature
10571057
/**
10581058
* \brief TrackerFeature based on LBP
10591059
*/
1060-
class CV_EXPORTS_W TrackerFeatureLBP : public TrackerFeature
1060+
class CV_EXPORTS TrackerFeatureLBP : public TrackerFeature
10611061
{
10621062
public:
10631063

@@ -1083,7 +1083,7 @@ based on @cite MIL .
10831083
10841084
Original code can be found here <http://vision.ucsd.edu/~bbabenko/project_miltrack.shtml>
10851085
*/
1086-
class CV_EXPORTS_W TrackerMIL : public Tracker
1086+
class CV_EXPORTS TrackerMIL : public Tracker
10871087
{
10881088
public:
10891089
struct CV_EXPORTS Params
@@ -1113,7 +1113,7 @@ class CV_EXPORTS_W TrackerMIL : public Tracker
11131113
The classifier uses the surrounding background as negative examples in update step to avoid the
11141114
drifting problem. The implementation is based on @cite OLB .
11151115
*/
1116-
class CV_EXPORTS_W TrackerBoosting : public Tracker
1116+
class CV_EXPORTS TrackerBoosting : public Tracker
11171117
{
11181118
public:
11191119
struct CV_EXPORTS Params
@@ -1151,7 +1151,7 @@ by authors to outperform MIL). During the implementation period the code at
11511151
<http://www.aonsquared.co.uk/node/5>, the courtesy of the author Arthur Amarra, was used for the
11521152
reference purpose.
11531153
*/
1154-
class CV_EXPORTS_W TrackerMedianFlow : public Tracker
1154+
class CV_EXPORTS TrackerMedianFlow : public Tracker
11551155
{
11561156
public:
11571157
struct CV_EXPORTS Params
@@ -1180,7 +1180,7 @@ The Median Flow algorithm (see cv::TrackerMedianFlow) was chosen as a tracking c
11801180
implementation, following authors. Tracker is supposed to be able to handle rapid motions, partial
11811181
occlusions, object absence etc.
11821182
*/
1183-
class CV_EXPORTS_W TrackerTLD : public Tracker
1183+
class CV_EXPORTS TrackerTLD : public Tracker
11841184
{
11851185
public:
11861186
struct CV_EXPORTS Params
@@ -1202,7 +1202,7 @@ class CV_EXPORTS_W TrackerTLD : public Tracker
12021202
* as well as the matlab implementation. For more information about KCF with color-names features, please refer to
12031203
* <http://www.cvl.isy.liu.se/research/objrec/visualtracking/colvistrack/index.html>.
12041204
*/
1205-
class CV_EXPORTS_W TrackerKCF : public Tracker
1205+
class CV_EXPORTS TrackerKCF : public Tracker
12061206
{
12071207
public:
12081208
/**
@@ -1271,7 +1271,7 @@ class CV_EXPORTS_W MultiTracker
12711271
* In the case of trackerType is given, it will be set as the default algorithm for all trackers.
12721272
* @param trackerType the name of the tracker algorithm to be used
12731273
*/
1274-
MultiTracker(const String& trackerType = "");
1274+
CV_WRAP MultiTracker(const String& trackerType = "");
12751275

12761276
/**
12771277
* \brief Destructor
@@ -1284,30 +1284,30 @@ class CV_EXPORTS_W MultiTracker
12841284
* @param image input image
12851285
* @param boundingBox a rectangle represents ROI of the tracked object
12861286
*/
1287-
bool add(const Mat& image, const Rect2d& boundingBox);
1287+
CV_WRAP bool add(const Mat& image, const Rect2d& boundingBox);
12881288

12891289
/**
12901290
* \brief Add a new object to be tracked.
12911291
* @param trackerType the name of the tracker algorithm to be used
12921292
* @param image input image
12931293
* @param boundingBox a rectangle represents ROI of the tracked object
12941294
*/
1295-
bool add(const String& trackerType, const Mat& image, const Rect2d& boundingBox);
1295+
CV_WRAP bool add(const String& trackerType, const Mat& image, const Rect2d& boundingBox);
12961296

12971297
/**
12981298
* \brief Add a set of objects to be tracked.
12991299
* @param trackerType the name of the tracker algorithm to be used
13001300
* @param image input image
13011301
* @param boundingBox list of the tracked objects
13021302
*/
1303-
bool add(const String& trackerType, const Mat& image, std::vector<Rect2d> boundingBox);
1303+
CV_WRAP bool add(const String& trackerType, const Mat& image, std::vector<Rect2d> boundingBox);
13041304

13051305
/**
13061306
* \brief Add a set of objects to be tracked using the defaultAlgorithm tracker.
13071307
* @param image input image
13081308
* @param boundingBox list of the tracked objects
13091309
*/
1310-
bool add(const Mat& image, std::vector<Rect2d> boundingBox);
1310+
CV_WRAP bool add(const Mat& image, std::vector<Rect2d> boundingBox);
13111311

13121312
/**
13131313
* \brief Update the current tracking status.
@@ -1324,7 +1324,7 @@ class CV_EXPORTS_W MultiTracker
13241324
* @param image input image
13251325
* @param boundingBox the tracking result, represent a list of ROIs of the tracked objects.
13261326
*/
1327-
bool update(const Mat& image, std::vector<Rect2d> & boundingBox);
1327+
CV_WRAP bool update(const Mat& image, CV_OUT std::vector<Rect2d> & boundingBox);
13281328

13291329
protected:
13301330
//!< storage for the tracker algorithms.
@@ -1337,8 +1337,8 @@ class CV_EXPORTS_W MultiTracker
13371337
class ROISelector {
13381338
public:
13391339
Rect2d select(Mat img, bool fromCenter = true);
1340-
Rect2d select(const std::string& windowName, Mat img, bool showCrossair = true, bool fromCenter = true);
1341-
void select(const std::string& windowName, Mat img, std::vector<Rect2d> & boundingBox, bool fromCenter = true);
1340+
Rect2d select(const cv::String& windowName, Mat img, bool showCrossair = true, bool fromCenter = true);
1341+
void select(const cv::String& windowName, Mat img, std::vector<Rect2d> & boundingBox, bool fromCenter = true);
13421342

13431343
struct handlerT{
13441344
// basic parameters
@@ -1366,8 +1366,8 @@ class ROISelector {
13661366
};
13671367

13681368
Rect2d CV_EXPORTS_W selectROI(Mat img, bool fromCenter = true);
1369-
Rect2d CV_EXPORTS_W selectROI(const std::string& windowName, Mat img, bool showCrossair = true, bool fromCenter = true);
1370-
void CV_EXPORTS_W selectROI(const std::string& windowName, Mat img, std::vector<Rect2d> & boundingBox, bool fromCenter = true);
1369+
Rect2d CV_EXPORTS_W selectROI(const cv::String& windowName, Mat img, bool showCrossair = true, bool fromCenter = true);
1370+
void CV_EXPORTS_W selectROI(const cv::String& windowName, Mat img, std::vector<Rect2d> & boundingBox, bool fromCenter = true);
13711371

13721372

13731373
/************************************ Multi-Tracker Classes ---By Tyan Vladimir---************************************/
@@ -1376,7 +1376,7 @@ void CV_EXPORTS_W selectROI(const std::string& windowName, Mat img, std::vector<
13761376
13771377
@sa Tracker, MultiTrackerTLD
13781378
*/
1379-
class CV_EXPORTS_W MultiTracker_Alt
1379+
class CV_EXPORTS MultiTracker_Alt
13801380
{
13811381
public:
13821382
/** @brief Constructor for Multitracker
@@ -1433,7 +1433,7 @@ occlusions, object absence etc.
14331433
14341434
@sa Tracker, MultiTracker, TrackerTLD
14351435
*/
1436-
class CV_EXPORTS_W MultiTrackerTLD : public MultiTracker_Alt
1436+
class CV_EXPORTS MultiTrackerTLD : public MultiTracker_Alt
14371437
{
14381438
public:
14391439
/** @brief Update all trackers from the tracking-list, find a new most likely bounding boxes for the targets by
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
import cv2
3+
4+
cv2.namedWindow("tracking")
5+
camera = cv2.VideoCapture("E:/code/opencv/samples/data/768x576.avi")
6+
tracker = cv2.MultiTracker("MIL")
7+
bbox1 = (638.0,230.0,56.0,101.0)
8+
bbox2 = (240.0,210.0,60.0,104.0)
9+
bbox3 = (486.0,149.0,54.0,83.0)
10+
init_once = False
11+
12+
while camera.isOpened():
13+
ok, image=camera.read()
14+
if not ok:
15+
print 'no image read'
16+
break
17+
18+
if not init_once:
19+
# add a list of boxes:
20+
ok = tracker.add(image, (bbox1,bbox2))
21+
# or add single box:
22+
ok = tracker.add(image, bbox3)
23+
init_once = True
24+
25+
ok, boxes = tracker.update(image)
26+
print ok, boxes
27+
28+
for newbox in boxes:
29+
p1 = (int(newbox[0]), int(newbox[1]))
30+
p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
31+
cv2.rectangle(image, p1, p2, (200,0,0))
32+
33+
cv2.imshow("tracking", image)
34+
k = cv2.waitKey(1) & 0xff
35+
if k == 27 : break # esc pressed

modules/tracking/samples/tracker.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import cv2
3+
4+
cv2.namedWindow("tracking")
5+
camera = cv2.VideoCapture("E:/code/opencv/samples/data/768x576.avi")
6+
bbox = (638.0,230.0,56.0,101.0)
7+
tracker = cv2.Tracker_create("MIL")
8+
init_once = False
9+
10+
while camera.isOpened():
11+
ok, image=camera.read()
12+
if not ok:
13+
print 'no image read'
14+
break
15+
16+
if not init_once:
17+
ok = tracker.init(image, bbox)
18+
init_once = True
19+
20+
ok, newbox = tracker.update(image)
21+
print ok, newbox
22+
23+
if ok:
24+
p1 = (int(newbox[0]), int(newbox[1]))
25+
p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
26+
cv2.rectangle(image, p1, p2, (200,0,0))
27+
28+
cv2.imshow("tracking", image)
29+
k = cv2.waitKey(1) & 0xff
30+
if k == 27 : break # esc pressed

0 commit comments

Comments
 (0)