问题
I am trying to create bounding box using dimensions mentioned in the detection annotation in this dataset (I am using the polyterrasse one) for some reason the following code works for 25 frames and then suddenly it gives me an error :
IndexError: invalid index to scalar variable.
frame_path=glob.glob("path/to/pointcloud/folder/*.ezd")
bbox_path= glob.glob("path/to/detection/annotation/folder/*.ezd.bbox")
bbox=[]
cbox_dim=[]
for i in range(len(bbox_path)):
#convert the frame into pcd format and then load via PCL
bbox=np.loadtxt(bbox_path[i], dtype=np.float32) # load tracklets for the frame
if bbox.size==0:
continue
cbox_dim=np.asarray(bbox)
pc= pcl.PointCloud() #create pointcloud
pc.from_array(obj) #load frame into the point cloud
clipper=pc.make_cropbox()
for j in range(len(cbox_dim)):
tx = cbox_dim[j][0] #Error occurs: Invalid index to scalar variable
ty = cbox_dim[j][1]
tz = cbox_dim[j][2]
#similarly set rotation and dimensions
What are the possible reasons for this?
回答1:
I downloaded the data, unpacked it, checked it and it turned out that file polyterrasse026.ezd.bbox
has only 1 row of data:
1.718750 5.066964 -0.327395 0.693458 0.684387 1.325830 0 0 0 0
That is why bbox_path[26]
is 1D array. This is the reason you got the error.
EDIT.
To verify if bbox
is 2D array you may use, for example, bbox.ndim == 2
. bbox.size
shows the number of elements in the array not the number of dimensions.
The nature of your problem is that np.loadtxt()
returns 1D array if file contains only one row of data. You may handle this problem like this:
if bbox.ndim == 1:
bbox = np.array([bbox])
来源:https://stackoverflow.com/questions/52368296/index-error-invalid-index-to-scalar-variable