Home > Net >  Converting Data of string-formatted, Image and Label to list / numpy array
Converting Data of string-formatted, Image and Label to list / numpy array

Time:01-01

I am using imitation learning to teach a car how to drive in 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.

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.

"[[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. 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:

[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().

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. I tried both of the last solutions here 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

['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.

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.

CodePudding user response:

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. 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.

I am doing this using :

cv.imwrite(path uniqueidentifier, img)

This way there is no need to track a specific label for each image. They are just grouped together in six folders, so no more data loss by forcing images to be strings.

CodePudding user response:

Your solution works as each image has only one 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.

  • Related