首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从带有图像注释的CSV文件中提取值,并将它们附加到新的RetinaNet文件中?

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

Stack Overflow用户
提问于 2020-03-13 14:55:17
回答 1查看 794关注 0票数 0

我正在尝试将软件中的csv文件转换为一个可以在RetinaNet中使用的csv文件。RetinaNet培训数据所需的格式是:path/to/Image.jpg、x1、y1、x2、y2、class_name。这是我的CSV文件的一个例子,它来自于VIA:+=============+===========+==============+===========+========================================================+===+、file_size、filename、file_size、region_count、region_id、region_shape_attributes、、、img 30.png、2331731、10、10、10,{"name":"rect","x":65,"y":778,“宽度”:108,“高度”:65}\x{e76f} +-------------+-----------+--------------+-----------+--------------------------------------------------------+---+

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

代码语言:javascript
复制
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)

该代码输出:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-03-13 15:47:29

region_shape_attributes列是一个JSON字符串。您需要解析它,以获得它包含的值。

Python内置了JSON支持:

代码语言:javascript
复制
import json

# ... open CSV file, for each record ...

    shape = json.parse(line[4])

    top_left_x = shape['x']
    top_left_y = shape['y']
    # etc
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60672648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档