Home > Blockchain >  How to preserve order in python while comparing images from 2 different directories?
How to preserve order in python while comparing images from 2 different directories?

Time:03-06

I was comparing images from 2 different directories. I write code but the sequence of comparing is this

{ 0,1,10,11,12,...,19,2,20,21,..} not like {0,1,2,3,...,9,10,11,12,...}

L1 = os.listdir("D:\\image_dir_1\\")

image_list_1 = list()
image_list_2 = list()
final_i = list()
final_j = list()
dirs = -1 

for i in L1:
    dirs = dirs   1
    img1 = cv2.imread("D:\\image_dir_1\\"  i )
    L2 = os.listdir("D:\\image_dir_2\\" str(dirs)  '\\')
    for j in L2:
        img2 = cv2.imread("D:\\image_dir_2\\"   str(dirs)  '\\'  j )
        image_list_1.append(FUN_1(img1 , img2))
        image_list_2.append(FUN_2(img1 , img2))
        final_i.append(i)
        final_j.append(j)

    filei = pd.DataFrame(final_i,columns=['Col_1'])
    filej = pd.DataFrame(final_j,columns=['Col_2'])

    frame_1 = pd.DataFrame(image_list_1,columns=['c1'])
    frame_2 = pd.DataFrame(Image_list_2,columns=['c2'])
    
    final_value = pd.concat([filei, filej, frame_1,frame_2],axis=1)

final_value.to_csv('spreadsheet.csv',index=None)

CodePudding user response:

You have to sort the L1 list:

L1 = sorted(os.listdir("D:\\image_dir_1\\"), key=lambda str: int(str[:-4].strip()))

The issue is that without sorting them by their actual int value, you have the files names sorted lexicographically.

  • Related