Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
evsuits
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
evsuits
Commits
b5bb0ed3
提交
b5bb0ed3
authored
12月 26, 2019
作者:
blu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
object detection
上级
8fab9d1d
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
128 行增加
和
317 行删除
+128
-317
Makefile
opencv-yolo/Makefile
+6
-3
main.cpp
opencv-yolo/main.cpp
+34
-271
yolo.hpp
opencv-yolo/yolo.hpp
+88
-43
没有找到文件。
opencv-yolo/Makefile
浏览文件 @
b5bb0ed3
...
@@ -13,5 +13,8 @@ all: $(PROG) $(PROG2)
...
@@ -13,5 +13,8 @@ all: $(PROG) $(PROG2)
$(PROG)
:
$(SRCS)
$(PROG)
:
$(SRCS)
$(CC)
$(CFLAGS)
-o
$(PROG)
$(SRCS)
$(LIBS)
$(CC)
$(CFLAGS)
-o
$(PROG)
$(SRCS)
$(LIBS)
$(PROG2)
:
$(SRCS2)
$(PROG2)
:
$(SRCS2) yolo.hpp
$(CC)
$(CFLAGS)
-o
$(PROG2)
$(SRCS2)
$(LIBS)
-I
../opencv-motion-detect/inc
-I
../opencv-motion-detect/vendor/include
$(CC)
$(CFLAGS)
-o
$(PROG2)
$(SRCS2)
$(LIBS)
-I
../opencv-motion-detect/inc
-I
../opencv-motion-detect/vendor/include
\ No newline at end of file
clean
:
rm
-fr
main yolo
\ No newline at end of file
opencv-yolo/main.cpp
浏览文件 @
b5bb0ed3
#include <fstream>
#include "yolo.hpp"
#include "clipp.h"
#include <sstream>
#include <sstream>
#include <iostream>
#include <tuple>
#include "fs.h"
#include "spdlog/spdlog.h"
#ifdef _MY_HEADERS_
using
namespace
clipp
;
#include <opencv2/core/types_c.h>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#else
#include <opencv2/core/types_c.h>
#include <opencv2/opencv.hpp>
#endif
using
namespace
cv
;
using
namespace
dnn
;
using
namespace
std
;
using
namespace
std
;
class
YoloDectect
{
int
main
(
int
argc
,
char
*
argv
[]){
private
:
bool
bHumanOnly
=
true
;
// Initialize the parameters
float
fConfident
=
0.1
;
const
string
selfId
=
"YoloDetector"
;
bool
bVerbose
=
false
;
float
confThreshold
=
0.5
;
// Confidence threshold
bool
help
=
false
;
float
nmsThreshold
=
0.4
;
// Non-maximum suppression threshold
bool
bCont
=
false
;
int
inpWidth
=
416
;
// Width of network's input image
string
sInput
,
sOutput
=
"detect.jpg"
;
int
inpHeight
=
416
;
// Height of network's input image
string
modelPath
=
"."
;
vector
<
string
>
classes
;
Net
net
;
auto
cli
=
(
Mat
blob
;
value
(
"input path"
,
sInput
),
VideoCapture
cap
;
option
(
"-cl"
).
set
(
fConfident
).
doc
(
"confidence level of detection, default: 0.1"
),
VideoWriter
video
;
option
(
"-vv"
,
"--debug"
).
set
(
bVerbose
).
doc
(
"verbose prints"
),
bool
bOutputIsImg
=
false
;
option
(
"-human"
,
"--human-only"
).
set
(
bHumanOnly
).
doc
(
"detect only human object"
),
string
outFileBase
;
option
(
"-c"
,
"--config-path"
).
set
(
modelPath
).
doc
(
"model and configuration path"
),
bool
cmdStop
=
false
;
option
(
"-h"
,
"--help"
).
set
(
help
).
doc
(
"print this help info"
),
option
(
"-o"
,
"--output"
).
set
(
sOutput
).
doc
(
"output, eg: a.jpg; b.avi"
),
// Get the names of the output layers
option
(
"-r"
,
"--continue"
).
set
(
bCont
).
doc
(
"continue detection, default: false"
)
vector
<
String
>
getOutputsNames
(
const
Net
&
net
)
);
{
static
vector
<
String
>
names
;
if
(
!
parse
(
argc
,
argv
,
cli
)
||
help
)
{
if
(
names
.
empty
())
{
stringstream
s
;
//Get the indices of the output layers, i.e. the layers with unconnected outputs
s
<<
make_man_page
(
cli
,
argv
[
0
]);
vector
<
int
>
outLayers
=
net
.
getUnconnectedOutLayers
();
spdlog
::
info
(
s
.
str
());
exit
(
0
);
//get the names of all the layers in the network
vector
<
String
>
layersNames
=
net
.
getLayerNames
();
// Get the names of the output layers in names
names
.
resize
(
outLayers
.
size
());
for
(
size_t
i
=
0
;
i
<
outLayers
.
size
();
++
i
)
names
[
i
]
=
layersNames
[
outLayers
[
i
]
-
1
];
}
return
names
;
}
// draw the predicted bounding box
void
drawPred
(
int
classId
,
float
conf
,
int
left
,
int
top
,
int
right
,
int
bottom
,
Mat
&
frame
)
{
// draw a rectangle displaying the bounding box
rectangle
(
frame
,
Point
(
left
,
top
),
Point
(
right
,
bottom
),
Scalar
(
255
,
178
,
50
),
3
);
//get the label for the class name and its confidence
string
label
=
format
(
"%.2f"
,
conf
);
if
(
!
classes
.
empty
())
{
CV_Assert
(
classId
<
(
int
)
classes
.
size
());
label
=
classes
[
classId
]
+
":"
+
label
;
}
// display the label at the top of the bounding box
int
baseLine
;
Size
labelSize
=
getTextSize
(
label
,
FONT_HERSHEY_SIMPLEX
,
0.5
,
1
,
&
baseLine
);
top
=
max
(
top
,
labelSize
.
height
);
rectangle
(
frame
,
Point
(
left
,
top
-
round
(
1.5
*
labelSize
.
height
)),
Point
(
left
+
round
(
1.5
*
labelSize
.
width
),
top
+
baseLine
),
Scalar
(
255
,
255
,
255
),
FILLED
);
putText
(
frame
,
label
,
Point
(
left
,
top
),
FONT_HERSHEY_SIMPLEX
,
0.75
,
Scalar
(
0
,
0
,
0
),
1
);
}
// post process
vector
<
tuple
<
string
,
double
,
Rect
>>
postprocess
(
Mat
&
frame
,
const
vector
<
Mat
>&
outs
)
{
vector
<
int
>
classIds
;
vector
<
float
>
confidences
;
vector
<
Rect
>
boxes
;
for
(
size_t
i
=
0
;
i
<
outs
.
size
();
++
i
)
{
// Scan through all the bounding boxes output from the network and keep only the
// ones with high confidence scores. Assign the box's class label as the class
// with the highest score for the box.
float
*
data
=
(
float
*
)
outs
[
i
].
data
;
for
(
int
j
=
0
;
j
<
outs
[
i
].
rows
;
++
j
,
data
+=
outs
[
i
].
cols
)
{
Mat
scores
=
outs
[
i
].
row
(
j
).
colRange
(
5
,
outs
[
i
].
cols
);
Point
classIdPoint
;
double
confidence
;
// Get the value and location of the maximum score
minMaxLoc
(
scores
,
0
,
&
confidence
,
0
,
&
classIdPoint
);
if
(
confidence
>
confThreshold
)
{
int
centerX
=
(
int
)(
data
[
0
]
*
frame
.
cols
);
int
centerY
=
(
int
)(
data
[
1
]
*
frame
.
rows
);
int
width
=
(
int
)(
data
[
2
]
*
frame
.
cols
);
int
height
=
(
int
)(
data
[
3
]
*
frame
.
rows
);
int
left
=
centerX
-
width
/
2
;
int
top
=
centerY
-
height
/
2
;
classIds
.
push_back
(
classIdPoint
.
x
);
confidences
.
push_back
((
float
)
confidence
);
boxes
.
push_back
(
Rect
(
left
,
top
,
width
,
height
));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with lower confidences
vector
<
int
>
indices
;
NMSBoxes
(
boxes
,
confidences
,
confThreshold
,
nmsThreshold
,
indices
);
vector
<
tuple
<
string
,
double
,
Rect
>>
ret
;
for
(
size_t
i
=
0
;
i
<
indices
.
size
();
++
i
)
{
int
idx
=
indices
[
i
];
Rect
box
=
boxes
[
idx
];
ret
.
push_back
(
tuple
<
string
,
double
,
Rect
>
(
classes
[
classIds
[
idx
]],
confidences
[
idx
],
box
));
drawPred
(
classIds
[
idx
],
confidences
[
idx
],
box
.
x
,
box
.
y
,
box
.
x
+
box
.
width
,
box
.
y
+
box
.
height
,
frame
);
}
return
ret
;
}
}
if
(
bVerbose
)
{
//
spdlog
::
set_level
(
spdlog
::
level
::
debug
);
protected
:
//
public
:
typedef
int
(
*
callback
)(
vector
<
tuple
<
string
,
double
,
Rect
>>&
,
Mat
);
YoloDectect
(
string
path
=
""
)
{
if
(
path
.
empty
())
{
path
=
"."
;
}
// Load names of classes
string
classesFile
=
path
+
"/coco.names"
;
// Give the configuration and weight files for the model
String
modCfg
=
path
+
"/yolov3-tiny.cfg"
;
String
modWeights
=
path
+
"/yolov3-tiny.weights"
;
if
(
!
fs
::
exists
(
classesFile
)
||
!
fs
::
exists
(
modCfg
)
||
!
fs
::
exists
(
modWeights
))
{
spdlog
::
error
(
"{} failed to load configration files"
,
selfId
);
exit
(
1
);
}
ifstream
ifs
(
classesFile
.
c_str
());
string
line
;
while
(
getline
(
ifs
,
line
))
{
classes
.
push_back
(
line
);
}
// Load the network
net
=
readNetFromDarknet
(
modCfg
,
modWeights
);
net
.
setPreferableBackend
(
DNN_BACKEND_OPENCV
);
net
.
setPreferableTarget
(
DNN_TARGET_CPU
);
spdlog
::
info
(
"{} inited"
,
selfId
);
}
}
vector
<
tuple
<
string
,
double
,
Rect
>>
process
(
Mat
&
inFrame
,
Mat
&
outFrame
)
YoloDectect
detector
(
modelPath
,
bHumanOnly
,
fConfident
,
bCont
);
{
detector
.
process
(
sInput
,
sOutput
);
if
(
inFrame
.
empty
())
{
}
return
vector
<
tuple
<
string
,
double
,
Rect
>>
();
\ No newline at end of file
}
// Create a 4D blob from a frame.
blobFromImage
(
inFrame
,
blob
,
1
/
255.0
,
cvSize
(
inpWidth
,
inpHeight
),
Scalar
(
0
,
0
,
0
),
true
,
false
);
//Sets the input to the network
net
.
setInput
(
blob
);
// Runs the forward pass to get output of the output layers
vector
<
Mat
>
outs
;
net
.
forward
(
outs
,
getOutputsNames
(
net
));
// Remove the bounding boxes with low confidence
auto
ret
=
postprocess
(
inFrame
,
outs
);
// The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
vector
<
double
>
layersTimes
;
double
freq
=
getTickFrequency
()
/
1000
;
double
t
=
net
.
getPerfProfile
(
layersTimes
)
/
freq
;
spdlog
::
info
(
"{} infer time: {} ms"
,
selfId
,
t
);
inFrame
.
convertTo
(
outFrame
,
CV_8U
);
return
ret
;
}
int
process
(
string
inVideoUri
,
string
outFile
=
"processed.jpg"
,
callback
cb
=
nullptr
)
{
if
(
inVideoUri
.
empty
())
{
inVideoUri
=
"0"
;
}
if
(
!
cap
.
open
(
inVideoUri
))
{
spdlog
::
error
(
"{} failed to open input video {}"
,
selfId
,
inVideoUri
);
return
-
1
;
}
ghc
::
filesystem
::
path
p
(
outFile
);
auto
dir
=
p
.
parent_path
();
if
((
outFile
.
substr
(
outFile
.
find_last_of
(
"."
)
+
1
)
==
"jpg"
))
{
bOutputIsImg
=
true
;
outFileBase
=
string
(
dir
/
p
.
stem
());
spdlog
::
info
(
"{} outFileBase {}"
,
selfId
,
outFileBase
);
}
else
{
bOutputIsImg
=
false
;
if
(
!
video
.
open
(
outFile
,
VideoWriter
::
fourcc
(
'M'
,
'J'
,
'P'
,
'G'
),
28
,
Size
(
cap
.
get
(
CAP_PROP_FRAME_WIDTH
),
cap
.
get
(
CAP_PROP_FRAME_HEIGHT
))))
{
spdlog
::
error
(
"{} failed to open output video {}"
,
selfId
,
outFile
);
return
-
1
;
}
}
spdlog
::
info
(
"{} try to process video {} to {}"
,
selfId
,
inVideoUri
,
outFile
);
long
frameCnt
=
0
;
long
detCnt
=
0
,
skipCnt
=
0
;
Mat
frame
,
outFrame
;
while
(
waitKey
(
1
)
<
0
)
{
// get frame from the video
if
(
cmdStop
)
{
break
;
}
if
(
!
cap
.
read
(
frame
))
{
spdlog
::
error
(
"{} failed to read frame from {}"
,
selfId
,
inVideoUri
);
break
;
}
frameCnt
++
;
// Stop the program if reached end of video
if
(
frame
.
empty
())
{
continue
;
}
vector
<
tuple
<
string
,
double
,
Rect
>>
ret
=
process
(
frame
,
outFrame
);
if
(
cb
==
nullptr
)
{
if
(
ret
.
size
()
==
0
&&
bOutputIsImg
)
{
// no detection
if
(
skipCnt
%
100
==
0
)
{
spdlog
::
info
(
"{} no valid object detected skipped frame count {}"
,
selfId
,
skipCnt
);
}
skipCnt
++
;
continue
;
}
if
(
bOutputIsImg
)
{
string
ofname
=
outFileBase
+
to_string
(
detCnt
)
+
".jpg"
;
imwrite
(
ofname
,
outFrame
);
detCnt
++
;
}
else
{
video
.
write
(
outFrame
);
}
}
else
{
cb
(
ret
,
outFrame
);
}
}
cap
.
release
();
if
(
!
bOutputIsImg
)
video
.
release
();
return
0
;
}
};
int
main
(
int
argc
,
char
**
argv
)
{
YoloDectect
det
;
det
.
process
(
"rtsp://admin:ZQEAAI@192.168.0.101:554/h264/ch1/main/av_stream"
,
"a.avi"
);
return
0
;
}
opencv-yolo/yolo.hpp
浏览文件 @
b5bb0ed3
...
@@ -28,8 +28,8 @@ public:
...
@@ -28,8 +28,8 @@ public:
private
:
private
:
// Initialize the parameters
// Initialize the parameters
const
string
selfId
=
"YoloDetector"
;
const
string
selfId
=
"YoloDetector"
;
float
confThreshold
=
0.
5
;
// Confidence threshold
float
confThreshold
=
0.
1
;
// Confidence threshold
float
nmsThreshold
=
0.
4
;
// Non-maximum suppression threshold
float
nmsThreshold
=
0.
2
;
// Non-maximum suppression threshold
int
inpWidth
=
416
;
// Width of network's input image
int
inpWidth
=
416
;
// Width of network's input image
int
inpHeight
=
416
;
// Height of network's input image
int
inpHeight
=
416
;
// Height of network's input image
vector
<
string
>
classes
;
vector
<
string
>
classes
;
...
@@ -38,10 +38,14 @@ private:
...
@@ -38,10 +38,14 @@ private:
VideoCapture
cap
;
VideoCapture
cap
;
VideoWriter
video
;
VideoWriter
video
;
bool
bOutputIsImg
=
false
;
bool
bOutputIsImg
=
false
;
bool
bInputIsImage
=
true
;
string
outFileBase
;
string
outFileBase
;
bool
cmdStop
=
false
;
bool
cmdStop
=
false
;
unsigned
int
wrapNum
=
0
;
unsigned
int
wrapNum
=
0
;
unsigned
int
numLogSkip
=
0
;
unsigned
int
numLogSkip
=
0
;
bool
bHumanOnly
=
false
;
bool
bContinue
=
true
;
int
cameNo
=
-
1
;
// Get the names of the output layers
// Get the names of the output layers
vector
<
String
>
getOutputsNames
(
const
Net
&
net
)
vector
<
String
>
getOutputsNames
(
const
Net
&
net
)
...
@@ -109,6 +113,12 @@ private:
...
@@ -109,6 +113,12 @@ private:
int
left
=
centerX
-
width
/
2
;
int
left
=
centerX
-
width
/
2
;
int
top
=
centerY
-
height
/
2
;
int
top
=
centerY
-
height
/
2
;
if
(
bHumanOnly
){
if
(
classes
[
classIdPoint
.
x
]
!=
"person"
){
continue
;
}
}
classIds
.
push_back
(
classIdPoint
.
x
);
classIds
.
push_back
(
classIdPoint
.
x
);
confidences
.
push_back
((
float
)
confidence
);
confidences
.
push_back
((
float
)
confidence
);
boxes
.
push_back
(
Rect
(
left
,
top
,
width
,
height
));
boxes
.
push_back
(
Rect
(
left
,
top
,
width
,
height
));
...
@@ -137,12 +147,17 @@ protected:
...
@@ -137,12 +147,17 @@ protected:
//
//
public
:
public
:
typedef
int
(
*
callback
)(
vector
<
tuple
<
string
,
double
,
Rect
>>&
,
Mat
);
typedef
int
(
*
callback
)(
vector
<
tuple
<
string
,
double
,
Rect
>>&
,
Mat
);
YoloDectect
(
string
path
=
"."
,
unsigned
int
_wrapNum
=
10
,
unsigned
int
_numLogSkip
=
380
)
YoloDectect
(
string
path
=
"."
,
bool
_humanOnly
=
false
,
float
confThresh
=
0.1
,
bool
_bContinue
=
true
,
unsigned
int
_wrapNum
=
10
,
unsigned
int
_numLogSkip
=
380
)
{
{
if
(
path
.
empty
())
{
if
(
path
.
empty
())
{
path
=
"."
;
path
=
"."
;
}
}
bHumanOnly
=
_humanOnly
;
bContinue
=
_bContinue
;
confThreshold
=
confThresh
;
wrapNum
=
_wrapNum
;
wrapNum
=
_wrapNum
;
numLogSkip
=
_numLogSkip
;
numLogSkip
=
_numLogSkip
;
...
@@ -167,7 +182,7 @@ public:
...
@@ -167,7 +182,7 @@ public:
net
=
readNetFromDarknet
(
modCfg
,
modWeights
);
net
=
readNetFromDarknet
(
modCfg
,
modWeights
);
net
.
setPreferableBackend
(
DNN_BACKEND_OPENCV
);
net
.
setPreferableBackend
(
DNN_BACKEND_OPENCV
);
net
.
setPreferableTarget
(
DNN_TARGET_CPU
);
net
.
setPreferableTarget
(
DNN_TARGET_CPU
);
spdlog
::
info
(
"{} inited"
,
selfId
);
spdlog
::
debug
(
"{} inited"
,
selfId
);
}
}
vector
<
tuple
<
string
,
double
,
Rect
>>
process
(
Mat
&
inFrame
,
Mat
*
pOutFrame
,
bool
bModify
=
false
)
vector
<
tuple
<
string
,
double
,
Rect
>>
process
(
Mat
&
inFrame
,
Mat
*
pOutFrame
,
bool
bModify
=
false
)
...
@@ -194,7 +209,7 @@ public:
...
@@ -194,7 +209,7 @@ public:
if
(
numLogSkip
==
0
||
numFrameProcessed
%
numLogSkip
==
0
)
{
if
(
numLogSkip
==
0
||
numFrameProcessed
%
numLogSkip
==
0
)
{
double
freq
=
getTickFrequency
()
/
1000
;
double
freq
=
getTickFrequency
()
/
1000
;
double
t
=
net
.
getPerfProfile
(
layersTimes
)
/
freq
;
double
t
=
net
.
getPerfProfile
(
layersTimes
)
/
freq
;
spdlog
::
info
(
"{} infer time: {} ms"
,
selfId
,
t
);
spdlog
::
debug
(
"{} infer time: {} ms"
,
selfId
,
t
);
}
}
if
(
pOutFrame
!=
nullptr
){
if
(
pOutFrame
!=
nullptr
){
inFrame
.
convertTo
(
*
pOutFrame
,
CV_8U
);
inFrame
.
convertTo
(
*
pOutFrame
,
CV_8U
);
...
@@ -204,15 +219,27 @@ public:
...
@@ -204,15 +219,27 @@ public:
return
ret
;
return
ret
;
}
}
int
process
(
string
inVideoUri
,
string
outFile
=
"processed.jpg"
,
bool
bHumanExit
=
false
,
callback
cb
=
nullptr
)
int
process
(
string
inVideoUri
,
string
outFile
=
"processed.jpg"
,
callback
cb
=
nullptr
)
{
{
if
(
inVideoUri
.
empty
())
{
if
(
inVideoUri
.
empty
())
{
inVideoUri
=
"0"
;
inVideoUri
=
"0"
;
}
}
if
(
!
cap
.
open
(
inVideoUri
))
{
try
{
spdlog
::
error
(
"{} failed to open input video {}"
,
selfId
,
inVideoUri
);
if
(
inVideoUri
.
substr
(
inVideoUri
.
find_last_of
(
"."
)
+
1
)
==
"mp4"
||
(
cameNo
=
stoi
(
inVideoUri
))
>=
0
)
{
return
-
1
;
bInputIsImage
=
false
;
}
}
catch
(
Exception
&
e
)
{
}
if
(
!
bInputIsImage
)
{
if
((
cameNo
==
-
1
&&
!
cap
.
open
(
inVideoUri
))
||
(
cameNo
!=
-
1
&&
!
cap
.
open
(
cameNo
)))
{
spdlog
::
error
(
"{} failed to open input video {}"
,
selfId
,
inVideoUri
);
exit
(
1
);
}
}
}
ghc
::
filesystem
::
path
p
(
outFile
);
ghc
::
filesystem
::
path
p
(
outFile
);
...
@@ -221,9 +248,14 @@ public:
...
@@ -221,9 +248,14 @@ public:
if
((
outFile
.
substr
(
outFile
.
find_last_of
(
"."
)
+
1
)
==
"jpg"
))
{
if
((
outFile
.
substr
(
outFile
.
find_last_of
(
"."
)
+
1
)
==
"jpg"
))
{
bOutputIsImg
=
true
;
bOutputIsImg
=
true
;
outFileBase
=
string
(
dir
/
p
.
stem
());
outFileBase
=
string
(
dir
/
p
.
stem
());
spdlog
::
info
(
"{} outFileBase {}"
,
selfId
,
outFileBase
);
spdlog
::
debug
(
"{} outFileBase {}"
,
selfId
,
outFileBase
);
}
}
else
{
else
{
if
(
bInputIsImage
)
{
spdlog
::
error
(
"{} can't output image {} as video {}, invalid params combination"
,
selfId
,
inVideoUri
,
outFile
);
exit
(
1
);
}
bOutputIsImg
=
false
;
bOutputIsImg
=
false
;
if
(
!
video
.
open
(
outFile
,
VideoWriter
::
fourcc
(
'M'
,
'J'
,
'P'
,
'G'
),
28
,
Size
(
cap
.
get
(
CAP_PROP_FRAME_WIDTH
),
cap
.
get
(
CAP_PROP_FRAME_HEIGHT
))))
{
if
(
!
video
.
open
(
outFile
,
VideoWriter
::
fourcc
(
'M'
,
'J'
,
'P'
,
'G'
),
28
,
Size
(
cap
.
get
(
CAP_PROP_FRAME_WIDTH
),
cap
.
get
(
CAP_PROP_FRAME_HEIGHT
))))
{
spdlog
::
error
(
"{} failed to open output video {}"
,
selfId
,
outFile
);
spdlog
::
error
(
"{} failed to open output video {}"
,
selfId
,
outFile
);
...
@@ -231,7 +263,7 @@ public:
...
@@ -231,7 +263,7 @@ public:
}
}
}
}
spdlog
::
info
(
"{} try to process video {} to {}"
,
selfId
,
inVideoUri
,
outFile
);
spdlog
::
debug
(
"{} try to process video {} to {}"
,
selfId
,
inVideoUri
,
outFile
);
unsigned
long
frameCnt
=
0
;
unsigned
long
frameCnt
=
0
;
unsigned
long
detCnt
=
0
,
skipCnt
=
0
;
unsigned
long
detCnt
=
0
,
skipCnt
=
0
;
...
@@ -241,23 +273,32 @@ public:
...
@@ -241,23 +273,32 @@ public:
if
(
cmdStop
)
{
if
(
cmdStop
)
{
break
;
break
;
}
}
if
(
!
cap
.
read
(
frame
))
{
spdlog
::
info
(
"{} done reading frame from {}"
,
selfId
,
inVideoUri
);
break
;
}
frameCnt
++
;
if
(
frameCnt
%
100
==
0
)
spdlog
::
info
(
"framecnt {}"
,
frameCnt
);
if
(
frameCnt
%
30
!=
0
){
if
(
bInputIsImage
){
continue
;
frame
=
imread
(
inVideoUri
);
if
(
!
frame
.
data
){
spdlog
::
error
(
"{} failed to read image {}"
,
selfId
,
inVideoUri
);
exit
(
1
);
}
cmdStop
=
true
;
}
}
else
{
if
(
!
cap
.
read
(
frame
))
{
break
;
}
// Stop the program if reached end of video
frameCnt
++
;
if
(
frame
.
empty
())
{
if
(
frameCnt
%
100
==
0
)
continue
;
spdlog
::
debug
(
"framecnt {}"
,
frameCnt
);
if
(
frameCnt
%
30
!=
0
){
continue
;
}
// Stop the program if reached end of video
if
(
frame
.
empty
())
{
continue
;
}
}
}
vector
<
tuple
<
string
,
double
,
Rect
>>
ret
=
process
(
frame
,
&
outFrame
,
true
);
vector
<
tuple
<
string
,
double
,
Rect
>>
ret
=
process
(
frame
,
&
outFrame
,
true
);
...
@@ -265,31 +306,34 @@ public:
...
@@ -265,31 +306,34 @@ public:
if
(
ret
.
size
()
==
0
&&
bOutputIsImg
)
{
if
(
ret
.
size
()
==
0
&&
bOutputIsImg
)
{
// no detection
// no detection
if
(
numLogSkip
==
0
||
skipCnt
%
numLogSkip
==
0
)
{
if
(
numLogSkip
==
0
||
skipCnt
%
numLogSkip
==
0
)
{
spdlog
::
info
(
"{} no valid object detected skipped frame count {}"
,
selfId
,
skipCnt
);
spdlog
::
debug
(
"{} no valid object detected skipped frame count {}"
,
selfId
,
skipCnt
);
}
}
skipCnt
++
;
skipCnt
++
;
continue
;
continue
;
}
}
if
(
bHumanExit
){
for
(
auto
&
[
s
,
c
,
r
]
:
ret
)
{
if
(
s
==
"person"
){
string
ofname
=
outFileBase
+
"_person.jpg"
;
imwrite
(
ofname
,
outFrame
);
spdlog
::
info
(
"found human {} x: {}, y: {}, w: {}, h: {}"
,
c
,
r
.
x
,
r
.
y
,
r
.
width
,
r
.
height
);
cmdStop
=
true
;
break
;
}
}
}
if
(
bOutputIsImg
)
{
if
(
bOutputIsImg
)
{
if
(
wrapNum
>
0
)
{
if
(
bHumanOnly
){
detCnt
=
detCnt
%
wrapNum
;
for
(
auto
&
[
s
,
c
,
r
]
:
ret
)
{
if
(
s
==
"person"
){
auto
ms
=
chrono
::
duration_cast
<
chrono
::
milliseconds
>
(
chrono
::
system_clock
::
now
().
time_since_epoch
()).
count
();
string
ofname
=
outFileBase
+
"_person_"
+
to_string
(
ms
)
+
".jpg"
;
imwrite
(
ofname
,
outFrame
);
spdlog
::
info
(
"found human {} x: {}, y: {}, w: {}, h: {}"
,
c
,
r
.
x
,
r
.
y
,
r
.
width
,
r
.
height
);
if
(
!
bContinue
){
cmdStop
=
true
;
break
;
}
}
}
}
else
{
if
(
wrapNum
>
0
)
{
detCnt
=
detCnt
%
wrapNum
;
}
string
ofname
=
outFileBase
+
to_string
(
detCnt
)
+
".jpg"
;
imwrite
(
ofname
,
outFrame
);
detCnt
++
;
}
}
string
ofname
=
outFileBase
+
to_string
(
detCnt
)
+
".jpg"
;
imwrite
(
ofname
,
outFrame
);
detCnt
++
;
}
}
else
{
else
{
video
.
write
(
outFrame
);
video
.
write
(
outFrame
);
...
@@ -299,6 +343,7 @@ public:
...
@@ -299,6 +343,7 @@ public:
}
}
}
}
spdlog
::
info
(
"{} done processing {}"
,
selfId
,
inVideoUri
);
cap
.
release
();
cap
.
release
();
if
(
!
bOutputIsImg
)
video
.
release
();
if
(
!
bOutputIsImg
)
video
.
release
();
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论