0

I am certainly working on a facial recognition project and having to use Google Colaboratory with it. I have run this code and it works well in my IDE (I use Pycharm) but now that I transferred it over to Colaboratory, I faced a specific type of issue. The program is suppose to go through a folder/directory that has all the images that it will work with. But for some reason, the for loop will not start at the beginning of the folder nor follow it in a straight order. It would instead go to seemingly random places in the folder rather than first file to last file. Here is the code with the name of image being printed in the loop:

# import required packages
import os
import cv2
import dlib
import argparse
import time

# import the necessary packages
from google.colab.patches import cv2_imshow
from imutils import face_utils
from imutils.face_utils.helpers import FACIAL_LANDMARKS_5_IDXS, FACIAL_LANDMARKS_IDXS
import numpy as np
import imutils

prefix = 'gdrive/MyDrive/'
detector_path = os.path.join(prefix, 'mmod_human_face_detector.dat')
predictor_path = os.path.join(prefix, 'shape_predictor_68_face_landmarks.dat')

# init dlib face dec and landmark predictor
# cnn based face detector with weights
# shape predictor
cnn_face_detector = dlib.cnn_face_detection_model_v1(detector_path)
predictor = dlib.shape_predictor(predictor_path)
for images in os.listdir(prefix+"Dataset"):
  print(images)

I have already mounted my google drive and uploaded my folder of images in there and set to run as a GPU. I went back to my IDE and it runs perfectly there. The packages are all installed and running well. I have tried moving around the folder and even uploading it all to the temporary VM and still nothing. I even checked to make sure it can go through normal for loops and it does. Is there something different I have to do for Colab?

JJHV36
  • 1
  • 2
    The contents of a folder don't have an intrinsic order. If you want to process files in a certain order, you will need to sort the list of files. – larsks Jun 18 '21 at 23:21
  • You may want to use `glob` for the ordered loop-ing over directories: check [this topic](https://stackoverflow.com/questions/6773584/how-is-pythons-glob-glob-ordered) – ans Jun 21 '21 at 09:19

0 Answers0