【问题标题】:How to convert inkml file to an image format如何将inkml文件转换为图像格式
【发布时间】:2019-12-11 17:01:57
【问题描述】:

我的数据集由手写文本的 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

【问题讨论】:

    标签: python image inkml


    【解决方案1】:

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

    【讨论】:

    • 你能分享一下代码吗?
    猜你喜欢
    • 2021-09-02
    • 2019-08-19
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2017-08-31
    • 2014-02-10
    • 2023-04-01
    相关资源
    最近更新 更多