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
639b8007
Commit
639b8007
authored
Oct 30, 2017
by
Vadim Pisarevsky
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9941 from catree:improve_dnn_samples
parents
712689e8
48e07437
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
285 additions
and
120 deletions
+285
-120
ssd_mobilenet_object_detection.cpp
samples/dnn/ssd_mobilenet_object_detection.cpp
+58
-18
ssd_object_detection.cpp
samples/dnn/ssd_object_detection.cpp
+98
-44
yolo_object_detection.cpp
samples/dnn/yolo_object_detection.cpp
+129
-58
No files found.
samples/dnn/ssd_mobilenet_object_detection.cpp
View file @
639b8007
...
@@ -23,23 +23,25 @@ const char* classNames[] = {"background",
...
@@ -23,23 +23,25 @@ const char* classNames[] = {"background",
"motorbike"
,
"person"
,
"pottedplant"
,
"motorbike"
,
"person"
,
"pottedplant"
,
"sheep"
,
"sofa"
,
"train"
,
"tvmonitor"
};
"sheep"
,
"sofa"
,
"train"
,
"tvmonitor"
};
const
char
*
about
=
"This sample uses Single-Shot Detector "
const
char
*
about
=
"This sample uses MobileNet Single-Shot Detector "
"(https://arxiv.org/abs/1512.02325)"
"(https://arxiv.org/abs/1704.04861) "
"to detect objects on image.
\n
"
"to detect objects on camera/video/image.
\n
"
".caffemodel model's file is avaliable here: "
".caffemodel model's file is available here: "
"https://github.com/chuanqi305/MobileNet-SSD
\n
"
;
"https://github.com/chuanqi305/MobileNet-SSD
\n
"
"Default network is 300x300 and 20-classes VOC.
\n
"
;
const
char
*
params
const
char
*
params
=
"{ help | false | print usage }"
=
"{ help | false | print usage }"
"{ proto | MobileNetSSD_deploy.prototxt | model configuration }"
"{ proto | MobileNetSSD_deploy.prototxt | model configuration }"
"{ model | MobileNetSSD_deploy.caffemodel | model weights }"
"{ model | MobileNetSSD_deploy.caffemodel | model weights }"
"{ video | | video for detection }"
"{ camera_device | 0 | camera device number }"
"{ video | | video or image for detection}"
"{ out | | path to output video file}"
"{ out | | path to output video file}"
"{ min_confidence | 0.2 | min confidence }"
;
"{ min_confidence | 0.2 | min confidence }"
;
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
cv
::
CommandLineParser
parser
(
argc
,
argv
,
params
);
CommandLineParser
parser
(
argc
,
argv
,
params
);
if
(
parser
.
get
<
bool
>
(
"help"
))
if
(
parser
.
get
<
bool
>
(
"help"
))
{
{
...
@@ -55,19 +57,40 @@ int main(int argc, char** argv)
...
@@ -55,19 +57,40 @@ int main(int argc, char** argv)
dnn
::
Net
net
=
readNetFromCaffe
(
modelConfiguration
,
modelBinary
);
dnn
::
Net
net
=
readNetFromCaffe
(
modelConfiguration
,
modelBinary
);
//! [Initialize network]
//! [Initialize network]
VideoCapture
cap
(
parser
.
get
<
String
>
(
"video"
));
if
(
net
.
empty
())
if
(
!
cap
.
isOpened
())
// check if we succeeded
{
{
cap
=
VideoCapture
(
0
);
cerr
<<
"Can't load network by using the following files: "
<<
endl
;
cerr
<<
"prototxt: "
<<
modelConfiguration
<<
endl
;
cerr
<<
"caffemodel: "
<<
modelBinary
<<
endl
;
cerr
<<
"Models can be downloaded here:"
<<
endl
;
cerr
<<
"https://github.com/chuanqi305/MobileNet-SSD"
<<
endl
;
exit
(
-
1
);
}
VideoCapture
cap
;
if
(
parser
.
get
<
String
>
(
"video"
).
empty
())
{
int
cameraDevice
=
parser
.
get
<
int
>
(
"camera_device"
);
cap
=
VideoCapture
(
cameraDevice
);
if
(
!
cap
.
isOpened
())
{
cout
<<
"Couldn't find camera: "
<<
cameraDevice
<<
endl
;
return
-
1
;
}
}
else
{
cap
.
open
(
parser
.
get
<
String
>
(
"video"
));
if
(
!
cap
.
isOpened
())
if
(
!
cap
.
isOpened
())
{
{
cout
<<
"Couldn't
find camera"
<<
endl
;
cout
<<
"Couldn't
open image or video: "
<<
parser
.
get
<
String
>
(
"video"
)
<<
endl
;
return
-
1
;
return
-
1
;
}
}
}
}
Size
inVideoSize
=
Size
((
int
)
cap
.
get
(
CV_CAP_PROP_FRAME_WIDTH
),
//Acquire input size
Size
inVideoSize
;
(
int
)
cap
.
get
(
CV_CAP_PROP_FRAME_HEIGHT
));
inVideoSize
=
Size
((
int
)
cap
.
get
(
CV_CAP_PROP_FRAME_WIDTH
),
//Acquire input size
(
int
)
cap
.
get
(
CV_CAP_PROP_FRAME_HEIGHT
));
Size
cropSize
;
Size
cropSize
;
if
(
inVideoSize
.
width
/
(
float
)
inVideoSize
.
height
>
WHRatio
)
if
(
inVideoSize
.
width
/
(
float
)
inVideoSize
.
height
>
WHRatio
)
...
@@ -93,9 +116,18 @@ int main(int argc, char** argv)
...
@@ -93,9 +116,18 @@ int main(int argc, char** argv)
for
(;;)
for
(;;)
{
{
Mat
frame
;
Mat
frame
;
cap
>>
frame
;
// get a new frame from camera
cap
>>
frame
;
// get a new frame from camera/video or read image
//! [Prepare blob]
if
(
frame
.
empty
())
{
waitKey
();
break
;
}
if
(
frame
.
channels
()
==
4
)
cvtColor
(
frame
,
frame
,
COLOR_BGRA2BGR
);
//! [Prepare blob]
Mat
inputBlob
=
blobFromImage
(
frame
,
inScaleFactor
,
Mat
inputBlob
=
blobFromImage
(
frame
,
inScaleFactor
,
Size
(
inWidth
,
inHeight
),
meanVal
,
false
);
//Convert Mat to batch of images
Size
(
inWidth
,
inHeight
),
meanVal
,
false
);
//Convert Mat to batch of images
//! [Prepare blob]
//! [Prepare blob]
...
@@ -108,15 +140,23 @@ int main(int argc, char** argv)
...
@@ -108,15 +140,23 @@ int main(int argc, char** argv)
Mat
detection
=
net
.
forward
(
"detection_out"
);
//compute output
Mat
detection
=
net
.
forward
(
"detection_out"
);
//compute output
//! [Make forward pass]
//! [Make forward pass]
std
::
vector
<
double
>
layersTimings
;
vector
<
double
>
layersTimings
;
double
freq
=
getTickFrequency
()
/
1000
;
double
freq
=
getTickFrequency
()
/
1000
;
double
time
=
net
.
getPerfProfile
(
layersTimings
)
/
freq
;
double
time
=
net
.
getPerfProfile
(
layersTimings
)
/
freq
;
cout
<<
"Inference time, ms: "
<<
time
<<
endl
;
Mat
detectionMat
(
detection
.
size
[
2
],
detection
.
size
[
3
],
CV_32F
,
detection
.
ptr
<
float
>
());
Mat
detectionMat
(
detection
.
size
[
2
],
detection
.
size
[
3
],
CV_32F
,
detection
.
ptr
<
float
>
());
frame
=
frame
(
crop
);
frame
=
frame
(
crop
);
ostringstream
ss
;
if
(
!
outputVideo
.
isOpened
())
{
ss
<<
"FPS: "
<<
1000
/
time
<<
" ; time: "
<<
time
<<
" ms"
;
putText
(
frame
,
ss
.
str
(),
Point
(
20
,
20
),
0
,
0.5
,
Scalar
(
0
,
0
,
255
));
}
else
cout
<<
"Inference time, ms: "
<<
time
<<
endl
;
float
confidenceThreshold
=
parser
.
get
<
float
>
(
"min_confidence"
);
float
confidenceThreshold
=
parser
.
get
<
float
>
(
"min_confidence"
);
for
(
int
i
=
0
;
i
<
detectionMat
.
rows
;
i
++
)
for
(
int
i
=
0
;
i
<
detectionMat
.
rows
;
i
++
)
{
{
...
@@ -131,7 +171,7 @@ int main(int argc, char** argv)
...
@@ -131,7 +171,7 @@ int main(int argc, char** argv)
int
xRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
5
)
*
frame
.
cols
);
int
xRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
5
)
*
frame
.
cols
);
int
yRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
6
)
*
frame
.
rows
);
int
yRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
6
)
*
frame
.
rows
);
ostringstream
ss
;
ss
.
str
(
""
)
;
ss
<<
confidence
;
ss
<<
confidence
;
String
conf
(
ss
.
str
());
String
conf
(
ss
.
str
());
...
...
samples/dnn/ssd_object_detection.cpp
View file @
639b8007
...
@@ -40,15 +40,26 @@ static Mat preprocess(const Mat& frame)
...
@@ -40,15 +40,26 @@ static Mat preprocess(const Mat& frame)
return
preprocessed
;
return
preprocessed
;
}
}
const
char
*
classNames
[]
=
{
"background"
,
"aeroplane"
,
"bicycle"
,
"bird"
,
"boat"
,
"bottle"
,
"bus"
,
"car"
,
"cat"
,
"chair"
,
"cow"
,
"diningtable"
,
"dog"
,
"horse"
,
"motorbike"
,
"person"
,
"pottedplant"
,
"sheep"
,
"sofa"
,
"train"
,
"tvmonitor"
};
const
char
*
about
=
"This sample uses Single-Shot Detector "
const
char
*
about
=
"This sample uses Single-Shot Detector "
"(https://arxiv.org/abs/1512.02325)"
"(https://arxiv.org/abs/1512.02325) "
"to detect objects on image
\n
"
;
// TODO: link
"to detect objects on camera/video/image.
\n
"
".caffemodel model's file is available here: "
"https://github.com/weiliu89/caffe/tree/ssd#models
\n
"
"Default network is 300x300 and 20-classes VOC.
\n
"
;
const
char
*
params
const
char
*
params
=
"{ help | false | print usage }"
=
"{ help | false | print usage }"
"{ proto | | model configuration }"
"{ proto | | model configuration }"
"{ model | | model weights }"
"{ model | | model weights }"
"{ image | | image for detection }"
"{ camera_device | 0 | camera device number}"
"{ video | | video or image for detection}"
"{ min_confidence | 0.5 | min confidence }"
;
"{ min_confidence | 0.5 | min confidence }"
;
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
...
@@ -57,7 +68,7 @@ int main(int argc, char** argv)
...
@@ -57,7 +68,7 @@ int main(int argc, char** argv)
if
(
parser
.
get
<
bool
>
(
"help"
))
if
(
parser
.
get
<
bool
>
(
"help"
))
{
{
std
::
cout
<<
about
<<
std
::
endl
;
cout
<<
about
<<
endl
;
parser
.
printMessage
();
parser
.
printMessage
();
return
0
;
return
0
;
}
}
...
@@ -79,58 +90,101 @@ int main(int argc, char** argv)
...
@@ -79,58 +90,101 @@ int main(int argc, char** argv)
exit
(
-
1
);
exit
(
-
1
);
}
}
cv
::
Mat
frame
=
cv
::
imread
(
parser
.
get
<
string
>
(
"image"
),
-
1
);
VideoCapture
cap
;
if
(
parser
.
get
<
String
>
(
"video"
).
empty
())
if
(
frame
.
channels
()
==
4
)
{
cvtColor
(
frame
,
frame
,
COLOR_BGRA2BGR
);
int
cameraDevice
=
parser
.
get
<
int
>
(
"camera_device"
);
//! [Prepare blob]
cap
=
VideoCapture
(
cameraDevice
);
Mat
preprocessedFrame
=
preprocess
(
frame
);
if
(
!
cap
.
isOpened
())
{
Mat
inputBlob
=
blobFromImage
(
preprocessedFrame
,
1.0
f
,
Size
(),
Scalar
(),
false
);
//Convert Mat to batch of images
cout
<<
"Couldn't find camera: "
<<
cameraDevice
<<
endl
;
//! [Prepare blob]
return
-
1
;
}
}
else
{
cap
.
open
(
parser
.
get
<
String
>
(
"video"
));
if
(
!
cap
.
isOpened
())
{
cout
<<
"Couldn't open image or video: "
<<
parser
.
get
<
String
>
(
"video"
)
<<
endl
;
return
-
1
;
}
}
//! [Set input blob]
for
(;;)
net
.
setInput
(
inputBlob
,
"data"
);
//set the network input
{
//! [Set input blob]
cv
::
Mat
frame
;
cap
>>
frame
;
// get a new frame from camera/video or read image
//! [Make forward pass]
if
(
frame
.
empty
())
Mat
detection
=
net
.
forward
(
"detection_out"
);
//compute output
{
//! [Make forward pass]
waitKey
();
break
;
}
Mat
detectionMat
(
detection
.
size
[
2
],
detection
.
size
[
3
],
CV_32F
,
detection
.
ptr
<
float
>
());
if
(
frame
.
channels
()
==
4
)
cvtColor
(
frame
,
frame
,
COLOR_BGRA2BGR
);
float
confidenceThreshold
=
parser
.
get
<
float
>
(
"min_confidence"
);
//! [Prepare blob]
for
(
int
i
=
0
;
i
<
detectionMat
.
rows
;
i
++
)
Mat
preprocessedFrame
=
preprocess
(
frame
);
{
float
confidence
=
detectionMat
.
at
<
float
>
(
i
,
2
);
if
(
confidence
>
confidenceThreshold
)
Mat
inputBlob
=
blobFromImage
(
preprocessedFrame
,
1.0
f
,
Size
(),
Scalar
(),
false
);
//Convert Mat to batch of images
{
//! [Prepare blob]
size_t
objectClass
=
(
size_t
)(
detectionMat
.
at
<
float
>
(
i
,
1
));
float
xLeftBottom
=
detectionMat
.
at
<
float
>
(
i
,
3
)
*
frame
.
cols
;
//! [Set input blob]
float
yLeftBottom
=
detectionMat
.
at
<
float
>
(
i
,
4
)
*
frame
.
rows
;
net
.
setInput
(
inputBlob
,
"data"
);
//set the network input
float
xRightTop
=
detectionMat
.
at
<
float
>
(
i
,
5
)
*
frame
.
cols
;
//! [Set input blob]
float
yRightTop
=
detectionMat
.
at
<
float
>
(
i
,
6
)
*
frame
.
rows
;
std
::
cout
<<
"Class: "
<<
objectClass
<<
std
::
endl
;
//! [Make forward pass]
std
::
cout
<<
"Confidence: "
<<
confidence
<<
std
::
endl
;
Mat
detection
=
net
.
forward
(
"detection_out"
);
//compute output
//! [Make forward pass]
std
::
cout
<<
" "
<<
xLeftBottom
vector
<
double
>
layersTimings
;
<<
" "
<<
yLeftBottom
double
freq
=
getTickFrequency
()
/
1000
;
<<
" "
<<
xRightTop
double
time
=
net
.
getPerfProfile
(
layersTimings
)
/
freq
;
<<
" "
<<
yRightTop
<<
std
::
endl
;
ostringstream
ss
;
ss
<<
"FPS: "
<<
1000
/
time
<<
" ; time: "
<<
time
<<
" ms"
;
putText
(
frame
,
ss
.
str
(),
Point
(
20
,
20
),
0
,
0.5
,
Scalar
(
0
,
0
,
255
));
Rect
object
((
int
)
xLeftBottom
,
(
int
)
yLeftBottom
,
Mat
detectionMat
(
detection
.
size
[
2
],
detection
.
size
[
3
],
CV_32F
,
detection
.
ptr
<
float
>
());
(
int
)(
xRightTop
-
xLeftBottom
),
(
int
)(
yRightTop
-
yLeftBottom
));
rectangle
(
frame
,
object
,
Scalar
(
0
,
255
,
0
));
float
confidenceThreshold
=
parser
.
get
<
float
>
(
"min_confidence"
);
for
(
int
i
=
0
;
i
<
detectionMat
.
rows
;
i
++
)
{
float
confidence
=
detectionMat
.
at
<
float
>
(
i
,
2
);
if
(
confidence
>
confidenceThreshold
)
{
size_t
objectClass
=
(
size_t
)(
detectionMat
.
at
<
float
>
(
i
,
1
));
int
xLeftBottom
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
3
)
*
frame
.
cols
);
int
yLeftBottom
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
4
)
*
frame
.
rows
);
int
xRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
5
)
*
frame
.
cols
);
int
yRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
6
)
*
frame
.
rows
);
ss
.
str
(
""
);
ss
<<
confidence
;
String
conf
(
ss
.
str
());
Rect
object
(
xLeftBottom
,
yLeftBottom
,
xRightTop
-
xLeftBottom
,
yRightTop
-
yLeftBottom
);
rectangle
(
frame
,
object
,
Scalar
(
0
,
255
,
0
));
String
label
=
String
(
classNames
[
objectClass
])
+
": "
+
conf
;
int
baseLine
=
0
;
Size
labelSize
=
getTextSize
(
label
,
FONT_HERSHEY_SIMPLEX
,
0.5
,
1
,
&
baseLine
);
rectangle
(
frame
,
Rect
(
Point
(
xLeftBottom
,
yLeftBottom
-
labelSize
.
height
),
Size
(
labelSize
.
width
,
labelSize
.
height
+
baseLine
)),
Scalar
(
255
,
255
,
255
),
CV_FILLED
);
putText
(
frame
,
label
,
Point
(
xLeftBottom
,
yLeftBottom
),
FONT_HERSHEY_SIMPLEX
,
0.5
,
Scalar
(
0
,
0
,
0
));
}
}
}
}
imshow
(
"detections"
,
frame
);
imshow
(
"detections"
,
frame
);
waitKey
();
if
(
waitKey
(
1
)
>=
0
)
break
;
}
return
0
;
return
0
;
}
// main
}
// main
samples/dnn/yolo_object_detection.cpp
View file @
639b8007
...
@@ -15,29 +15,36 @@ const size_t network_width = 416;
...
@@ -15,29 +15,36 @@ const size_t network_width = 416;
const
size_t
network_height
=
416
;
const
size_t
network_height
=
416
;
const
char
*
about
=
"This sample uses You only look once (YOLO)-Detector "
const
char
*
about
=
"This sample uses You only look once (YOLO)-Detector "
"(https://arxiv.org/abs/1612.08242)"
"(https://arxiv.org/abs/1612.08242) "
"to detect objects on image
\n
"
;
// TODO: link
"to detect objects on camera/video/image.
\n
"
"Models can be downloaded here: "
"https://pjreddie.com/darknet/yolo/
\n
"
"Default network is 416x416.
\n
"
"Class names can be downloaded here: "
"https://github.com/pjreddie/darknet/tree/master/data
\n
"
;
const
char
*
params
const
char
*
params
=
"{ help | false | print usage }"
=
"{ help | false | print usage }"
"{ cfg | | model configuration }"
"{ cfg | | model configuration }"
"{ model | | model weights }"
"{ model | | model weights }"
"{ image | | image for detection }"
"{ camera_device | 0 | camera device number}"
"{ min_confidence | 0.24 | min confidence }"
;
"{ video | | video or image for detection}"
"{ min_confidence | 0.24 | min confidence }"
"{ class_names | | class names }"
;
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
cv
::
CommandLineParser
parser
(
argc
,
argv
,
params
);
CommandLineParser
parser
(
argc
,
argv
,
params
);
if
(
parser
.
get
<
bool
>
(
"help"
))
if
(
parser
.
get
<
bool
>
(
"help"
))
{
{
std
::
cout
<<
about
<<
std
::
endl
;
cout
<<
about
<<
endl
;
parser
.
printMessage
();
parser
.
printMessage
();
return
0
;
return
0
;
}
}
String
modelConfiguration
=
parser
.
get
<
s
tring
>
(
"cfg"
);
String
modelConfiguration
=
parser
.
get
<
S
tring
>
(
"cfg"
);
String
modelBinary
=
parser
.
get
<
s
tring
>
(
"model"
);
String
modelBinary
=
parser
.
get
<
S
tring
>
(
"model"
);
//! [Initialize network]
//! [Initialize network]
dnn
::
Net
net
=
readNetFromDarknet
(
modelConfiguration
,
modelBinary
);
dnn
::
Net
net
=
readNetFromDarknet
(
modelConfiguration
,
modelBinary
);
...
@@ -53,65 +60,130 @@ int main(int argc, char** argv)
...
@@ -53,65 +60,130 @@ int main(int argc, char** argv)
exit
(
-
1
);
exit
(
-
1
);
}
}
cv
::
Mat
frame
=
cv
::
imread
(
parser
.
get
<
string
>
(
"image"
));
VideoCapture
cap
;
if
(
parser
.
get
<
String
>
(
"video"
).
empty
())
{
int
cameraDevice
=
parser
.
get
<
int
>
(
"camera_device"
);
cap
=
VideoCapture
(
cameraDevice
);
if
(
!
cap
.
isOpened
())
{
cout
<<
"Couldn't find camera: "
<<
cameraDevice
<<
endl
;
return
-
1
;
}
}
else
{
cap
.
open
(
parser
.
get
<
String
>
(
"video"
));
if
(
!
cap
.
isOpened
())
{
cout
<<
"Couldn't open image or video: "
<<
parser
.
get
<
String
>
(
"video"
)
<<
endl
;
return
-
1
;
}
}
vector
<
string
>
classNamesVec
;
ifstream
classNamesFile
(
parser
.
get
<
String
>
(
"class_names"
).
c_str
());
if
(
classNamesFile
.
is_open
())
{
string
className
=
""
;
while
(
classNamesFile
>>
className
)
classNamesVec
.
push_back
(
className
);
}
//! [Resizing without keeping aspect ratio]
for
(;;)
cv
::
Mat
resized
;
{
cv
::
resize
(
frame
,
resized
,
cv
::
Size
(
network_width
,
network_height
))
;
Mat
frame
;
//! [Resizing without keeping aspect ratio]
cap
>>
frame
;
// get a new frame from camera/video or read image
//! [Prepare blob]
if
(
frame
.
empty
())
Mat
inputBlob
=
blobFromImage
(
resized
,
1
/
255.
F
);
//Convert Mat to batch of images
{
//! [Prepare blob]
waitKey
();
break
;
}
//! [Set input blob]
if
(
frame
.
channels
()
==
4
)
net
.
setInput
(
inputBlob
,
"data"
);
//set the network input
cvtColor
(
frame
,
frame
,
COLOR_BGRA2BGR
);
//! [Set input blob]
//! [Make forward pass]
//! [Resizing without keeping aspect ratio]
cv
::
Mat
detectionMat
=
net
.
forward
(
"detection_out"
);
//compute output
Mat
resized
;
//! [Make forward pass]
resize
(
frame
,
resized
,
Size
(
network_width
,
network_height
));
//! [Resizing without keeping aspect ratio]
//! [Prepare blob]
Mat
inputBlob
=
blobFromImage
(
resized
,
1
/
255.
F
);
//Convert Mat to batch of images
//! [Prepare blob]
float
confidenceThreshold
=
parser
.
get
<
float
>
(
"min_confidence"
);
//! [Set input blob]
for
(
int
i
=
0
;
i
<
detectionMat
.
rows
;
i
++
)
net
.
setInput
(
inputBlob
,
"data"
);
//set the network input
{
//! [Set input blob]
const
int
probability_index
=
5
;
const
int
probability_size
=
detectionMat
.
cols
-
probability_index
;
//! [Make forward pass]
float
*
prob_array_ptr
=
&
detectionMat
.
at
<
float
>
(
i
,
probability_index
);
Mat
detectionMat
=
net
.
forward
(
"detection_out"
);
//compute output
//! [Make forward pass]
size_t
objectClass
=
std
::
max_element
(
prob_array_ptr
,
prob_array_ptr
+
probability_size
)
-
prob_array_ptr
;
vector
<
double
>
layersTimings
;
float
confidence
=
detectionMat
.
at
<
float
>
(
i
,
(
int
)
objectClass
+
probability_index
);
double
freq
=
getTickFrequency
()
/
1000
;
double
time
=
net
.
getPerfProfile
(
layersTimings
)
/
freq
;
ostringstream
ss
;
ss
<<
"FPS: "
<<
1000
/
time
<<
" ; time: "
<<
time
<<
" ms"
;
putText
(
frame
,
ss
.
str
(),
Point
(
20
,
20
),
0
,
0.5
,
Scalar
(
0
,
0
,
255
));
if
(
confidence
>
confidenceThreshold
)
float
confidenceThreshold
=
parser
.
get
<
float
>
(
"min_confidence"
);
for
(
int
i
=
0
;
i
<
detectionMat
.
rows
;
i
++
)
{
{
float
x
=
detectionMat
.
at
<
float
>
(
i
,
0
);
const
int
probability_index
=
5
;
float
y
=
detectionMat
.
at
<
float
>
(
i
,
1
);
const
int
probability_size
=
detectionMat
.
cols
-
probability_index
;
float
width
=
detectionMat
.
at
<
float
>
(
i
,
2
);
float
*
prob_array_ptr
=
&
detectionMat
.
at
<
float
>
(
i
,
probability_index
);
float
height
=
detectionMat
.
at
<
float
>
(
i
,
3
);
float
xLeftBottom
=
(
x
-
width
/
2
)
*
frame
.
cols
;
size_t
objectClass
=
max_element
(
prob_array_ptr
,
prob_array_ptr
+
probability_size
)
-
prob_array_ptr
;
float
yLeftBottom
=
(
y
-
height
/
2
)
*
frame
.
rows
;
float
confidence
=
detectionMat
.
at
<
float
>
(
i
,
(
int
)
objectClass
+
probability_index
);
float
xRightTop
=
(
x
+
width
/
2
)
*
frame
.
cols
;
float
yRightTop
=
(
y
+
height
/
2
)
*
frame
.
rows
;
if
(
confidence
>
confidenceThreshold
)
{
std
::
cout
<<
"Class: "
<<
objectClass
<<
std
::
endl
;
float
x
=
detectionMat
.
at
<
float
>
(
i
,
0
);
std
::
cout
<<
"Confidence: "
<<
confidence
<<
std
::
endl
;
float
y
=
detectionMat
.
at
<
float
>
(
i
,
1
);
float
width
=
detectionMat
.
at
<
float
>
(
i
,
2
);
std
::
cout
<<
" "
<<
xLeftBottom
float
height
=
detectionMat
.
at
<
float
>
(
i
,
3
);
<<
" "
<<
yLeftBottom
int
xLeftBottom
=
static_cast
<
int
>
((
x
-
width
/
2
)
*
frame
.
cols
);
<<
" "
<<
xRightTop
int
yLeftBottom
=
static_cast
<
int
>
((
y
-
height
/
2
)
*
frame
.
rows
);
<<
" "
<<
yRightTop
<<
std
::
endl
;
int
xRightTop
=
static_cast
<
int
>
((
x
+
width
/
2
)
*
frame
.
cols
);
int
yRightTop
=
static_cast
<
int
>
((
y
+
height
/
2
)
*
frame
.
rows
);
Rect
object
((
int
)
xLeftBottom
,
(
int
)
yLeftBottom
,
(
int
)(
xRightTop
-
xLeftBottom
),
Rect
object
(
xLeftBottom
,
yLeftBottom
,
(
int
)(
yRightTop
-
yLeftBottom
));
xRightTop
-
xLeftBottom
,
yRightTop
-
yLeftBottom
);
rectangle
(
frame
,
object
,
Scalar
(
0
,
255
,
0
));
rectangle
(
frame
,
object
,
Scalar
(
0
,
255
,
0
));
if
(
objectClass
<
classNamesVec
.
size
())
{
ss
.
str
(
""
);
ss
<<
confidence
;
String
conf
(
ss
.
str
());
String
label
=
String
(
classNamesVec
[
objectClass
])
+
": "
+
conf
;
int
baseLine
=
0
;
Size
labelSize
=
getTextSize
(
label
,
FONT_HERSHEY_SIMPLEX
,
0.5
,
1
,
&
baseLine
);
rectangle
(
frame
,
Rect
(
Point
(
xLeftBottom
,
yLeftBottom
-
labelSize
.
height
),
Size
(
labelSize
.
width
,
labelSize
.
height
+
baseLine
)),
Scalar
(
255
,
255
,
255
),
CV_FILLED
);
putText
(
frame
,
label
,
Point
(
xLeftBottom
,
yLeftBottom
),
FONT_HERSHEY_SIMPLEX
,
0.5
,
Scalar
(
0
,
0
,
0
));
}
else
{
cout
<<
"Class: "
<<
objectClass
<<
endl
;
cout
<<
"Confidence: "
<<
confidence
<<
endl
;
cout
<<
" "
<<
xLeftBottom
<<
" "
<<
yLeftBottom
<<
" "
<<
xRightTop
<<
" "
<<
yRightTop
<<
endl
;
}
}
}
}
}
imshow
(
"detections"
,
frame
);
imshow
(
"detections"
,
frame
);
waitKey
();
if
(
waitKey
(
1
)
>=
0
)
break
;
}
return
0
;
return
0
;
}
// main
}
// main
\ No newline at end of file
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