Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
68b61067
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
68b61067
编写于
2月 05, 2018
作者:
X
xiaohang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add ctc reader
上级
8c737c22
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
52 addition
and
30 deletion
+52
-30
fluid/ocr_recognition/ctc_reader.py
fluid/ocr_recognition/ctc_reader.py
+52
-30
未找到文件。
fluid/ocr_recognition/ctc_reader.py
浏览文件 @
68b61067
import
os
import
cv2
import
numpy
as
np
from
PIL
import
Image
from
paddle.v2.image
import
load_image
...
...
@@ -9,40 +10,64 @@ class DataGenerator(object):
def
__init__
(
self
):
pass
def
train_reader
(
self
,
img_root_dir
,
img_label_list
):
def
train_reader
(
self
,
img_root_dir
,
img_label_list
,
batchsize
):
'''
Reader interface for training.
:param img_root_dir: The root path of the image for training.
:param img_root_dir: The root path of the image for training.
:type file_list: str
:param img_label_list: The path of the <image_name, label> file for training.
:type file_list: str
'''
# sort by height, e.g. idx
img_label_lines
=
[]
for
line
in
open
(
img_label_list
):
# h, w, img_name, labels
items
=
line
.
split
(
' '
)
idx
=
"{:0>5d}"
.
format
(
int
(
items
[
0
]))
img_label_lines
.
append
(
idx
+
' '
+
line
)
img_label_lines
.
sort
()
if
batchsize
==
1
:
to_file
=
"tmp.txt"
cmd
=
"cat "
+
img_label_list
+
" | awk '{print $1,$2,$3,$4;}' | shuf > "
+
to_file
print
"cmd: "
+
cmd
os
.
system
(
cmd
)
print
"finish batch shuffle"
img_label_lines
=
open
(
to_file
,
'r'
).
readlines
()
else
:
to_file
=
"tmp.txt"
#cmd1: partial shuffle
cmd
=
"cat "
+
img_label_list
+
" | awk '{printf(
\"
%04d%.4f %s
\\
n
\"
, $1, rand(), $0)}' | sort | sed 1,$((1 + RANDOM % 100))d | "
#cmd2: batch merge and shuffle
cmd
+=
"awk '{printf $2
\"
\"
$3
\"
\"
$4
\"
\"
$5
\"
\"
; if(NR % "
+
str
(
batchsize
)
+
" == 0) print
\"\"
;}' | shuf | "
#cmd3: batch split
cmd
+=
"awk '{if(NF == "
+
str
(
batchsize
)
+
" * 4) {for(i = 0; i < "
+
str
(
batchsize
)
+
"; i++) print $(4*i+1)
\"
\"
$(4*i+2)
\"
\"
$(4*i+3)
\"
\"
$(4*i+4);}}' > "
+
to_file
print
"cmd: "
+
cmd
os
.
system
(
cmd
)
print
"finish batch shuffle"
img_label_lines
=
open
(
to_file
,
'r'
).
readlines
()
def
reader
():
for
line
in
img_label_lines
:
# h, w, img_name, labels
items
=
line
.
split
(
' '
)[
1
:]
assert
len
(
items
)
==
4
label
=
[
int
(
c
)
for
c
in
items
[
-
1
].
split
(
','
)]
img
=
load_image
(
os
.
path
.
join
(
img_root_dir
,
items
[
2
]))
img
=
np
.
transpose
(
img
,
(
2
,
0
,
1
))
#img = img[np.newaxis, ...]
yield
img
,
label
sizes
=
len
(
img_label_lines
)
/
batchsize
for
i
in
range
(
sizes
):
result
=
[]
sz
=
[
0
,
0
]
for
j
in
range
(
batchsize
):
line
=
img_label_lines
[
i
*
batchsize
+
j
]
# h, w, img_name, labels
items
=
line
.
split
(
' '
)
label
=
[
int
(
c
)
for
c
in
items
[
-
1
].
split
(
','
)]
img
=
Image
.
open
(
os
.
path
.
join
(
img_root_dir
,
items
[
2
])).
convert
(
'L'
)
#zhuanhuidu
if
j
==
0
:
sz
=
img
.
size
img
=
img
.
resize
((
sz
[
0
],
sz
[
1
]))
img
=
np
.
array
(
img
)
-
127.5
img
=
img
[
np
.
newaxis
,
...]
result
.
append
([
img
,
label
])
yield
result
return
reader
...
...
@@ -50,7 +75,7 @@ class DataGenerator(object):
'''
Reader interface for inference.
:param img_root_dir: The root path of the images for training.
:param img_root_dir: The root path of the images for training.
:type file_list: str
:param img_label_list: The path of the <image_name, label> file for testing.
...
...
@@ -62,14 +87,11 @@ class DataGenerator(object):
# h, w, img_name, labels
items
=
line
.
split
(
' '
)
assert
len
(
items
)
==
4
label
=
[
int
(
c
)
for
c
in
items
[
-
1
].
split
(
','
)]
img
=
load_image
(
os
.
path
.
join
(
img_root_dir
,
items
[
2
]))
img
=
np
.
transpose
(
img
,
(
2
,
0
,
1
))
#img = img[np.newaxis, ...]
img
=
Image
.
open
(
os
.
path
.
join
(
img_root_dir
,
items
[
2
])).
convert
(
'L'
)
img
=
np
.
array
(
img
)
-
127.5
img
=
img
[
np
.
newaxis
,
...]
yield
img
,
label
return
reader
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录