简体   繁体   English

如何从带有图像注释的 CSV 文件中提取值并将它们附加到 Python 中 RetinaNet 的新 CSV 文件中?

[英]How can I extract values from a CSV file with image annotations and append them to a new CSV file for RetinaNet in Python?

I'm trying to convert a csv file from the VGG Image Annotator software into a csv file that can be used in RetinaNet.我正在尝试将 VGG Image Annotator 软件中的 csv 文件转换为可在 RetinaNet 中使用的 csv 文件。 The format I need for RetinaNet training data is: path/to/image.jpg,x1,y1,x2,y2,class_name .我需要 RetinaNet 训练数据的格式是: path/to/image.jpg,x1,y1,x2,y2,class_name This is an example of my CSV file from VIA: +=============+===========+==============+===========+========================================================+===+ |这是我来自 VIA 的 CSV 文件示例: +==============+============+============ ==+============+==================================== ======================+====+ | filename |文件名 | file_size |文件大小 | region_count | region_count | region_id | region_id | region_shape_attributes | region_shape_attributes | | | +=============+===========+==============+===========+========================================================+===+ | +============+============+==============+======== ====+============================================== ==========+====+ | img--30.png | img--30.png | 2331731 | 2331731 | 10 | 10 | 0 | 0 | {"name":"rect","x":65,"y":778,"width":108,"height":65} | {"name":"rect","x":65,"y":778,"width":108,"height":65} | | | +-------------+-----------+--------------+-----------+--------------------------------------------------------+---+ +------------+-----------+--------------+-------- ---+---------------------------------------------- ----------+---+

Basically, I need to pull the x, y, width, and height attributes from within the brackets and append them to a list.基本上,我需要从括号内提取x、y、宽度和高度属性并将它们附加到列表中。 This is my python code:这是我的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)

This code outputs:此代码输出:

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)

The region_shape_attributes column is a JSON string. region_shape_attributes列是一个 JSON 字符串。 You need to parse it in order to get the values it contains.您需要解析它以获取它包含的值。

Python has built-in JSON support: 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将新值附加/更新到现有 csv 文件的行中,从新的 csv 文件作为 python 或其他内容的新列 - How to append/update new values to the rows of a existing csv file from a new csv file as a new column in python using pandas or something else 如何从 Python 中的 CSV 文件中提取 JSON 对象? - How can I extract a JSON object from a CSV file in Python? 如何从CSV文件中提取数据列并将其定义为x和y变量,然后使用pylab在python中绘制它们? - How can I extract columns of data from a CSV file and define them as x and y variables, then plot them in python using pylab? 如何在python中将新行附加到csv文件 - How to append a new line to a csv file in python 如何在 Python 的 CSV 文件中 append 一个新值? - How to append a new value in a CSV file in Python? 如何为CSV文件中的每一列提取两个字符之间的子字符串,并将这些值复制到Python中的新列中? - How can I extract a substring between two characters for every row of a column in a CSV file and copy those values into a new column in Python? 如何将 CSV 文件附加到 Python 中的现有 zip 存档? - How can I append a CSV file to an existing zip archive in Python? 如何从 msg 文件中提取数据并将它们插入(附加)到 csv 文件中? - How to extract data from msg files and insert (append) them to csv file? 如何将不同子目录中的多个csv文件中的值提取到新的csv文件中? - How would I extract values out of multiple csv files that are in different subdirectories into a new csv file? 如何将CSV文件中的行中的值附加到python中的列表中? - How do I append values from a row in a CSV file into a list in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM