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

View File

@ -20,7 +20,7 @@ def download_external_file(file_name: str, exact_file_path: str, url: str) -> No
Returns: Returns:
None 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}") logger.info(f"Downloading MiniFASNetV2 weights to {exact_file_path}")
try: try:
gdown.download(url, exact_file_path, quiet=False) gdown.download(url, exact_file_path, quiet=False)

View File

@ -1,5 +1,4 @@
import os import os
from pathlib import Path
from deepface.commons.logger import Logger from deepface.commons.logger import Logger
logger = Logger() logger = Logger()
@ -13,16 +12,16 @@ def initialize_folder() -> None:
OSError: if the folder cannot be created. OSError: if the folder cannot be created.
""" """
home = get_deepface_home() home = get_deepface_home()
deepface_home_path = home + "/.deepface" deepface_home_path = os.path.join(home, ".deepface")
weights_path = deepface_home_path + "/weights" weights_path = os.path.join(deepface_home_path, "weights")
if not os.path.exists(deepface_home_path): if not os.path.exists(deepface_home_path):
os.makedirs(deepface_home_path, exist_ok=True) 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): if not os.path.exists(weights_path):
os.makedirs(weights_path, exist_ok=True) 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: def get_deepface_home() -> str:
@ -32,4 +31,4 @@ def get_deepface_home() -> str:
Returns: Returns:
str: the home directory. 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: for file in f:
exact_path = os.path.join(r, file) exact_path = os.path.join(r, file)
_, ext = os.path.splitext(exact_path) ext_lower = os.path.splitext(exact_path)[-1].lower()
ext_lower = ext.lower()
if ext_lower not in {".jpg", ".jpeg", ".png"}: if ext_lower not in {".jpg", ".jpeg", ".png"}:
continue continue
with Image.open(exact_path) as img: # lazy 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) images.append(exact_path)
return images 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" return load_image_from_base64(img), "base64 encoded string"
# The image is a url # 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 return load_image_from_web(url=img), img
# The image is a path # 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") raise ValueError(f"Confirm that {img} exists")
# image must be a file on the system then # image must be a file on the system then
# image name must have english characters # 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}") raise ValueError(f"Input image must not have non-english characters - {img}")
img_obj_bgr = cv2.imread(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 # content type is safer option than file extension
with Image.open(io.BytesIO(decoded_bytes)) as img: with Image.open(io.BytesIO(decoded_bytes)) as img:
file_type = img.format.lower() file_type = img.format.lower()
if file_type not in ["jpeg", "png"]: if file_type not in {"jpeg", "png"}:
raise ValueError(f"input image can be jpg or png, but it is {file_type}") raise ValueError(f"Input image can be jpg or png, but it is {file_type}")
nparr = np.fromstring(decoded_bytes, np.uint8) nparr = np.fromstring(decoded_bytes, np.uint8)
img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR) img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

View File

@ -67,14 +67,13 @@ def load_model(
# load weights # load weights
home = folder_utils.get_deepface_home() 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...") logger.info("age_model_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/age_model_weights.h5"
gdown.download(url, output, quiet=False) 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 return age_model
@ -89,6 +88,6 @@ def find_apparent_age(age_predictions: np.ndarray) -> np.float64:
Returns: Returns:
apparent_age (float) 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) apparent_age = np.sum(age_predictions * output_indexes)
return apparent_age return apparent_age

View File

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

View File

@ -74,13 +74,12 @@ def load_model(
# load weights # load weights
home = folder_utils.get_deepface_home() 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...") logger.info("gender_model_weights.h5 will be downloaded...")
output = home + "/.deepface/weights/gender_model_weights.h5"
gdown.download(url, output, quiet=False) 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 return gender_model

View File

@ -71,13 +71,12 @@ def load_model(
# load weights # load weights
home = folder_utils.get_deepface_home() 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...") 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) 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 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 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): if not os.path.isfile(weights_path):
logger.info(f"Downloading CenterFace weights from {WEIGHTS_URL} to {weights_path}...") logger.info(f"Downloading CenterFace weights from {WEIGHTS_URL} to {weights_path}...")
try: try:

View File

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

View File

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

View File

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

View File

@ -9,7 +9,7 @@ from deepface.commons.logger import Logger
logger = Logger() logger = Logger()
# Model's weights paths # 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 # Google Drive URL from repo (https://github.com/derronqi/yolov8-face) ~6MB
WEIGHT_URL = "https://drive.google.com/uc?id=1qcr9DbgsX3ryrz2uU8w4Xm3cOrRywXqb" WEIGHT_URL = "https://drive.google.com/uc?id=1qcr9DbgsX3ryrz2uU8w4Xm3cOrRywXqb"
@ -35,7 +35,8 @@ class YoloClient(Detector):
"Please install using 'pip install ultralytics'" "Please install using 'pip install ultralytics'"
) from e ) 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 # Download the model's weights if they don't exist
if not os.path.isfile(weight_path): 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" 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" file_name = "face_detection_yunet_2023mar.onnx"
home = folder_utils.get_deepface_home() 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...") logger.info(f"{file_name} will be downloaded...")
output = home + f"/.deepface/weights/{file_name}"
gdown.download(url, output, quiet=False) gdown.download(url, output, quiet=False)
try: try:
face_detector = cv2.FaceDetectorYN_create( face_detector = cv2.FaceDetectorYN_create(output, "", (0, 0))
home + f"/.deepface/weights/{file_name}", "", (0, 0)
)
except Exception as err: except Exception as err:
raise ValueError( raise ValueError(
"Exception while calling opencv.FaceDetectorYN_create module." "Exception while calling opencv.FaceDetectorYN_create module."

View File

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

View File

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

View File

@ -67,21 +67,19 @@ class DlibResNet:
) from e ) from e
home = folder_utils.get_deepface_home() 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 # download pre-trained model if it does not exist
if os.path.isfile(weight_file) != True: if not os.path.isfile(weight_file):
logger.info("dlib_face_recognition_resnet_model_v1.dat is going to be downloaded") logger.info(f"{filename} is going to be downloaded")
url = f"http://dlib.net/files/{filename + 'bz2'}"
file_name = "dlib_face_recognition_resnet_model_v1.dat.bz2" output = weight_file + ".bz2"
url = f"http://dlib.net/files/{file_name}"
output = f"{home}/.deepface/weights/{file_name}"
gdown.download(url, output, quiet=False) gdown.download(url, output, quiet=False)
zipfile = bz2.BZ2File(output) zipfile = bz2.BZ2File(output)
data = zipfile.read() data = zipfile.read()
newfilepath = output[:-4] # discard .bz2 extension with open(weight_file, "wb") as f:
with open(newfilepath, "wb") as f:
f.write(data) f.write(data)
self.model = dlib.face_recognition_model_v1(weight_file) 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() 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: if not os.path.isfile(output):
logger.info("facenet_weights.h5 will be downloaded...") logger.info(f"{os.path.basename(output)} will be downloaded...")
output = home + "/.deepface/weights/facenet_weights.h5"
gdown.download(url, output, quiet=False) 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() 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: if not os.path.isfile(output):
logger.info("facenet512_weights.h5 will be downloaded...") logger.info(f"{os.path.basename(output)} will be downloaded...")
output = home + "/.deepface/weights/facenet512_weights.h5"
gdown.download(url, output, quiet=False) 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() 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: if not os.path.isfile(output):
logger.info("VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...") logger.info(f"{filename} will be downloaded...")
output_zipped = output + ".zip"
output = home + "/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5.zip" gdown.download(url, output_zipped, quiet=False)
gdown.download(url, output, quiet=False)
# unzip VGGFace2_DeepFace_weights_val-0.9034.h5.zip # unzip VGGFace2_DeepFace_weights_val-0.9034.h5.zip
with zipfile.ZipFile(output, "r") as zip_ref: with zipfile.ZipFile(output_zipped, "r") as zip_ref:
zip_ref.extractall(home + "/.deepface/weights/") 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. # drop F8 and D0. F7 is the representation layer.
deepface_model = Model(inputs=base_model.layers[0].input, outputs=base_model.layers[-3].output) 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() model = GhostFaceNetV1()
home = folder_utils.get_deepface_home() 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}") logger.info(f"Pre-trained weights is downloaded from {PRETRAINED_WEIGHTS} to {output}")
gdown.download(PRETRAINED_WEIGHTS, output, quiet=False) gdown.download(PRETRAINED_WEIGHTS, output, quiet=False)
logger.info(f"Pre-trained weights is just downloaded to {output}") 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() 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: if not os.path.isfile(output):
logger.info("openface_weights.h5 will be downloaded...") logger.info(f"{os.path.basename(output)} will be downloaded...")
output = home + "/.deepface/weights/openface_weights.h5"
gdown.download(url, output, quiet=False) 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() 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): model = SFaceWrapper(model_path=output)
logger.info("sface weights will be downloaded...")
gdown.download(url, file_name, quiet=False)
model = SFaceWrapper(model_path=file_name)
return model return model

View File

@ -134,10 +134,10 @@ def load_model(
model = base_model() model = base_model()
home = folder_utils.get_deepface_home() 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: if not os.path.isfile(output):
logger.info("vgg_face_weights.h5 will be downloaded...") logger.info(f"{os.path.basename(output)} will be downloaded...")
gdown.download(url, output, quiet=False) gdown.download(url, output, quiet=False)
model.load_weights(output) model.load_weights(output)

View File

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

View File

@ -83,7 +83,7 @@ def test_file_types_while_loading_base64():
img1_path = "dataset/img47.jpg" img1_path = "dataset/img47.jpg"
img1_base64 = image_to_base64(image_path=img1_path) 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) _ = image_utils.load_image_from_base64(uri=img1_base64)
img2_path = "dataset/img1.jpg" img2_path = "dataset/img1.jpg"