Skip to content Skip to sidebar Skip to footer

Python - Numpy 3d Array - Concatenate Issues

I have a txt file with 46 entries that looks like this - 2020-05-24T10:57:12.743606#[0.0, 0.0, 0.0653934553265572, 0.0, 1.0, 0.0] 2020-05-24T10:57:12.806380#[0.0, 0.0, 0.0, 0.0, 1.

Solution 1:

Here is an implementation with dummy data

collect = []
for i in range(46):

    #create dummy arrays, simulate list of 3 RGB images
    a = [np.zeros((70,320,3)) for b in range(3)]
    # a[0].shape: (70,320,3) #concatenate along axis 2
    b = np.concatenate(a, axis=2)
    # b.shape: (70,320,9)#create new axis in position zero
    b = b[np.newaxis, ...]
    # b.shape : (1,70,320,9)
    collect.append(b)

output = np.concatenate(collect, axis=0)

output.shape
(46, 70, 320, 9)

edit:

# IIUC:# left camera makes 70,320,3 at time t# right camera makes 70,320,3 at time t# center camera makes 70,320,3 at time t# these need to be concatenated to 70,320,9# if so, you can use a dictionary#initialise dict
collected_images = {}
for timepoint, row inenumerate(data):
    #at every timepoint, initialise dict entry
    collected_images[timepoint] = []
    for camera in ['center', 'left', 'right']:
        image = cv2.imread('path/to/image')
        collected_images[timepoint].append(image)

# now you have all images in a dictionary# to generate the array, you can

output = []
for key, val in collected_iamges.items():
    temp = np.concatenate(val, axis=2)
    output.append(temp[np.newaxis, ...])

output = np.concatenate(output, axis=0)

Solution 2:

After @warped's first answer, I figured out the output list from the text file was the problem. It was dumping all lines in one go. After several tries, I ended up using csv.reader which made things so much easier. After that just extended @warped's second answer and got the task done.

withopen('train.txt', 'r') as f:
    lines = f.readlines()
    data = csv.reader(lines, delimiter = "#")
    for count, index inenumerate(data):
        img_id = index[0]
        label = [float(item) for item in index[1][1:-1].split(",")]

Label solution from here -- Python - convert list of string to float - square braces and decimal point causing problems

After it was basically using the answer.

This link helped me choose csv reader -- Python not reading from a text file correctly?

Post a Comment for "Python - Numpy 3d Array - Concatenate Issues"