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
0e355313
Commit
0e355313
authored
Dec 06, 2013
by
Andrey Pavlenko
Committed by
OpenCV Buildbot
Dec 06, 2013
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1872 from KonstantinMatskevich:ocl_mt_test
parents
c34fc180
45dd1895
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
114 additions
and
27 deletions
+114
-27
facedetect.cpp
samples/ocl/facedetect.cpp
+114
-27
No files found.
samples/ocl/facedetect.cpp
View file @
0e355313
...
...
@@ -5,10 +5,16 @@
#include <iostream>
#include <stdio.h>
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
# include <thread>
#endif
using
namespace
std
;
using
namespace
cv
;
#define LOOP_NUM 10
///////////////////////////single-threading faces detecting///////////////////////////////
const
static
Scalar
colors
[]
=
{
CV_RGB
(
0
,
0
,
255
),
CV_RGB
(
0
,
128
,
255
),
CV_RGB
(
0
,
255
,
255
),
...
...
@@ -22,7 +28,7 @@ const static Scalar colors[] = { CV_RGB(0,0,255),
int64
work_begin
=
0
;
int64
work_end
=
0
;
string
output
Name
;
string
inputName
,
outputName
,
cascade
Name
;
static
void
workBegin
()
{
...
...
@@ -58,41 +64,17 @@ static void Draw(Mat& img, vector<Rect>& faces, double scale);
// Else if will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
double
checkRectSimilarity
(
Size
sz
,
vector
<
Rect
>&
cpu_rst
,
vector
<
Rect
>&
gpu_rst
);
int
main
(
int
argc
,
const
char
**
argv
)
static
int
facedetect_one_thread
(
bool
useCPU
,
double
scale
)
{
const
char
*
keys
=
"{ h | help | false | print help message }"
"{ i | input | | specify input image }"
"{ t | template | haarcascade_frontalface_alt.xml |"
" specify template file path }"
"{ c | scale | 1.0 | scale image }"
"{ s | use_cpu | false | use cpu or gpu to process the image }"
"{ o | output | facedetect_output.jpg |"
" specify output image save path(only works when input is images) }"
;
CommandLineParser
cmd
(
argc
,
argv
,
keys
);
if
(
cmd
.
get
<
bool
>
(
"help"
))
{
cout
<<
"Usage : facedetect [options]"
<<
endl
;
cout
<<
"Available options:"
<<
endl
;
cmd
.
printParams
();
return
EXIT_SUCCESS
;
}
CvCapture
*
capture
=
0
;
Mat
frame
,
frameCopy
,
image
;
bool
useCPU
=
cmd
.
get
<
bool
>
(
"s"
);
string
inputName
=
cmd
.
get
<
string
>
(
"i"
);
outputName
=
cmd
.
get
<
string
>
(
"o"
);
string
cascadeName
=
cmd
.
get
<
string
>
(
"t"
);
double
scale
=
cmd
.
get
<
double
>
(
"c"
);
ocl
::
OclCascadeClassifier
cascade
;
CascadeClassifier
cpu_cascade
;
if
(
!
cascade
.
load
(
cascadeName
)
||
!
cpu_cascade
.
load
(
cascadeName
)
)
{
cout
<<
"ERROR: Could not load classifier cascade
"
<<
endl
;
cout
<<
"ERROR: Could not load classifier cascade
: "
<<
cascadeName
<<
endl
;
return
EXIT_FAILURE
;
}
...
...
@@ -176,9 +158,114 @@ int main( int argc, const char** argv )
}
cvDestroyWindow
(
"result"
);
std
::
cout
<<
"single-threaded sample has finished"
<<
std
::
endl
;
return
0
;
}
///////////////////////////////////////detectfaces with multithreading////////////////////////////////////////////
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
#define MAX_THREADS 10
static
void
detectFaces
(
std
::
string
fileName
)
{
ocl
::
OclCascadeClassifier
cascade
;
if
(
!
cascade
.
load
(
cascadeName
))
{
std
::
cout
<<
"ERROR: Could not load classifier cascade: "
<<
cascadeName
<<
std
::
endl
;
return
;
}
Mat
img
=
imread
(
fileName
,
CV_LOAD_IMAGE_COLOR
);
if
(
img
.
empty
())
{
std
::
cout
<<
"cann't open file "
+
fileName
<<
std
::
endl
;
return
;
}
ocl
::
oclMat
d_img
;
d_img
.
upload
(
img
);
std
::
vector
<
Rect
>
oclfaces
;
cascade
.
detectMultiScale
(
d_img
,
oclfaces
,
1.1
,
3
,
0
|
CV_HAAR_SCALE_IMAGE
,
Size
(
30
,
30
),
Size
(
0
,
0
));
for
(
unsigned
int
i
=
0
;
i
<
oclfaces
.
size
();
i
++
)
rectangle
(
img
,
Point
(
oclfaces
[
i
].
x
,
oclfaces
[
i
].
y
),
Point
(
oclfaces
[
i
].
x
+
oclfaces
[
i
].
width
,
oclfaces
[
i
].
y
+
oclfaces
[
i
].
height
),
colors
[
i
%
8
],
3
);
std
::
string
::
size_type
pos
=
outputName
.
rfind
(
'.'
);
std
::
string
outputNameTid
=
outputName
+
'-'
+
std
::
to_string
(
_threadid
);
if
(
pos
==
std
::
string
::
npos
)
{
std
::
cout
<<
"Invalid output file name: "
<<
outputName
<<
std
::
endl
;
}
else
{
outputNameTid
=
outputName
.
substr
(
0
,
pos
)
+
"_"
+
std
::
to_string
(
_threadid
)
+
outputName
.
substr
(
pos
);
imwrite
(
outputNameTid
,
img
);
}
imshow
(
outputNameTid
,
img
);
waitKey
(
0
);
}
static
void
facedetect_multithreading
(
int
nthreads
)
{
int
thread_number
=
MAX_THREADS
<
nthreads
?
MAX_THREADS
:
nthreads
;
std
::
vector
<
std
::
thread
>
threads
;
for
(
int
i
=
0
;
i
<
thread_number
;
i
++
)
threads
.
push_back
(
std
::
thread
(
detectFaces
,
inputName
));
for
(
int
i
=
0
;
i
<
thread_number
;
i
++
)
threads
[
i
].
join
();
}
#endif
int
main
(
int
argc
,
const
char
**
argv
)
{
const
char
*
keys
=
"{ h | help | false | print help message }"
"{ i | input | | specify input image }"
"{ t | template | haarcascade_frontalface_alt.xml |"
" specify template file path }"
"{ c | scale | 1.0 | scale image }"
"{ s | use_cpu | false | use cpu or gpu to process the image }"
"{ o | output | facedetect_output.jpg |"
" specify output image save path(only works when input is images) }"
"{ n | thread_num | 1 | set number of threads >= 1 }"
;
CommandLineParser
cmd
(
argc
,
argv
,
keys
);
if
(
cmd
.
get
<
bool
>
(
"help"
))
{
cout
<<
"Usage : facedetect [options]"
<<
endl
;
cout
<<
"Available options:"
<<
endl
;
cmd
.
printParams
();
return
EXIT_SUCCESS
;
}
bool
useCPU
=
cmd
.
get
<
bool
>
(
"s"
);
inputName
=
cmd
.
get
<
string
>
(
"i"
);
outputName
=
cmd
.
get
<
string
>
(
"o"
);
cascadeName
=
cmd
.
get
<
string
>
(
"t"
);
double
scale
=
cmd
.
get
<
double
>
(
"c"
);
int
n
=
cmd
.
get
<
int
>
(
"n"
);
if
(
n
>
1
)
{
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
std
::
cout
<<
"multi-threaded sample is running"
<<
std
::
endl
;
facedetect_multithreading
(
n
);
std
::
cout
<<
"multi-threaded sample has finished"
<<
std
::
endl
;
return
0
;
#else
std
::
cout
<<
"std::thread is not supported, running a single-threaded version"
<<
std
::
endl
;
#endif
}
if
(
n
<
0
)
std
::
cout
<<
"incorrect number of threads:"
<<
n
<<
", running a single-threaded version"
<<
std
::
endl
;
else
std
::
cout
<<
"single-threaded sample is running"
<<
std
::
endl
;
return
facedetect_one_thread
(
useCPU
,
scale
);
}
void
detect
(
Mat
&
img
,
vector
<
Rect
>&
faces
,
ocl
::
OclCascadeClassifier
&
cascade
,
double
scale
,
bool
calTime
)
...
...
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