# Path: DeepFried2/datasets/images.py
# def imread(fname, dtype=_np.float32):
#     im = _cv2.imread(fname, flags=_cv2.IMREAD_UNCHANGED)
#     if im is None:
#         raise IOError("Couldn't open image file {}".format(fname))
#     # Convert BGR to RGB. OpenCV is weird like that.
#     if im.ndim == 3 and im.shape[-1] == 3:
#         im = im[:,:,::-1]
#     return im.astype(dtype)


import DeepFried2 as df
import numpy as _np
import os.path as _p
from .images import imread
from tarfile import open as _taropen
        from sklearn.preprocessing import LabelEncoder



def data(fold=False):
    fname = df.zoo.download('http://dags.stanford.edu/data/iccv09Data.tar.gz')

    # extracting files one-by-one in memory is unfortunately WAY too slow
    # for this dataset. So we bite the bullet and extract the full tgz.

    where = _p.dirname(fname)
    imgdir = 'iccv09Data/images/'

    with _taropen(fname, 'r') as f:
        f.extractall(where)
        ids = [_p.basename(n)[:-4] for n in f.getnames() if n.startswith(imgdir)]


# Path: DeepFried2/datasets/images.py
# def imread(fname, dtype=_np.float32):
#     im = _cv2.imread(fname, flags=_cv2.IMREAD_UNCHANGED)
#     if im is None:
#         raise IOError("Couldn't open image file {}".format(fname))
#     # Convert BGR to RGB. OpenCV is weird like that.
#     if im.ndim == 3 and im.shape[-1] == 3:
#         im = im[:,:,::-1]
#     return im.astype(dtype)


# Path: DeepFried2/datasets/stanfordbg.py
import DeepFried2 as df
import numpy as _np
import os.path as _p
from .images import imread
from tarfile import open as _taropen
        from sklearn.preprocessing import LabelEncoder




def data(fold=False):
    fname = df.zoo.download('http://dags.stanford.edu/data/iccv09Data.tar.gz')

    # extracting files one-by-one in memory is unfortunately WAY too slow
    # for this dataset. So we bite the bullet and extract the full tgz.

    where = _p.dirname(fname)
    imgdir = 'iccv09Data/images/'

    with _taropen(fname, 'r') as f:
        f.extractall(where)
        ids = [_p.basename(n)[:-4] for n in f.getnames() if n.startswith(imgdir)]


    X = [imread(_p.join(where, imgdir, i) + '.jpg') for i in ids]