-3
  • List item

I am a newbie to python. I wanted to know Instead of asking the user to give input file with a path, how can I automatically select input files one after another the from a folder. i.e, it should pick the first image file from the folder, do some processing, then pick the 2nd file, then 3rd file... and so on till all the files in that folder have been processed and do action when a condition from a function which is called is satisfied.?

I am trying compare_images:

def compare_images(img1, img2):
    # normalize to compensate for exposure difference
    img1 = to_grayscale(imread(img1).astype(float))
    img2 = to_grayscale(imread(img2).astype(float))

    img1 = normalize(img1)
    img2 = normalize(img2)
    # calculate the difference and its norms
    diff = img1 - img2  # elementwise for scipy arrays
    m_norm = sum(abs(diff))  # Manhattan norm
    s = m_norm/img1.size
    return s

This is where I'm calling the compare_images function.But this throws an error. It simply runs and stops without producing any output even on the console or throws error saying unable to find file even when it exists. I feel I'm going wrong in my approach. Help.

path=os.getcwd()
folder1 = os.listdir(path)
folder2 = os.path.join(path,"cd\\")

for filename1 in folder1:
     for filename2 in os.listdir(folder2):
        if filename1.endswith(".jpg"):
            s = compare_images(filename1,filename2)
            print(s)
            if s > 10:
                shutil.copy(filename1,folder2)

Please rectify me as to where I'm going wrong. How to copy files only when a condition is met and that condition is drawn from another function?

  • https://docs.python.org/3/library/pathlib.html#pathlib.Path.cwd – Natecat Mar 22 '18 at 19:12
  • https://docs.python.org/3/library/pathlib.html#pathlib.Path.iterdir These two methods might be useful to you – Natecat Mar 22 '18 at 19:13
  • @Natecat, I want to have it in a loop . Could you please elaborate. Like in C, we increment pointers i++, j++ etc, then do some processing,how to do like that in py. – Mueez Siraj Mar 22 '18 at 19:14
  • https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory (I don't have enough rep to add this as a comment) – bzier Mar 22 '18 at 19:16
  • @bzier, if you are able to understand my question. like how sorting algorithms work, we have 2 poonters (i, j) i is pointing at one position, j at another. do the comparison, then increment either i or j and again from that position of pointers, we do the comparison. Similar to that, I wanted with image files in python – Mueez Siraj Mar 22 '18 at 19:19
  • @MueezSiraj As I mentioned in my comment below, I believe you should split out the new addition into a separate question. However, you mentioned that you encounter an error, but haven't provided the error. It is difficult to help without knowing what the error is. One thing I notice is that you reference `filename2` for comparison, but that doesn't seem to be declared anywhere. – bzier Mar 23 '18 at 18:01
  • @bzier, details are added. – Mueez Siraj Mar 23 '18 at 20:21
  • @MueezSiraj Let's back up a bit. What are you actually trying to accomplish here? The question is still somewhat chaotic and unclear. It looks to me like you are attempting to copy images from one directory to another, only if a similar image isn't already present in the target directory. Is that correct? – bzier Mar 23 '18 at 21:08
  • @bzier I actually want to get the count of people in a video or webcam. I am capturing the detected faces in a folder. but since 1 face many images will be captured I thought I will take 1 face and if it isn't there in another folder then copy it. Then next image will be compared and then so on. Finally if I give the length of the folder2 as output then I will get the count – Mueez Siraj Mar 24 '18 at 04:09
  • Basically I want to have only 1 image of a person even when 100s of his face images are captured so that I get the count of actual number of faces detected – Mueez Siraj Mar 24 '18 at 04:11
  • @bzier, for the count logic which is working I am tracking each face and increment count,but this will not compare if the person had previously been detected or not – Mueez Siraj Mar 24 '18 at 04:13

2 Answers2

1

Take a look at the link I suggested: How can I iterate over files in a given directory?

Adapted from that answer:

directory = "/some/directory/with/images/"

for filename in os.listdir(directory):
    if filename.endswith(".png") or filename.endswith(".jpg"): 
        # do image processing for the file here
        # instead of just printing the filename
        print(filename)
bzier
  • 445
  • 3
  • 11
  • I have updated the question. Please look into this. You are'nt getting me – Mueez Siraj Mar 23 '18 at 16:49
  • please help me out – Mueez Siraj Mar 23 '18 at 17:12
  • @MueezSiraj Now you have changed the question quite a bit. Originally it was asking how to select files from a directory without user input. Now you are asking about copying files under certain conditions. If you have a new question, it should be asked separately instead of appended to the end of this one. – bzier Mar 23 '18 at 17:59
  • yes initially I wanted to ask a separate question but stackoverflow isn't allowing me to do so as I have already asked one recently – Mueez Siraj Mar 23 '18 at 18:15
  • That was the reason I appended , please help with this one pls – Mueez Siraj Mar 23 '18 at 18:16
0

Based on the changes to the original question, and the clarifying comments below it, let me attempt another answer.

Assuming that your compare_images function works as expected (I cannot verify this), the following code should do what you are looking for. This checks if all of the images in the target folder are above the comparison threshold. If so, the file needs to be copied (it is a different image). If not, there's at least one similar-enough image, so the current one is skipped.

Note that performance may suffer for a large number of images. Depending on the complexity of the compare_images computations, and the number of images it needs to process, this may not be the most efficient solution.

# [...other code... (e.g. imports, etc)]

def compare_images(img1, img2):
    # normalize to compensate for exposure difference
    img1 = to_grayscale(imread(img1).astype(float))
    img2 = to_grayscale(imread(img2).astype(float))

    img1 = normalize(img1)
    img2 = normalize(img2)
    # calculate the difference and its norms
    diff = img1 - img2  # elementwise for scipy arrays
    m_norm = sum(abs(diff))  # Manhattan norm
    s = m_norm/img1.size
    return s

def above_threshold(img1, img2):
    s = compare_images(img1, img2)
    return s > 10

def process_files():
    folder1 = os.getcwd()
    folder2 = os.path.join(folder1, "cd")
    print("Folder1: " + folder1)
    print("Folder2: " + folder2)

    for filename1 in os.listdir(folder1):
        print("File: " + filename1)
        if filename1.endswith(".png"):
            if all(above_threshold(filename1, filename2) for filename2 in os.listdir(folder2)):
                print("  Copying (similar image was not found)")
                shutil.copy(filename1, folder2)
            else:
                print("  Skipping (found similar image)")
        else:
            print("  Skipping (not a png file)")
bzier
  • 445
  • 3
  • 11
  • Thank you so much for your help. I can't thank enough – Mueez Siraj Mar 24 '18 at 13:40
  • ,https://stackoverflow.com/questions/49473984/how-do-i-automate-this-process-of-capturing-detected-faces-and-adding-to-the-dat – Mueez Siraj Mar 25 '18 at 10:30
  • Assist in that question in the link. https://stackoverflow.com/questions/49473984/how-do-i-automate-this-process-of-capturing-detected-faces-and-adding-to-the-dat – Mueez Siraj Mar 25 '18 at 10:31