I'm filling a 3d array using python, each array element represent a pixel.
The values that I need to insert to the array are stored in a very large .txt file (56 millions lines, formatted as follows - x,y,z,r,g,b)
right now I:
init 3d array with zeros.
read the file line by line.
for each line taking only the first 3 elements (x,y,z).
calculate the array location[i,j] from the x and y
if array[i,j] equals zero --> insert line read from file
else skip to next file
for 56 million lines it takes me about 160 seconds
how can speed this up using python? (gpu is available)
array = np.zeros((height, width), dtype=np.float32)
with open(point_cloud_file) as pc_file:
while True:
line = pc_file.readline()
if not line:
break
nof_read_lines += 1
new_line = line.strip()
try:
x, y, z, _, _, _ = new_line.split(',')
except:
nof_skipped_lines += 1
continue
# insert to array
pixel_x = some calculation
pixel_y = some calculation
if 0 < pixel_x < width and 0 < pixel_y < height:
if array[int(pixel_y), int(pixel_x), 0] == 0:
array[int(pixel_y), int(pixel_x), :] = x, y, z
else:
nof_skipped_lines += 1 # pixel already filled with values