简体   繁体   English

将字符串格式、图像和 Label 的数据转换为列表 / numpy 数组

[英]Converting Data of string-formatted, Image and Label to list / numpy array

I am using imitation learning to teach a car how to drive in Gazebo.我正在使用模仿学习来教汽车如何在 Gazebo 中驾驶。 I am using an image from the camera feed on the car as the data, and the respective velocity command for that frame as its label.我使用来自汽车上的摄像头馈送的图像作为数据,并将该帧的相应速度命令用作其 label。

I saved all this data from a python script, into a text file, and it is formatted as "[[image]], dtype=uint8), ['velcmd1', ... 'velcmd6'], with lots more entries that follow in same format as the first, as seen below.我将 python 脚本中的所有这些数据保存到一个文本文件中,它的格式为“[[image]], dtype=uint8), ['velcmd1', ... 'velcmd6'],还有更多条目遵循与第一个相同的格式,如下所示。

"[[0, 0, 0, ..., 0, 0, 0],       [0, 0, 0, ..., 0, 0, 0],       ...,       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ['0.295245', '0.0', '0.0', '0.0', '0.0', '0.0']]", "[[0, 0, 0, ..., 0, 0, 0],

I need to convert this out of string formats and into two respective data types, one as an array that represents the image, and the other as a list that is the label.我需要将其从字符串格式转换为两种各自的数据类型,一种作为表示图像的数组,另一种作为列表,即 label。 I have been able to separate the two by doing some ugly string.split() and string.replace(), and I have been able to get the label into the format, with its type being printed below:我已经能够通过一些丑陋的 string.split() 和 string.replace() 将两者分开,并且我已经能够将 label 转换为格式,其类型打印在下面:

[0.295245, 0.0, 0.0, 0.0, 0.0, 0.0]
<class 'list'>

I did this by evaluating the string of the label with ast.literal_eval().我通过使用 ast.literal_eval() 评估 label 的字符串来做到这一点。

For the image, I am able to get it into the format:对于图像,我可以将其转换为以下格式:

[[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]]
<class 'str'>

However, it is still a string, and ast.literal_eval() raises a Malformed String ValueError.但是,它仍然是一个字符串,并且 ast.literal_eval() 会引发 Malformed String ValueError。 I tried both of the last solutions here Malformed String ValueError ast.literal_eval() with String representation of Tuple我在这里尝试了最后两种解决方案Malformed String ValueError ast.literal_eval() with String representation of Tuple

I instead tried to manually create an array then append the values to this array, which gives相反,我尝试手动创建一个数组,然后 append 将该数组的值提供给

['0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0', '..., 0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0', '0, 0, 0, ..., 0, 0, 0']
<class 'list'>

However, now the individual entries are strings, and not arrays themselves.但是,现在各个条目是字符串,而不是 arrays 本身。

When I have trained NN's in the past, the image data has been in the form:当我过去训练神经网络时,图像数据的形式是:

[[ 32  31  30 ... 100 101 103]
[ 30  30  30 ... 100 101 103]
[ 30  30  31 ... 101 101 102]
...
[ 34  34  32 ...  87  87  87]
[ 30  30  29 ... 100 100  98]
[ 30  29  30 ... 100  99 100]]
<class 'numpy.ndarray'>

How can I convert that original string of the image from the text file into this final form?如何将文本文件中的图像原始字符串转换为最终形式?

Note: Training data is not all 0's (black), it is just binarized, so entries shown are black.注意:训练数据并非全为 0(黑色),它只是被二值化,所以显示的条目是黑色的。

The solution I found for tracking the labels for my images: there are only 6 distinct labels, so instead of trying to save the image next to its label somewhere, I created six different folders for each label.我找到的用于跟踪图像标签的解决方案:只有 6 个不同的标签,因此我没有尝试将图像保存在 label 旁边的某个地方,而是为每个 label 创建了六个不同的文件夹。 Then if velcmd1 is the label for the outputted image, I put it in the folder with all the other images that will be labeled with velcmd1.然后,如果 velcmd1 是输出图像的 label,我将它与将用 velcmd1 标记的所有其他图像一起放入文件夹中。

I am doing this using:我正在使用:

cv.imwrite(path+uniqueidentifier, img)

This way there is no need to track a specific label for each image.这样就不需要为每个图像跟踪特定的 label。 They are just grouped together in six folders, so no more data loss by forcing images to be strings.它们只是在六个文件夹中组合在一起,因此不会因将图像强制为字符串而导致数据丢失。

Your solution works as each image has only one label.您的解决方案有效,因为每个图像只有一个 label。 If you need multiple labels per file, you would have to place the same image in multiple folders which can quickly eat up disk space.如果每个文件需要多个标签,则必须将相同的图像放在多个文件夹中,这会很快占用磁盘空间。

As an alternative, you could create a database that associates labels with the path of an image file.作为替代方案,您可以创建一个将标签与图像文件的路径相关联的数据库。 Then you can still save images directly as .png , .jpeg , etc. and have labels associated with them.然后,您仍然可以将图像直接保存为.png.jpeg等,并具有与之关联的标签。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM