I am trying to write img_id (single value) in first column of csv and image features array (f_descriptor_T) in remaining columns of csv file.
I have written following code:
path=/home/dir
listing = os.listdir(path)
for file in listing:
path_img_ext = os.path.join(path, file) #combine filename and extension
data_read = feature_io.ReadFromFile(path_img_ext) #read single features of single image in each iteration
base=os.path.basename(file) #Extract image_id from its name
os.path.splitext(file)
img_id = os.path.splitext(base)[0]
f_descriptor = data_read[2] #Extract particular array, from multiple arrays of features
feature_flat=f_descriptor.flatten() #Make flat array
f_descriptor_T = np.array(feature_flat).T #Transpose feature array to convert col data to row form.
with open("output_0.csv", "a") as f:
writer = csv.writer(f)
writer.writerow([img_id])
writer.writerow(f_descriptor_T)
Unfortunately, the output shows img_id on one row and f_descriptor_T on second row, with each iteration of loop, as shown in following screenshot: Output
But i want to combine both (variable and array) in single row.
Note: I have tried zip and concatenate to combine both, but failed.
The desired output should be like this: Desired OutputNot necessary to print column header, but just img_id and f_descriptor_T array in single row.
f_descriptor_Tis an array of three rows. What do you mean you want them in one row?csv.writerto write arrays. Experiment withnp.savetxtfor writing arrays. Better yet, study its code to see how it writes rows of an array directly (with outcsvmodule).