From ca9ecbb3cab99ecccbc5286595034987214d0c09 Mon Sep 17 00:00:00 2001 From: "Samuel J. Woodward" Date: Mon, 6 Jan 2025 09:06:37 -0500 Subject: [PATCH] list_images now stores valid image and PIL exts in sets built ahead of time rather than on each iteration. exact_path is not created unless the file's ext is a valid image ext. --- deepface/commons/image_utils.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/deepface/commons/image_utils.py b/deepface/commons/image_utils.py index b72ce0b..f0facd6 100644 --- a/deepface/commons/image_utils.py +++ b/deepface/commons/image_utils.py @@ -23,18 +23,15 @@ def list_images(path: str) -> List[str]: images (list): list of exact image paths """ images = [] + image_exts = {".jpg", ".jpeg", ".png"} + pil_exts = {"jpeg", "png"} for r, _, f in os.walk(path): for file in f: - exact_path = os.path.join(r, file) - - ext_lower = os.path.splitext(exact_path)[-1].lower() - - if ext_lower not in {".jpg", ".jpeg", ".png"}: - continue - - with Image.open(exact_path) as img: # lazy - if img.format.lower() in {"jpeg", "png"}: - images.append(exact_path) + if os.path.splitext(file)[1].lower() in image_exts: + exact_path = os.path.join(r, file) + with Image.open(exact_path) as img: # lazy + if img.format.lower() in pil_exts: + images.append(exact_path) return images