Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
bc8b6040
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看板
未验证
提交
bc8b6040
编写于
2月 07, 2018
作者:
W
whs
提交者:
GitHub
2月 07, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #618 from shiwenguo/ctc_reader
add ctc reader
上级
2738ca10
911e0bb3
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
97 addition
and
0 deletion
+97
-0
fluid/ocr_recognition/ctc_reader.py
fluid/ocr_recognition/ctc_reader.py
+97
-0
未找到文件。
fluid/ocr_recognition/ctc_reader.py
0 → 100644
浏览文件 @
bc8b6040
import
os
import
cv2
import
numpy
as
np
from
PIL
import
Image
from
paddle.v2.image
import
load_image
class
DataGenerator
(
object
):
def
__init__
(
self
):
pass
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.
:type file_list: str
:param img_label_list: The path of the <image_name, label> file for training.
:type file_list: str
'''
img_label_lines
=
[]
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
():
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
def
test_reader
(
self
,
img_root_dir
,
img_label_list
):
'''
Reader interface for inference.
: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.
:type file_list: list
'''
def
reader
():
for
line
in
open
(
img_label_list
):
# 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'
)
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录