Skip to content
Snippets Groups Projects
Commit bef98d41 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Updated download method and added a download reference in our servers issue #5

parent 5fd19bbf
Branches
Tags
1 merge request!11Fixing download
......@@ -14,6 +14,36 @@ from .utils import bounding_box_2_rectangle
from .FaceDetector import FaceDetector
def download_file(url, out_file):
"""Downloads a file from a given url
Parameters
----------
url : str
The url to download form.
out_file : str
Where to save the file.
"""
from bob.io.base import create_directories_safe
import os
create_directories_safe(os.path.dirname(out_file))
import sys
if sys.version_info[0] < 3:
# python2 technique for downloading a file
from urllib2 import urlopen
with open(out_file, 'wb') as f:
response = urlopen(url)
f.write(response.read())
else:
# python3 technique for downloading a file
from urllib.request import urlopen
from shutil import copyfileobj
with urlopen(url) as response:
with open(out_file, 'wb') as f:
copyfileobj(response, f)
class DlibLandmarkExtraction(object):
"""
Binds to the DLib landmark detection using the shape_predictor_68_face_landmarks,
......@@ -34,7 +64,7 @@ class DlibLandmarkExtraction(object):
def __init__(self, model=None, bob_landmark_format=False):
default_model = os.path.join(DlibLandmarkExtraction.get_dlib_model_path(), "shape_predictor_68_face_landmarks.dat")
default_model = os.path.join(DlibLandmarkExtraction.get_modelpath(), "shape_predictor_68_face_landmarks.dat")
if model is None:
self.model = default_model
if not os.path.exists(self.model):
......@@ -89,42 +119,54 @@ class DlibLandmarkExtraction(object):
return list(map(lambda p: (p.y, p.x), points.parts()))
@staticmethod
def get_dlib_model_path():
def get_modelpath():
import pkg_resources
return pkg_resources.resource_filename(__name__, 'data')
#url = 'http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2'
@staticmethod
def download_dlib_model():
"""
Download and extract the dlib model face detection model from
"""
import sys
import os
import bz2
logger.info("Downloading the shape_predictor_68_face_landmarks.dat Face model from database from "
"http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 ...")
tmp_file = os.path.join(DlibLandmarkExtraction.get_dlib_model_path(), "shape_predictor_68_face_landmarks.dat.bz2")
url = 'http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2'
#url = 'http://beatubulatest.lab.idiap.ch/software/bob/packages/vgg_face_caffe.tar.gz'
if sys.version_info[0] < 3:
# python2 technique for downloading a file
from urllib2 import urlopen
with open(tmp_file, 'wb') as out_file:
response = urlopen(url)
out_file.write(response.read())
else:
# python3 technique for downloading a file
from urllib.request import urlopen
from shutil import copyfileobj
with urlopen(url) as response:
with open(tmp_file, 'wb') as out_file:
copyfileobj(response, out_file)
"""
Download and extract the DLIB files
"""
import zipfile
zip_file = os.path.join(DlibLandmarkExtraction.get_modelpath(),
"shape_predictor_68_face_landmarks.dat.bz2")
urls = [
# This is a private link at Idiap to save bandwidth.
"http://beatubulatest.lab.idiap.ch/private/wheels/gitlab/"
"shape_predictor_68_face_landmarks.dat.bz2",
# this works for everybody
"http://dlib.net/files/"
"shape_predictor_68_face_landmarks.dat.bz2",
]
for url in urls:
try:
logger.info(
"Downloading DLIB model from "
"{} ...".format(url))
download_file(url, zip_file)
break
except Exception:
logger.warning(
"Could not download from the %s url", url, exc_info=True)
else: # else is for the for loop
if not os.path.isfile(zip_file):
raise RuntimeError("Could not download the zip file.")
# Unzip
logger.info("Unziping in {0}".format(DlibLandmarkExtraction.get_dlib_model_path()))
#t = tarfile.open(fileobj=open(tmp_file), mode='r:gz')
t = bz2.BZ2File(tmp_file)
open(os.path.join(DlibLandmarkExtraction.get_dlib_model_path(),'shape_predictor_68_face_landmarks.dat'), 'wb').write(t.read())
t.close()
logger.info("Unziping in {0}".format(DlibLandmarkExtraction.get_modelpath()))
t = bz2.BZ2File(zip_file)
open(os.path.join(DlibLandmarkExtraction.get_modelpath(),'shape_predictor_68_face_landmarks.dat'), 'wb').write(t.read())
t.close()
os.unlink(zip_file)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment