首页 > 解决方案 > Google Colab:迭代此目录时,For Loop 拒绝按顺序进行

问题描述

我当然正在从事面部识别项目,并且不得不使用 Google Colaboratory。我已经运行了这段代码,它在我的 IDE(我使用 Pycharm)中运行良好,但现在我将它转移到 Colaboratory,我遇到了一个特定类型的问题。该程序应该通过一个文件夹/目录,其中包含它将使用的所有图像。但由于某种原因,for 循环不会从文件夹的开头开始,也不会直接跟随它。相反,它会转到文件夹中看似随机的位置,而不是从第一个文件到最后一个文件。这是在循环中打印的带有图像名称的代码:

# 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)

我已经安装了我的谷歌驱动器并将我的图像文件夹上传到那里并设置为作为 GPU 运行。我回到了我的 IDE,它在那里完美运行。这些软件包都已安装并运行良好。我尝试过移动文件夹,甚至将其全部上传到临时 VM,但仍然没有。我什至检查以确保它可以通过正常的 for 循环并且确实可以。我需要为 Colab 做些什么不同的事情吗?

标签: pythongoogle-colaboratory

解决方案


推荐阅读