Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openvinotoolkit
mmaction2
提交
79f2fb63
M
mmaction2
项目概览
openvinotoolkit
/
mmaction2
大约 1 年 前同步成功
通知
2
Star
5
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mmaction2
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
79f2fb63
编写于
6月 28, 2020
作者:
D
duanhaodong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revise
上级
0ae7e8ec
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
45 addition
and
41 deletion
+45
-41
tools/gen_flow.py
tools/gen_flow.py
+45
-41
未找到文件。
tools/gen_flow.py
浏览文件 @
79f2fb63
...
...
@@ -5,44 +5,45 @@ import cv2
import
numpy
as
np
def
FlowToI
mg
(
raw_flow
,
bound
=
20.
):
"""Convert flow to gray image
def
flow_to_i
mg
(
raw_flow
,
bound
=
20.
):
"""Convert flow to gray image
.
Args:
raw_flow (np.
array float): estimated flow, the shape is [w, h]
bound (float):
bound for the flow-to-image normalization
raw_flow (np.
ndarray[float]): Estimated flow with the shape (w, h).
bound (float):
Bound for the flow-to-image normalization. Default: 20.
return
:
flow (np.array uint8): normalized flow
Returns
:
Flow (np.ndarray[uint8]): Normalized flow.
"""
flow
=
raw_flow
flow
[
flow
>
bound
]
=
bound
flow
[
flow
<
-
bound
]
=
-
bound
flow
=
np
.
clip
(
raw_flow
,
-
bound
,
bound
)
flow
+=
bound
flow
*=
(
255
/
float
(
2
*
bound
))
flow
=
flow
.
astype
(
np
.
uint8
)
return
flow
def
gen
_flow
(
frames
,
algo
=
'tvl1'
):
"""Estimate flow
given frames
def
gen
erate_flow
(
frames
,
method
=
'tvl1'
):
"""Estimate flow
with given frames.
Args:
frames (list): list of rgb frames
algo (str): which algorithm to use, in ['tvl1', 'farneback']
return:
flow (list): list of flow
frames (list[np.ndarray[uint8]]): List of rgb frames, with shape
(w, h, 3).
method (str): Use which method to generate flow. Options are 'tvl1'
and 'farneback'. Default: 'tvl1'.
Returns:
flow (list[np.ndarray[float]]): The result list of np.ndarray[float],
with shape (w, h, 2)
"""
assert
algo
in
[
'tvl1'
,
'farneback'
]
gray_frames
=
[
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_RGB2GRAY
)
for
im
in
frames
]
assert
method
in
[
'tvl1'
,
'farneback'
]
gray_frames
=
[
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_RGB2GRAY
)
for
frame
in
frames
]
if
algo
==
'tvl1'
:
if
method
==
'tvl1'
:
tvl1
=
cv2
.
optflow
.
DualTVL1OpticalFlow_create
()
def
op
(
x
,
y
):
return
tvl1
.
calc
(
x
,
y
,
None
)
elif
algo
==
'farneback'
:
elif
method
==
'farneback'
:
def
op
(
x
,
y
):
return
cv2
.
calcOpticalFlowFarneback
(
x
,
y
,
None
,
0.5
,
3
,
15
,
3
,
5
,
...
...
@@ -60,19 +61,21 @@ def extract_dense_flow(path,
bound
=
20.
,
write_image
=
False
,
start_idx
=
0
,
algo
=
'tvl1'
):
method
=
'tvl1'
):
"""Extract dense flow given video or frames, save them as gray-scale images
Args:
path (str):
l
ocation of the video or frames. If use a video as input,
path (str):
L
ocation of the video or frames. If use a video as input,
pass the location of the video. If use frames as input,
pass the template of frames, like '/path/{:05d}.jpg'.
dest (str): the directory to store the extracted flow images
bound (float): bound for the flow-to-image normalization
write_image (bool): whether to save the extracted images to dest
start_idx (int): denotes the starting index if use frames as input, the
the first image is path.format(start_idx)
algo (str): which algorithm to use, in ['tvl1', 'farneback']
dest (str): The directory to store the extracted flow images.
bound (float): Bound for the flow-to-image normalization. Default: 20.
write_image (bool): Whether to save the extracted images to dest.
Default: False.
start_idx (int): The starting frame index if use frames as input, the
first image is path.format(start_idx). Default: 0.
method (str): Use which method to generate flow. Options are 'tvl1'
and 'farneback'. Default: 'tvl1'.
"""
if
osp
.
exists
(
path
):
...
...
@@ -84,16 +87,16 @@ def extract_dense_flow(path,
flag
,
f
=
vid
.
read
()
else
:
idx
=
start_idx
im_name
=
path
.
format
(
idx
)
while
osp
.
exists
(
im_name
):
frames
.
append
(
cv2
.
imread
(
im_name
))
im
g
_name
=
path
.
format
(
idx
)
while
osp
.
exists
(
im
g
_name
):
frames
.
append
(
cv2
.
imread
(
im
g
_name
))
idx
+=
1
im
_name
=
path
.
format
(
im_name
)
im
g_name
=
path
.
format
(
idx
)
flow
=
gen
_flow
(
frames
,
algo
=
algo
)
flow
=
gen
erate_flow
(
frames
,
method
=
method
)
flow_x
=
[
FlowToI
mg
(
x
[:,
:,
0
],
bound
)
for
x
in
flow
]
flow_y
=
[
FlowToI
mg
(
x
[:,
:,
1
],
bound
)
for
x
in
flow
]
flow_x
=
[
flow_to_i
mg
(
x
[:,
:,
0
],
bound
)
for
x
in
flow
]
flow_y
=
[
flow_to_i
mg
(
x
[:,
:,
1
],
bound
)
for
x
in
flow
]
if
not
osp
.
exists
(
dest
):
os
.
system
(
'mkdir -p '
+
dest
)
...
...
@@ -106,17 +109,18 @@ def extract_dense_flow(path,
for
ind
in
range
(
len
(
flow_y
))
]
for
imx
,
namex
in
zip
(
flow_x
,
flow_x_names
):
cv2
.
imwrite
(
namex
,
imx
)
for
imy
,
namey
in
zip
(
flow_y
,
flow_y_names
):
cv2
.
imwrite
(
namey
,
imy
)
num_frames
=
len
(
flow
)
for
i
in
range
(
num_frames
):
cv2
.
imwrite
(
flow_x
[
i
],
flow_x_names
[
i
])
cv2
.
imwrite
(
flow_y
[
i
],
flow_y_names
[
i
])
if
write_image
:
im_names
=
[
osp
.
join
(
dest
,
'img_{:05d}.jpg'
.
format
(
ind
))
for
ind
in
range
(
len
(
frames
))
]
for
im
,
name
in
zip
(
frames
,
im_names
):
cv2
.
imwrite
(
name
,
im
)
for
frame
,
name
in
zip
(
frames
,
im_names
):
cv2
.
imwrite
(
name
,
frame
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录