1

我的数据集由手写文本的inkml文件组成。我想将其转换为可用的图像格式来训练 CNN。python脚本会很有帮助。

我发现下面给出的方法是源代码

def get_traces_data(inkml_file_abs_path):

traces_data = []

tree = ET.parse(inkml_file_abs_path)
root = tree.getroot()
doc_namespace = "{http://www.w3.org/2003/InkML}"

'Stores traces_all with their corresponding id'
traces_all = [{'id': trace_tag.get('id'),
                'coords': [[round(float(axis_coord)) if float(axis_coord).is_integer() else round(float(axis_coord)) \
                                for axis_coord in coord[1:].split(' ')] if coord.startswith(' ') \
                            else [round(float(axis_coord)) if float(axis_coord).is_integer() else round(float(axis_coord)) \
                                for axis_coord in coord.split(' ')] \
                        for coord in (trace_tag.text).replace('\n', '').split(',')]} \
                        for trace_tag in root.findall(doc_namespace + 'trace')]

# print("before sort ", traces_all)
'Sort traces_all list by id to make searching for references faster'
traces_all.sort(key=lambda trace_dict: int(trace_dict['id']))
# print("after sort ", traces_all)
'Always 1st traceGroup is a redundant wrapper'
traceGroupWrapper = root.find(doc_namespace + 'traceGroup')

if traceGroupWrapper is not None:
    for traceGroup in traceGroupWrapper.findall(doc_namespace + 'traceGroup'):

        label = traceGroup.find(doc_namespace + 'annotation').text

        'traces of the current traceGroup'
        traces_curr = []
        for traceView in traceGroup.findall(doc_namespace + 'traceView'):

            'Id reference to specific trace tag corresponding to currently considered label'
            traceDataRef = int(traceView.get('traceDataRef'))

            'Each trace is represented by a list of coordinates to connect'
            single_trace = traces_all[traceDataRef]['coords']
            traces_curr.append(single_trace)


        traces_data.append({'label': label, 'trace_group': traces_curr})

else:
    'Consider Validation data that has no labels'
    [traces_data.append({'trace_group': [trace['coords']]}) for trace in traces_all]

return traces_data
4

1 回答 1

0

您可以考虑xml.etree.ElementTree在 Python 中使用来解析您的inkml文件并使用 OpenCV 的cv2.line方法连接点以绘制笔画。

于 2020-04-09T06:42:23.407 回答