0

Here is my python codes for constructing image pairs from a list of images then loop through them:

from itertools import combinations
import os

root = "/root/code/set1/"
folders = "images"
os.chdir(root+folders)
with open(os.path.join(root, root.split("/")[-1]+"_results.txt"), 'w') as f:
    
    
    metrics = ["cosine", "euclidean"]
    print("starting the looop")
    jresults = {}
    
    images = os.listdir(os.path.join(root, folders))
    images = [image for image in images if image.endswith(".jpg")]

    image_pairs = combinations(images, 2)

    print("number of pairs: {}".format(len(list(image_pairs)))) ## here it prints out correct numbers like 21325546
    for metric in metrics:
        print("metric: {}".format(metric))
        print("number of pairs: {}".format(len(list(image_pairs)))) ## HERE IT PRINTS 0 AND I COULDN'T FIGURE OUT WHY
        for img1, img2 in image_pairs:
            print("img1: {}, img2: {}".format(img1, img2)) ## HERE PRINTS NOTHING

I simply couldn't figure out why it disappeared and nothing is there to be looped through. Could anyone kindly help? Thanks so much!

shenglih
  • 879
  • 2
  • 8
  • 18

1 Answers1

3

You can make it a list to persist it:

image_pairs = list(combinations(images, 2))

combinations returns a generator, not a list in memory.

JacobIRR
  • 8,545
  • 8
  • 39
  • 68