Update path handling

This commit is contained in:
kremnik 2024-08-12 03:10:20 +03:00
parent f4592141d8
commit 0bb94a1a11
25 changed files with 112 additions and 150 deletions

View File

@ -86,7 +86,7 @@
"source": [
"target_paths = [\"lfwe\", \"dataset\", \"outputs\", \"outputs/test\", \"results\"]\n",
"for target_path in target_paths:\n",
" if os.path.exists(target_path) != True:\n",
" if not os.path.exists(target_path):\n",
" os.mkdir(target_path)\n",
" print(f\"{target_path} is just created\")"
]
@ -132,7 +132,7 @@
" np.save(target_path, pairs)\n",
" np.save(labels_path, labels)\n",
"else:\n",
" if os.path.exists(pairs_touch) != True:\n",
" if not os.path.exists(pairs_touch):\n",
" # loading pairs takes some time. but if we extract these pairs as image, no need to load it anymore\n",
" pairs = np.load(target_path)\n",
" labels = np.load(labels_path) "
@ -165,17 +165,17 @@
" img1_target = f\"lfwe/test/{i}_1.jpg\"\n",
" img2_target = f\"lfwe/test/{i}_2.jpg\"\n",
" \n",
" if os.path.exists(img1_target) != True:\n",
" if not os.path.exists(img1_target):\n",
" img1 = pairs[i][0]\n",
" # plt.imsave(img1_target, img1/255) #works for my mac\n",
" plt.imsave(img1_target, img1) #works for my debian\n",
" \n",
" if os.path.exists(img2_target) != True:\n",
" if not os.path.exists(img2_target):\n",
" img2 = pairs[i][1]\n",
" # plt.imsave(img2_target, img2/255) #works for my mac\n",
" plt.imsave(img2_target, img2) #works for my debian\n",
" \n",
"if os.path.exists(pairs_touch) != True:\n",
"if not os.path.exists(pairs_touch):\n",
" open(pairs_touch,'a').close()"
]
},
@ -208,7 +208,7 @@
" alignment_text = \"aligned\" if align is True else \"unaligned\"\n",
" task = f\"{model_name}_{detector_backend}_{distance_metric}_{alignment_text}\"\n",
" output_file = f\"outputs/test/{task}.csv\"\n",
" if os.path.exists(output_file) is True:\n",
" if os.path.exists(output_file):\n",
" #print(f\"{output_file} is available already\")\n",
" continue\n",
" \n",

View File

@ -20,7 +20,7 @@ def download_external_file(file_name: str, exact_file_path: str, url: str) -> No
Returns:
None
"""
if os.path.exists(exact_file_path) is False:
if not os.path.exists(exact_file_path):
logger.info(f"Downloading MiniFASNetV2 weights to {exact_file_path}")
try:
gdown.download(url, exact_file_path, quiet=False)

View File

@ -1,5 +1,4 @@
import os
from pathlib import Path
from deepface.commons.logger import Logger
logger = Logger()
@ -13,16 +12,16 @@ def initialize_folder() -> None:
OSError: if the folder cannot be created.
"""
home = get_deepface_home()
deepface_home_path = home + "/.deepface"
weights_path = deepface_home_path + "/weights"
deepface_home_path = os.path.join(home, ".deepface")
weights_path = os.path.join(deepface_home_path, "weights")
if not os.path.exists(deepface_home_path):
os.makedirs(deepface_home_path, exist_ok=True)
logger.info(f"Directory {home}/.deepface created")
logger.info(f"Directory {deepface_home_path} has been created")
if not os.path.exists(weights_path):
os.makedirs(weights_path, exist_ok=True)
logger.info(f"Directory {home}/.deepface/weights created")
logger.info(f"Directory {weights_path} has been created")
def get_deepface_home() -> str:
@ -32,4 +31,4 @@ def get_deepface_home() -> str:
Returns:
str: the home directory.
"""
return str(os.getenv("DEEPFACE_HOME", default=str(Path.home())))
return str(os.getenv("DEEPFACE_HOME", default=os.path.expanduser("~")))

View File

@ -26,14 +26,13 @@ def list_images(path: str) -> List[str]:
for file in f:
exact_path = os.path.join(r, file)
_, ext = os.path.splitext(exact_path)
ext_lower = ext.lower()
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"]:
if img.format.lower() in {"jpeg", "png"}:
images.append(exact_path)
return images
@ -86,17 +85,17 @@ def load_image(img: Union[str, np.ndarray]) -> Tuple[np.ndarray, str]:
return load_image_from_base64(img), "base64 encoded string"
# The image is a url
if img.lower().startswith("http://") or img.lower().startswith("https://"):
if img.lower().startswith(("http://", "https://")):
return load_image_from_web(url=img), img
# The image is a path
if os.path.isfile(img) is not True:
if not os.path.isfile(img):
raise ValueError(f"Confirm that {img} exists")
# image must be a file on the system then
# image name must have english characters
if img.isascii() is False:
if not img.isascii():
raise ValueError(f"Input image must not have non-english characters - {img}")
img_obj_bgr = cv2.imread(img)
@ -125,8 +124,8 @@ def load_image_from_base64(uri: str) -> np.ndarray:
# content type is safer option than file extension
with Image.open(io.BytesIO(decoded_bytes)) as img:
file_type = img.format.lower()
if file_type not in ["jpeg", "png"]:
raise ValueError(f"input image can be jpg or png, but it is {file_type}")
if file_type not in {"jpeg", "png"}:
raise ValueError(f"Input image can be jpg or png, but it is {file_type}")
nparr = np.fromstring(decoded_bytes, np.uint8)
img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

View File

@ -67,14 +67,13 @@ def load_model(
# load weights
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/age_model_weights.h5")
if os.path.isfile(home + "/.deepface/weights/age_model_weights.h5") != True:
if not os.path.isfile(output):
logger.info("age_model_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/age_model_weights.h5"
gdown.download(url, output, quiet=False)
age_model.load_weights(home + "/.deepface/weights/age_model_weights.h5")
age_model.load_weights(output)
return age_model
@ -89,6 +88,6 @@ def find_apparent_age(age_predictions: np.ndarray) -> np.float64:
Returns:
apparent_age (float)
"""
output_indexes = np.array(list(range(0, 101)))
output_indexes = np.arange(0, 101)
apparent_age = np.sum(age_predictions * output_indexes)
return apparent_age

View File

@ -97,13 +97,12 @@ def load_model(
# ----------------------------
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/facial_expression_model_weights.h5")
if os.path.isfile(home + "/.deepface/weights/facial_expression_model_weights.h5") != True:
logger.info("facial_expression_model_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/facial_expression_model_weights.h5"
if not os.path.isfile(output):
logger.info("Facial_expression_model_weights.h5 will be downloaded...")
gdown.download(url, output, quiet=False)
model.load_weights(home + "/.deepface/weights/facial_expression_model_weights.h5")
model.load_weights(output)
return model

View File

@ -74,13 +74,12 @@ def load_model(
# load weights
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/gender_model_weights.h5")
if os.path.isfile(home + "/.deepface/weights/gender_model_weights.h5") != True:
if not os.path.isfile(output):
logger.info("gender_model_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/gender_model_weights.h5"
gdown.download(url, output, quiet=False)
gender_model.load_weights(home + "/.deepface/weights/gender_model_weights.h5")
gender_model.load_weights(output)
return gender_model

View File

@ -71,13 +71,12 @@ def load_model(
# load weights
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/race_model_single_batch.h5")
if os.path.isfile(home + "/.deepface/weights/race_model_single_batch.h5") != True:
if not os.path.isfile(output):
logger.info("race_model_single_batch.h5 will be downloaded...")
output = home + "/.deepface/weights/race_model_single_batch.h5"
gdown.download(url, output, quiet=False)
race_model.load_weights(home + "/.deepface/weights/race_model_single_batch.h5")
race_model.load_weights(output)
return race_model

View File

@ -29,7 +29,9 @@ class CenterFaceClient(Detector):
"""
Download pre-trained weights of CenterFace model if necessary and load built model
"""
weights_path = f"{folder_utils.get_deepface_home()}/.deepface/weights/centerface.onnx"
home = folder_utils.get_deepface_home()
weights_path = os.path.join(home, ".deepface/weights/centerface.onnx")
if not os.path.isfile(weights_path):
logger.info(f"Downloading CenterFace weights from {WEIGHTS_URL} to {weights_path}...")
try:

View File

@ -20,8 +20,6 @@ class DlibClient(Detector):
Returns:
model (Any)
"""
home = folder_utils.get_deepface_home()
# this is not a must dependency. do not import it in the global level.
try:
import dlib
@ -32,24 +30,25 @@ class DlibClient(Detector):
) from e
# check required file exists in the home/.deepface/weights folder
if os.path.isfile(home + "/.deepface/weights/shape_predictor_5_face_landmarks.dat") != True:
home = folder_utils.get_deepface_home()
filename = "shape_predictor_5_face_landmarks.dat"
filepath = os.path.join(home, ".deepface/weights/", filename)
file_name = "shape_predictor_5_face_landmarks.dat.bz2"
logger.info(f"{file_name} is going to be downloaded")
if not os.path.isfile(filepath):
logger.info(f"{filename + '.bz2'} is going to be downloaded")
url = f"http://dlib.net/files/{file_name}"
output = f"{home}/.deepface/weights/{file_name}"
url = f"http://dlib.net/files/{filename + '.bz2'}"
output = filepath + ".bz2"
gdown.download(url, output, quiet=False)
zipfile = bz2.BZ2File(output)
data = zipfile.read()
newfilepath = output[:-4] # discard .bz2 extension
with open(newfilepath, "wb") as f:
with open(filepath, "wb") as f:
f.write(data)
face_detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(home + "/.deepface/weights/shape_predictor_5_face_landmarks.dat")
sp = dlib.shape_predictor(filepath)
detector = {}
detector["face_detector"] = face_detector

View File

@ -138,8 +138,8 @@ class OpenCvClient(Detector):
"""
opencv_path = self.__get_opencv_path()
if model_name == "haarcascade":
face_detector_path = opencv_path + "haarcascade_frontalface_default.xml"
if os.path.isfile(face_detector_path) != True:
face_detector_path = os.path.join(opencv_path, "haarcascade_frontalface_default.xml")
if not os.path.isfile(face_detector_path):
raise ValueError(
"Confirm that opencv is installed on your environment! Expected path ",
face_detector_path,
@ -148,8 +148,8 @@ class OpenCvClient(Detector):
detector = cv2.CascadeClassifier(face_detector_path)
elif model_name == "haarcascade_eye":
eye_detector_path = opencv_path + "haarcascade_eye.xml"
if os.path.isfile(eye_detector_path) != True:
eye_detector_path = os.path.join(opencv_path, "haarcascade_eye.xml")
if not os.path.isfile(eye_detector_path):
raise ValueError(
"Confirm that opencv is installed on your environment! Expected path ",
eye_detector_path,
@ -168,11 +168,4 @@ class OpenCvClient(Detector):
Returns:
installation_path (str)
"""
opencv_home = cv2.__file__
folders = opencv_home.split(os.path.sep)[0:-1]
path = folders[0]
for folder in folders[1:]:
path = path + "/" + folder
return path + "/data/"
return os.path.join(os.path.dirname(cv2.__file__), "data")

View File

@ -28,35 +28,21 @@ class SsdClient(Detector):
home = folder_utils.get_deepface_home()
# model structure
if os.path.isfile(home + "/.deepface/weights/deploy.prototxt") != True:
logger.info("deploy.prototxt will be downloaded...")
output_model = os.path.join(home, ".deepface/weights/deploy.prototxt")
if not os.path.isfile(output_model):
logger.info(f"{os.path.basename(output_model)} will be downloaded...")
url = "https://github.com/opencv/opencv/raw/3.4.0/samples/dnn/face_detector/deploy.prototxt"
output = home + "/.deepface/weights/deploy.prototxt"
gdown.download(url, output, quiet=False)
gdown.download(url, output_model, quiet=False)
# pre-trained weights
if (
os.path.isfile(home + "/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel")
!= True
):
logger.info("res10_300x300_ssd_iter_140000.caffemodel will be downloaded...")
output_weights = os.path.join(home, ".deepface/weights/res10_300x300_ssd_iter_140000.caffemodel")
if not os.path.isfile(output_weights):
logger.info(f"{os.path.basename(output_weights)} will be downloaded...")
url = "https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel"
output = home + "/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel"
gdown.download(url, output, quiet=False)
gdown.download(url, output_weights, quiet=False)
try:
face_detector = cv2.dnn.readNetFromCaffe(
home + "/.deepface/weights/deploy.prototxt",
home + "/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel",
)
face_detector = cv2.dnn.readNetFromCaffe(output_model, output_weights)
except Exception as err:
raise ValueError(
"Exception while calling opencv.dnn module."

View File

@ -9,7 +9,7 @@ from deepface.commons.logger import Logger
logger = Logger()
# Model's weights paths
PATH = "/.deepface/weights/yolov8n-face.pt"
PATH = ".deepface/weights/yolov8n-face.pt"
# Google Drive URL from repo (https://github.com/derronqi/yolov8-face) ~6MB
WEIGHT_URL = "https://drive.google.com/uc?id=1qcr9DbgsX3ryrz2uU8w4Xm3cOrRywXqb"
@ -35,7 +35,8 @@ class YoloClient(Detector):
"Please install using 'pip install ultralytics'"
) from e
weight_path = f"{folder_utils.get_deepface_home()}{PATH}"
home = folder_utils.get_deepface_home()
weight_path = os.path.join(home, PATH)
# Download the model's weights if they don't exist
if not os.path.isfile(weight_path):

View File

@ -43,15 +43,13 @@ class YuNetClient(Detector):
url = "https://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/face_detection_yunet_2023mar.onnx"
file_name = "face_detection_yunet_2023mar.onnx"
home = folder_utils.get_deepface_home()
if os.path.isfile(home + f"/.deepface/weights/{file_name}") is False:
output = os.path.join(home, ".deepface/weights", file_name)
if not os.path.isfile(output):
logger.info(f"{file_name} will be downloaded...")
output = home + f"/.deepface/weights/{file_name}"
gdown.download(url, output, quiet=False)
try:
face_detector = cv2.FaceDetectorYN_create(
home + f"/.deepface/weights/{file_name}", "", (0, 0)
)
face_detector = cv2.FaceDetectorYN_create(output, "", (0, 0))
except Exception as err:
raise ValueError(
"Exception while calling opencv.FaceDetectorYN_create module."

View File

@ -84,13 +84,11 @@ def load_model(
home = folder_utils.get_deepface_home()
file_name = "arcface_weights.h5"
output = home + "/.deepface/weights/" + file_name
if os.path.isfile(output) != True:
output = os.path.join(home, ".deepface/weights", file_name)
if not os.path.isfile(output):
logger.info(f"{file_name} will be downloaded to {output}")
gdown.download(url, output, quiet=False)
# ---------------------------------------
model.load_weights(output)

View File

@ -87,13 +87,12 @@ def load_model(
# ---------------------------------
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/deepid_keras_weights.h5")
if os.path.isfile(home + "/.deepface/weights/deepid_keras_weights.h5") != True:
logger.info("deepid_keras_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/deepid_keras_weights.h5"
if not os.path.isfile(output):
logger.info(f"{os.path.basename(output)} will be downloaded...")
gdown.download(url, output, quiet=False)
model.load_weights(home + "/.deepface/weights/deepid_keras_weights.h5")
model.load_weights(output)
return model

View File

@ -67,21 +67,19 @@ class DlibResNet:
) from e
home = folder_utils.get_deepface_home()
weight_file = home + "/.deepface/weights/dlib_face_recognition_resnet_model_v1.dat"
filename = "dlib_face_recognition_resnet_model_v1.dat"
weight_file = os.path.join(home, ".deepface/weights", filename)
# download pre-trained model if it does not exist
if os.path.isfile(weight_file) != True:
logger.info("dlib_face_recognition_resnet_model_v1.dat is going to be downloaded")
file_name = "dlib_face_recognition_resnet_model_v1.dat.bz2"
url = f"http://dlib.net/files/{file_name}"
output = f"{home}/.deepface/weights/{file_name}"
if not os.path.isfile(weight_file):
logger.info(f"{filename} is going to be downloaded")
url = f"http://dlib.net/files/{filename + 'bz2'}"
output = weight_file + ".bz2"
gdown.download(url, output, quiet=False)
zipfile = bz2.BZ2File(output)
data = zipfile.read()
newfilepath = output[:-4] # discard .bz2 extension
with open(newfilepath, "wb") as f:
with open(weight_file, "wb") as f:
f.write(data)
self.model = dlib.face_recognition_model_v1(weight_file)

View File

@ -1669,16 +1669,15 @@ def load_facenet128d_model(
# -----------------------------------
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/facenet_weights.h5")
if os.path.isfile(home + "/.deepface/weights/facenet_weights.h5") != True:
logger.info("facenet_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/facenet_weights.h5"
if not os.path.isfile(output):
logger.info(f"{os.path.basename(output)} will be downloaded...")
gdown.download(url, output, quiet=False)
# -----------------------------------
model.load_weights(home + "/.deepface/weights/facenet_weights.h5")
model.load_weights(output)
# -----------------------------------
@ -1699,16 +1698,15 @@ def load_facenet512d_model(
# -------------------------
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/facenet512_weights.h5")
if os.path.isfile(home + "/.deepface/weights/facenet512_weights.h5") != True:
logger.info("facenet512_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/facenet512_weights.h5"
if not os.path.isfile(output):
logger.info(f"{os.path.basename(output)} will be downloaded...")
gdown.download(url, output, quiet=False)
# -------------------------
model.load_weights(home + "/.deepface/weights/facenet512_weights.h5")
model.load_weights(output)
# -------------------------

View File

@ -85,19 +85,19 @@ def load_model(
# ---------------------------------
home = folder_utils.get_deepface_home()
filename = "VGGFace2_DeepFace_weights_val-0.9034.h5"
output = os.path.join(home, ".deepface/weights", filename)
if os.path.isfile(home + "/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5") != True:
logger.info("VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...")
output = home + "/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5.zip"
gdown.download(url, output, quiet=False)
if not os.path.isfile(output):
logger.info(f"{filename} will be downloaded...")
output_zipped = output + ".zip"
gdown.download(url, output_zipped, quiet=False)
# unzip VGGFace2_DeepFace_weights_val-0.9034.h5.zip
with zipfile.ZipFile(output, "r") as zip_ref:
zip_ref.extractall(home + "/.deepface/weights/")
with zipfile.ZipFile(output_zipped, "r") as zip_ref:
zip_ref.extractall(os.path.join(home, ".deepface/weights"))
base_model.load_weights(home + "/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5")
base_model.load_weights(output)
# drop F8 and D0. F7 is the representation layer.
deepface_model = Model(inputs=base_model.layers[0].input, outputs=base_model.layers[-3].output)

View File

@ -75,9 +75,9 @@ def load_model():
model = GhostFaceNetV1()
home = folder_utils.get_deepface_home()
output = home + "/.deepface/weights/ghostfacenet_v1.h5"
output = os.path.join(home, ".deepface/weights/ghostfacenet_v1.h5")
if os.path.isfile(output) is not True:
if not os.path.isfile(output):
logger.info(f"Pre-trained weights is downloaded from {PRETRAINED_WEIGHTS} to {output}")
gdown.download(PRETRAINED_WEIGHTS, output, quiet=False)
logger.info(f"Pre-trained weights is just downloaded to {output}")

View File

@ -381,16 +381,15 @@ def load_model(
# -----------------------------------
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/openface_weights.h5")
if os.path.isfile(home + "/.deepface/weights/openface_weights.h5") != True:
logger.info("openface_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/openface_weights.h5"
if not os.path.isfile(output):
logger.info(f"{os.path.basename(output)} will be downloaded...")
gdown.download(url, output, quiet=False)
# -----------------------------------
model.load_weights(home + "/.deepface/weights/openface_weights.h5")
model.load_weights(output)
# -----------------------------------

View File

@ -56,16 +56,13 @@ def load_model(
"""
home = folder_utils.get_deepface_home()
output = os.path.join(home, ".deepface/weights/face_recognition_sface_2021dec.onnx")
file_name = home + "/.deepface/weights/face_recognition_sface_2021dec.onnx"
if not os.path.isfile(output):
logger.info(f"{os.path.basename(output)} weights will be downloaded...")
gdown.download(url, output, quiet=False)
if not os.path.isfile(file_name):
logger.info("sface weights will be downloaded...")
gdown.download(url, file_name, quiet=False)
model = SFaceWrapper(model_path=file_name)
model = SFaceWrapper(model_path=output)
return model

View File

@ -134,10 +134,10 @@ def load_model(
model = base_model()
home = folder_utils.get_deepface_home()
output = home + "/.deepface/weights/vgg_face_weights.h5"
output = os.path.join(home, ".deepface/weights/vgg_face_weights.h5")
if os.path.isfile(output) != True:
logger.info("vgg_face_weights.h5 will be downloaded...")
if not os.path.isfile(output):
logger.info(f"{os.path.basename(output)} will be downloaded...")
gdown.download(url, output, quiet=False)
model.load_weights(output)

View File

@ -97,8 +97,8 @@ def find(
tic = time.time()
if os.path.isdir(db_path) is not True:
raise ValueError("Passed db_path does not exist!")
if not os.path.isdir(db_path):
raise ValueError(f"Passed path {db_path} does not exist!")
file_parts = [
"ds",

View File

@ -83,7 +83,7 @@ def test_file_types_while_loading_base64():
img1_path = "dataset/img47.jpg"
img1_base64 = image_to_base64(image_path=img1_path)
with pytest.raises(ValueError, match="input image can be jpg or png, but it is"):
with pytest.raises(ValueError, match="Input image can be jpg or png, but it is"):
_ = image_utils.load_image_from_base64(uri=img1_base64)
img2_path = "dataset/img1.jpg"