Hi. I am trying to load the dataset, I want to get the filenames sorted as 1,2,3..., But I am getting them as 1, 10,100,1000.... as you can see in the images? How should I get it done?
filenames = glob.glob("/content/path/train/*.png")
filenames.sort()
images = [cv2.imread(img) for img in filenames]
Well, that IS alphabetical order... If you know the names are all numeric, then you can supply a key
function to .sort()
that uses the ordering you want.
def bynumber(fn):
return int(os.path.basename(fn)[:-4])
...
filenames.sort( key=bynumber )
Alternatively:
filenames.sort( key=lambda fn: int(os.path.basename(fn)[:-4]) )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.