0

Here is my code snippet:

from detectron2.utils.visualizer import ColorMode
from detectron2.structures import pairwise_iou
import random
import numpy
from numpy import savetxt
dataset_dicts = DatasetCatalog.get('/content/scaphoid/test')
for d in random.sample(dataset_dicts,20):    
    im = cv2.imread(d["file_name"])
    outputs = predictor(im)
    v = Visualizer(im[:, :, ::-1], metadata=microcontroller_metadata, scale=0.8)
    v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
    bb2 =(outputs["instances"].get_fields()["pred_boxes"].tensor.to("cpu").numpy())
    #numpy.savetxt("/content/output/bb2.csv",bb2, delimiter=',', header="xmin,ymin,xmax,ymax", comments="")

I want to store bb2 np array into a csv file. I tried using the commented statement but this only saves last value of bb2 and not all the values How can I do it? Can any one please help

1 Answers1

1

You wrote the numpy.savetxt statement inside the for loop, and you are not appending the array which is why it keeps overwriting the array and the csv file with the most recent values.

You can create an empty array:

final = np.array([])

And then after bb2=(outputs["instances"].get_fields(["pred_boxes"].tensor.to("cpu").numpy()) write the following inside the for loop:

final = np.append(final, bb2)

Finally outside of the for loop, you write the numpy.savetxt statement:

numpy.savetxt("/content/output/bb2.csv",final, delimiter=',', header="xmin,ymin,xmax,ymax", comments="")

Hopefully this should solve your problem!

Naman Doctor
  • 126
  • 4