Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
a3be73b5
Commit
a3be73b5
authored
Jun 21, 2012
by
Alexander Smorkalov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
revisions 8681 and 8688 restored. Warning fixed.
Warning: changes beak binary compatibility
parent
0a58d8f1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
375 additions
and
112 deletions
+375
-112
detection_based_tracker.hpp
...ntrib/include/opencv2/contrib/detection_based_tracker.hpp
+58
-10
detection_based_tracker.cpp
modules/contrib/src/detection_based_tracker.cpp
+0
-0
DetectionBasedTracker_jni.cpp
.../android/face-detection/jni/DetectionBasedTracker_jni.cpp
+93
-71
DetectionBasedTracker.java
...tion/src/org/opencv/samples/fd/DetectionBasedTracker.java
+17
-6
FdActivity.java
.../face-detection/src/org/opencv/samples/fd/FdActivity.java
+1
-1
CMakeLists.txt
samples/cpp/CMakeLists.txt
+1
-1
core_vision_tracking_image.cpp
samples/cpp/core_vision_tracking_image.cpp
+55
-0
dbt_face_detection.cpp
samples/cpp/dbt_face_detection.cpp
+104
-0
detection_based_tracker_sample.cpp
samples/cpp/detection_based_tracker_sample.cpp
+46
-23
No files found.
modules/contrib/include/opencv2/contrib/detection_based_tracker.hpp
View file @
a3be73b5
...
...
@@ -7,22 +7,73 @@
#include <vector>
namespace
cv
{
class
DetectionBasedTracker
{
public
:
struct
Parameters
{
int
minObjectSize
;
int
maxObjectSize
;
double
scaleFactor
;
int
maxTrackLifetime
;
int
minNeighbors
;
int
minDetectionPeriod
;
//the minimal time between run of the big object detector (on the whole frame) in ms (1000 mean 1 sec), default=0
Parameters
();
};
DetectionBasedTracker
(
const
std
::
string
&
cascadeFilename
,
const
Parameters
&
params
);
class
IDetector
{
public
:
IDetector
()
:
minObjSize
(
96
,
96
),
maxObjSize
(
INT_MAX
,
INT_MAX
),
minNeighbours
(
2
),
scaleFactor
(
1.1
f
)
{}
virtual
void
detect
(
const
cv
::
Mat
&
Image
,
std
::
vector
<
cv
::
Rect
>&
objects
)
=
0
;
void
setMinObjectSize
(
const
cv
::
Size
&
min
)
{
minObjSize
=
min
;
}
void
setMaxObjectSize
(
const
cv
::
Size
&
max
)
{
maxObjSize
=
max
;
}
cv
::
Size
getMinObjectSize
()
const
{
return
minObjSize
;
}
cv
::
Size
getMaxObjectSize
()
const
{
return
maxObjSize
;
}
float
getScaleFactor
()
{
return
scaleFactor
;
}
void
setScaleFactor
(
float
value
)
{
scaleFactor
=
value
;
}
int
getMinNeighbours
()
{
return
minNeighbours
;
}
void
setMinNeighbours
(
int
value
)
{
minNeighbours
=
value
;
}
virtual
~
IDetector
()
{}
protected
:
cv
::
Size
minObjSize
;
cv
::
Size
maxObjSize
;
int
minNeighbours
;
float
scaleFactor
;
};
DetectionBasedTracker
(
cv
::
Ptr
<
IDetector
>
MainDetector
,
cv
::
Ptr
<
IDetector
>
TrackingDetector
,
const
Parameters
&
params
);
virtual
~
DetectionBasedTracker
();
virtual
bool
run
();
...
...
@@ -44,7 +95,6 @@ class DetectionBasedTracker
cv
::
Ptr
<
SeparateDetectionWork
>
separateDetectionWork
;
friend
void
*
workcycleObjectDetectorFunction
(
void
*
p
);
struct
InnerParameters
{
int
numLastPositionsToTrack
;
...
...
@@ -90,13 +140,11 @@ class DetectionBasedTracker
std
::
vector
<
float
>
weightsPositionsSmoothing
;
std
::
vector
<
float
>
weightsSizesSmoothing
;
cv
::
CascadeClassifier
cascadeForTracking
;
cv
::
Ptr
<
IDetector
>
cascadeForTracking
;
void
updateTrackedObjects
(
const
std
::
vector
<
cv
::
Rect
>&
detectedObjects
);
cv
::
Rect
calcTrackedObjectPositionToShow
(
int
i
)
const
;
void
detectInRegion
(
const
cv
::
Mat
&
img
,
const
cv
::
Rect
&
r
,
std
::
vector
<
cv
::
Rect
>&
detectedObjectsInRegions
);
};
}
//end of cv namespace
#endif
modules/contrib/src/detection_based_tracker.cpp
View file @
a3be73b5
This diff is collapsed.
Click to expand it.
samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp
View file @
a3be73b5
...
...
@@ -18,6 +18,29 @@ inline void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
mat
=
Mat
(
v_rect
,
true
);
}
class
CascadeDetectorAdapter
:
public
DetectionBasedTracker
::
IDetector
{
public
:
CascadeDetectorAdapter
(
cv
::
Ptr
<
cv
::
CascadeClassifier
>
detector
)
:
IDetector
(),
Detector
(
detector
)
{
CV_Assert
(
!
detector
.
empty
());
}
void
detect
(
const
cv
::
Mat
&
Image
,
std
::
vector
<
cv
::
Rect
>
&
objects
)
{
Detector
->
detectMultiScale
(
Image
,
objects
,
scaleFactor
,
minNeighbours
,
0
,
minObjSize
,
maxObjSize
);
}
virtual
~
CascadeDetectorAdapter
()
{}
private
:
CascadeDetectorAdapter
();
cv
::
Ptr
<
cv
::
CascadeClassifier
>
Detector
;
};
JNIEXPORT
jlong
JNICALL
Java_org_opencv_samples_fd_DetectionBasedTracker_nativeCreateObject
(
JNIEnv
*
jenv
,
jclass
,
jstring
jFileName
,
jint
faceSize
)
{
...
...
@@ -27,25 +50,26 @@ JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeC
try
{
DetectionBasedTracker
::
Parameters
DetectorParams
;
if
(
faceSize
>
0
)
DetectorParams
.
minObjectSize
=
faceSize
;
result
=
(
jlong
)
new
DetectionBasedTracker
(
stdFileName
,
DetectorParams
);
// TODO: Reimplement using adapter
// DetectionBasedTracker::Parameters DetectorParams;
// if (faceSize > 0)
// DetectorParams.minObjectSize = faceSize;
// result = (jlong)new DetectionBasedTracker(stdFileName, DetectorParams);
}
catch
(
cv
::
Exception
e
)
{
LOGD
(
"nativeCreateObject catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
LOGD
(
"nativeCreateObject catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
}
catch
(...)
{
LOGD
(
"nativeCreateObject catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
return
0
;
LOGD
(
"nativeCreateObject catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
return
0
;
}
return
result
;
...
...
@@ -56,22 +80,22 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDe
{
try
{
((
DetectionBasedTracker
*
)
thiz
)
->
stop
();
delete
(
DetectionBasedTracker
*
)
thiz
;
((
DetectionBasedTracker
*
)
thiz
)
->
stop
();
delete
(
DetectionBasedTracker
*
)
thiz
;
}
catch
(
cv
::
Exception
e
)
{
LOGD
(
"nativeestroyObject catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
LOGD
(
"nativeestroyObject catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
}
catch
(...)
{
LOGD
(
"nativeDestroyObject catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
LOGD
(
"nativeDestroyObject catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
}
}
...
...
@@ -80,21 +104,21 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSt
{
try
{
((
DetectionBasedTracker
*
)
thiz
)
->
run
();
((
DetectionBasedTracker
*
)
thiz
)
->
run
();
}
catch
(
cv
::
Exception
e
)
{
LOGD
(
"nativeStart catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
LOGD
(
"nativeStart catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
}
catch
(...)
{
LOGD
(
"nativeStart catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
LOGD
(
"nativeStart catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
}
}
...
...
@@ -103,21 +127,21 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSt
{
try
{
((
DetectionBasedTracker
*
)
thiz
)
->
stop
();
((
DetectionBasedTracker
*
)
thiz
)
->
stop
();
}
catch
(
cv
::
Exception
e
)
{
LOGD
(
"nativeStop catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
LOGD
(
"nativeStop catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
}
catch
(...)
{
LOGD
(
"nativeStop catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
LOGD
(
"nativeStop catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
}
}
...
...
@@ -126,28 +150,27 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSe
{
try
{
if
(
faceSize
>
0
)
{
DetectionBasedTracker
::
Parameters
DetectorParams
=
\
((
DetectionBasedTracker
*
)
thiz
)
->
getParameters
();
DetectorParams
.
minObjectSize
=
faceSize
;
((
DetectionBasedTracker
*
)
thiz
)
->
setParameters
(
DetectorParams
);
}
if
(
faceSize
>
0
)
{
// TODO: Reimplement using adapter
// DetectionBasedTracker::Parameters DetectorParams = ((DetectionBasedTracker*)thiz)->getParameters();
// DetectorParams.minObjectSize = faceSize;
// ((DetectionBasedTracker*)thiz)->setParameters(DetectorParams);
}
}
catch
(
cv
::
Exception
e
)
{
LOGD
(
"nativeStop catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
LOGD
(
"nativeStop catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
}
catch
(...)
{
LOGD
(
"nativeSetFaceSize catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
LOGD
(
"nativeSetFaceSize catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
}
}
...
...
@@ -157,23 +180,23 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDe
{
try
{
vector
<
Rect
>
RectFaces
;
((
DetectionBasedTracker
*
)
thiz
)
->
process
(
*
((
Mat
*
)
imageGray
));
((
DetectionBasedTracker
*
)
thiz
)
->
getObjects
(
RectFaces
);
vector_Rect_to_Mat
(
RectFaces
,
*
((
Mat
*
)
faces
)
);
vector
<
Rect
>
RectFaces
;
((
DetectionBasedTracker
*
)
thiz
)
->
process
(
*
((
Mat
*
)
imageGray
));
((
DetectionBasedTracker
*
)
thiz
)
->
getObjects
(
RectFaces
);
*
((
Mat
*
)
faces
)
=
Mat
(
RectFaces
,
true
);
}
catch
(
cv
::
Exception
e
)
{
LOGD
(
"nativeCreateObject catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
LOGD
(
"nativeCreateObject catched cv::Exception: %s"
,
e
.
what
());
jclass
je
=
jenv
->
FindClass
(
"org/opencv/core/CvException"
);
if
(
!
je
)
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
e
.
what
());
}
catch
(...)
{
LOGD
(
"nativeDetect catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
LOGD
(
"nativeDetect catched unknown exception"
);
jclass
je
=
jenv
->
FindClass
(
"java/lang/Exception"
);
jenv
->
ThrowNew
(
je
,
"Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}"
);
}
}
\ No newline at end of file
}
samples/android/face-detection/src/org/opencv/samples/fd/DetectionBasedTracker.java
View file @
a3be73b5
...
...
@@ -7,7 +7,9 @@ public class DetectionBasedTracker
{
public
DetectionBasedTracker
(
String
cascadeName
,
int
minFaceSize
)
{
mNativeObj
=
nativeCreateObject
(
cascadeName
,
minFaceSize
);
mMainDetector
=
nativeCreateDetector
(
cascadeName
,
minFaceSize
);
mTrackingDetector
=
nativeCreateDetector
(
cascadeName
,
minFaceSize
);
mNativeObj
=
nativeCreateTracker
(
mMainDetector
,
mTrackingDetector
);
}
public
void
start
()
...
...
@@ -22,7 +24,8 @@ public class DetectionBasedTracker
public
void
setMinFaceSize
(
int
size
)
{
nativeSetFaceSize
(
mNativeObj
,
size
);
nativeSetFaceSize
(
mMainDetector
,
size
);
nativeSetFaceSize
(
mTrackingDetector
,
size
);
}
public
void
detect
(
Mat
imageGray
,
MatOfRect
faces
)
...
...
@@ -32,17 +35,25 @@ public class DetectionBasedTracker
public
void
release
()
{
nativeDestroyObject
(
mNativeObj
);
nativeDestroyTracker
(
mNativeObj
);
nativeDestroyDetector
(
mMainDetector
);
nativeDestroyDetector
(
mTrackingDetector
);
mNativeObj
=
0
;
mMainDetector
=
0
;
mTrackingDetector
=
0
;
}
private
long
mNativeObj
=
0
;
private
long
mMainDetector
=
0
;
private
long
mTrackingDetector
=
0
;
private
static
native
long
nativeCreateObject
(
String
cascadeName
,
int
minFaceSize
);
private
static
native
void
nativeDestroyObject
(
long
thiz
);
private
static
native
long
nativeCreateDetector
(
String
cascadeName
,
int
minFaceSize
);
private
static
native
long
nativeCreateTracker
(
long
mainDetector
,
long
trackingDetector
);
private
static
native
void
nativeDestroyTracker
(
long
tracker
);
private
static
native
void
nativeDestroyDetector
(
long
detector
);
private
static
native
void
nativeStart
(
long
thiz
);
private
static
native
void
nativeStop
(
long
thiz
);
private
static
native
void
nativeSetFaceSize
(
long
thiz
,
int
size
);
private
static
native
void
nativeSetFaceSize
(
long
detector
,
int
size
);
private
static
native
void
nativeDetect
(
long
thiz
,
long
inputImage
,
long
faces
);
static
...
...
samples/android/face-detection/src/org/opencv/samples/fd/FdActivity.java
View file @
a3be73b5
...
...
@@ -91,7 +91,7 @@ public class FdActivity extends Activity {
ad
.
setMessage
(
"Fatal error: can't open camera!"
);
ad
.
setButton
(
"OK"
,
new
DialogInterface
.
OnClickListener
()
{
public
void
onClick
(
DialogInterface
dialog
,
int
which
)
{
dialog
.
dismiss
();
dialog
.
dismiss
();
finish
();
}
});
...
...
samples/cpp/CMakeLists.txt
View file @
a3be73b5
...
...
@@ -3,7 +3,7 @@
#
# ----------------------------------------------------------------------------
SET
(
OPENCV_CPP_SAMPLES_REQUIRED_DEPS opencv_core opencv_flann opencv_imgproc
SET
(
OPENCV_CPP_SAMPLES_REQUIRED_DEPS opencv_core
_vision_api opencv_core
opencv_flann opencv_imgproc
opencv_highgui opencv_ml opencv_video opencv_objdetect opencv_photo opencv_nonfree
opencv_features2d opencv_calib3d opencv_legacy opencv_contrib opencv_stitching opencv_videostab
)
...
...
samples/cpp/core_vision_tracking_image.cpp
0 → 100644
View file @
a3be73b5
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
#include <opencv2/core_vision_api/tracker.hpp>
#include <stdio.h>
#include <string>
#include <vector>
using
namespace
std
;
using
namespace
cv
;
const
string
WindowName
=
"Face Detection example"
;
const
Scalar
RectColor
=
CV_RGB
(
0
,
255
,
0
);
int
main
()
{
namedWindow
(
WindowName
);
cv
::
moveWindow
(
WindowName
,
100
,
100
);
Mat
Viewport
;
Mat
ReferenceFrame
=
imread
(
"board.jpg"
);
if
(
ReferenceFrame
.
empty
())
{
printf
(
"Error: Cannot load input image
\n
"
);
return
1
;
}
cv
::
Ptr
<
nv
::
Tracker
>
tracker
=
nv
::
Algorithm
::
create
<
nv
::
Tracker
>
(
"nv::Tracker::OpticalFlow"
);
tracker
->
initialize
();
// First frame for initialization
tracker
->
feed
(
ReferenceFrame
);
nv
::
Tracker
::
TrackedObjectHandler
obj
=
tracker
->
addObject
(
cv
::
Rect
(
100
,
100
,
200
,
200
));
while
(
true
)
{
tracker
->
feed
(
ReferenceFrame
);
if
(
obj
->
getStatus
()
==
nv
::
Tracker
::
LOST_STATUS
)
break
;
cv
::
Rect
currentLocation
=
obj
->
getLocation
();
ReferenceFrame
.
copyTo
(
Viewport
);
rectangle
(
Viewport
,
currentLocation
,
RectColor
);
imshow
(
WindowName
,
Viewport
);
if
(
cvWaitKey
(
30
)
>=
0
)
break
;
}
return
0
;
}
samples/cpp/dbt_face_detection.cpp
0 → 100644
View file @
a3be73b5
#if 0 //defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/contrib/detection_based_tracker.hpp>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using namespace cv;
const string WindowName = "Face Detection example";
class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector
{
public:
CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
IDetector(),
Detector(detector)
{
CV_Assert(!detector.empty());
}
void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects)
{
Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);
}
virtual ~CascadeDetectorAdapter()
{}
private:
CascadeDetectorAdapter();
cv::Ptr<cv::CascadeClassifier> Detector;
};
int main(int argc, char* argv[])
{
namedWindow(WindowName);
VideoCapture VideoStream(0);
if (!VideoStream.isOpened())
{
printf("Error: Cannot open video stream from camera\n");
return 1;
}
std::string cascadeFrontalfilename = "../../data/lbpcascades/lbpcascade_frontalface.xml";
cv::Ptr<cv::CascadeClassifier> cascade = new cv::CascadeClassifier(cascadeFrontalfilename);
cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = new CascadeDetectorAdapter(cascade);
cascade = new cv::CascadeClassifier(cascadeFrontalfilename);
cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = new CascadeDetectorAdapter(cascade);
DetectionBasedTracker::Parameters params;
DetectionBasedTracker Detector(MainDetector, TrackingDetector, params);
if (!Detector.run())
{
printf("Error: Detector initialization failed\n");
return 2;
}
Mat ReferenceFrame;
Mat GrayFrame;
vector<Rect> Faces;
while(true)
{
VideoStream >> ReferenceFrame;
cvtColor(ReferenceFrame, GrayFrame, COLOR_RGB2GRAY);
Detector.process(GrayFrame);
Detector.getObjects(Faces);
for (size_t i = 0; i < Faces.size(); i++)
{
rectangle(ReferenceFrame, Faces[i], CV_RGB(0,255,0));
}
imshow(WindowName, ReferenceFrame);
if (cvWaitKey(30) >= 0) break;
}
Detector.stop();
return 0;
}
#else
#include <stdio.h>
int
main
()
{
printf
(
"This sample works for UNIX or ANDROID only
\n
"
);
return
0
;
}
#endif
samples/cpp/detection_based_tracker_sample.cpp
View file @
a3be73b5
...
...
@@ -43,8 +43,6 @@
#define LOGE(...) do{} while(0)
#endif
using
namespace
cv
;
using
namespace
std
;
...
...
@@ -63,9 +61,31 @@ static void usage()
LOGE0
(
"
\t
(e.g.
\"
opencv/data/lbpcascades/lbpcascade_frontalface.xml
\"
"
);
}
class
CascadeDetectorAdapter
:
public
DetectionBasedTracker
::
IDetector
{
public
:
CascadeDetectorAdapter
(
cv
::
Ptr
<
cv
::
CascadeClassifier
>
detector
)
:
Detector
(
detector
)
{
CV_Assert
(
!
detector
.
empty
());
}
void
detect
(
const
cv
::
Mat
&
Image
,
std
::
vector
<
cv
::
Rect
>
&
objects
)
{
Detector
->
detectMultiScale
(
Image
,
objects
,
1.1
,
3
,
0
,
minObjSize
,
maxObjSize
);
}
virtual
~
CascadeDetectorAdapter
()
{}
private
:
CascadeDetectorAdapter
();
cv
::
Ptr
<
cv
::
CascadeClassifier
>
Detector
;
};
static
int
test_FaceDetector
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
4
)
{
if
(
argc
<
4
)
{
usage
();
return
-
1
;
}
...
...
@@ -80,12 +100,14 @@ static int test_FaceDetector(int argc, char *argv[])
vector
<
Mat
>
images
;
{
char
filename
[
256
];
for
(
int
n
=
1
;
;
n
++
)
{
for
(
int
n
=
1
;
;
n
++
)
{
snprintf
(
filename
,
sizeof
(
filename
),
filepattern
,
n
);
LOGD
(
"filename='%s'"
,
filename
);
Mat
m0
;
m0
=
imread
(
filename
);
if
(
m0
.
empty
())
{
if
(
m0
.
empty
())
{
LOGI0
(
"Cannot read the file --- break"
);
break
;
}
...
...
@@ -94,10 +116,15 @@ static int test_FaceDetector(int argc, char *argv[])
LOGD
(
"read %d images"
,
(
int
)
images
.
size
());
}
DetectionBasedTracker
::
Parameters
params
;
std
::
string
cascadeFrontalfilename
=
cascadefile
;
cv
::
Ptr
<
cv
::
CascadeClassifier
>
cascade
=
new
cv
::
CascadeClassifier
(
cascadeFrontalfilename
);
cv
::
Ptr
<
DetectionBasedTracker
::
IDetector
>
MainDetector
=
new
CascadeDetectorAdapter
(
cascade
);
cascade
=
new
cv
::
CascadeClassifier
(
cascadeFrontalfilename
);
cv
::
Ptr
<
DetectionBasedTracker
::
IDetector
>
TrackingDetector
=
new
CascadeDetectorAdapter
(
cascade
);
DetectionBasedTracker
fd
(
cascadeFrontalfilename
,
params
);
DetectionBasedTracker
::
Parameters
params
;
DetectionBasedTracker
fd
(
MainDetector
,
TrackingDetector
,
params
);
fd
.
run
();
...
...
@@ -108,12 +135,13 @@ static int test_FaceDetector(int argc, char *argv[])
double
freq
=
getTickFrequency
();
int
num_images
=
images
.
size
();
for
(
int
n
=
1
;
n
<=
num_images
;
n
++
)
{
for
(
int
n
=
1
;
n
<=
num_images
;
n
++
)
{
int64
tcur
=
getTickCount
();
int64
dt
=
tcur
-
tprev
;
tprev
=
tcur
;
double
t_ms
=
((
double
)
dt
)
/
freq
*
1000.0
;
LOGD
(
"
\n\n
STEP n=%d from prev step %f ms
\n
\n
"
,
n
,
t_ms
);
LOGD
(
"
\n\n
STEP n=%d from prev step %f ms
\n
"
,
n
,
t_ms
);
m
=
images
[
n
-
1
];
CV_Assert
(
!
m
.
empty
());
cvtColor
(
m
,
gray
,
CV_BGR2GRAY
);
...
...
@@ -123,11 +151,8 @@ static int test_FaceDetector(int argc, char *argv[])
vector
<
Rect
>
result
;
fd
.
getObjects
(
result
);
for
(
size_t
i
=
0
;
i
<
result
.
size
();
i
++
)
{
for
(
size_t
i
=
0
;
i
<
result
.
size
();
i
++
)
{
Rect
r
=
result
[
i
];
CV_Assert
(
r
.
area
()
>
0
);
Point
tl
=
r
.
tl
();
...
...
@@ -136,14 +161,14 @@ static int test_FaceDetector(int argc, char *argv[])
rectangle
(
m
,
tl
,
br
,
color
,
3
);
}
}
char
outfilename
[
256
];
for
(
int
n
=
1
;
n
<=
num_images
;
n
++
)
{
char
outfilename
[
256
];
for
(
int
n
=
1
;
n
<=
num_images
;
n
++
)
{
snprintf
(
outfilename
,
sizeof
(
outfilename
),
outfilepattern
,
n
);
LOGD
(
"outfilename='%s'"
,
outfilename
);
m
=
images
[
n
-
1
];
imwrite
(
outfilename
,
m
);
}
snprintf
(
outfilename
,
sizeof
(
outfilename
),
outfilepattern
,
n
);
LOGD
(
"outfilename='%s'"
,
outfilename
);
m
=
images
[
n
-
1
];
imwrite
(
outfilename
,
m
);
}
fd
.
stop
();
...
...
@@ -151,8 +176,6 @@ static int test_FaceDetector(int argc, char *argv[])
return
0
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
return
test_FaceDetector
(
argc
,
argv
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment