【问题标题】:How can I extract values from a CSV file with image annotations and append them to a new CSV file for RetinaNet in Python?如何从带有图像注释的 CSV 文件中提取值并将它们附加到 Python 中 RetinaNet 的新 CSV 文件中?
【发布时间】:2020-06-25 14:21:53
【问题描述】:

我正在尝试将 VGG Image Annotator 软件中的 csv 文件转换为可在 RetinaNet 中使用的 csv 文件。我需要的 RetinaNet 训练数据格式是:path/to/image.jpg,x1,y1,x2,y2,class_name。这是我来自 VIA 的 CSV 文件的示例: +==============+============+==============+======== ===+================================================ ==========+===+ |文件名 |文件大小 | region_count | region_id | region_shape_attributes | | +==============+============+==============+======== ===+================================================ ==========+===+ | img--30.png | 2331731 | 10 | 0 | {"name":"rect","x":65,"y":778,"width":108,"height":65} | | +-------------+------------+--------------+-------- ---+------------------------------------------------ ----------+---+

基本上,我需要从括号中提取 x、y、宽度和高度 属性并将它们附加到列表中。这是我的python代码:

import csv

via_path = 'data/tiled/via.csv'

image_annotations = []

with open(via_path, "r") as f:
    reader = csv.reader(f, delimiter=",")
    for line in reader: 
        if '#' in line[0][0]:
            # bypassing comments in csv
            continue
        filename = line[1][2:-2]
        # strip brackets, split and get only the values we care about, then convert all the string to int 
        top_left_x, top_left_y, width, height = list(map(int,list(map(float, line[4].strip('][').split(',')[1:]))))

        if width == 0 or height == 0:
            continue

        # move from top left and width/height to x and y values
        if top_left_x < 0:
            top_left_x = 1
        if top_left_y < 0:
            top_left_y = 1
        x1 = top_left_x
        x2 = top_left_x + width
        y1 = top_left_y
        y2 = top_left_y + height 

        # TODO didn't add names this time since it is all one class
        name = "bird"

        # create the csv row
        new_row = []
        new_row.append(filename)
        new_row.append(x1)
        new_row.append(y1)
        new_row.append(x2)
        new_row.append(y2)
        new_row.append(name)

        image_annotations.append(new_row)

此代码输出:

ValueError
---> top_left_x, top_left_y, width, height = list(map(int,list(map(float, line[4].strip('][').split(',')[1:]))))
ValueError: not enough values to unpack (expected 4, got 0)

【问题讨论】:

    标签: python csv deep-learning conv-neural-network retinanet


    【解决方案1】:

    region_shape_attributes 列是一个 JSON 字符串。您需要对其进行解析以获取其中包含的值。

    Python 具有内置的 JSON 支持:

    import json
    
    # ... open CSV file, for each record ...
    
        shape = json.parse(line[4])
    
        top_left_x = shape['x']
        top_left_y = shape['y']
        # etc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 2016-10-29
      • 2020-12-21
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多