From 68cf5d5bc0b498b34d874cff838fda9394932610 Mon Sep 17 00:00:00 2001
From: Tiago Freitas Pereira <tiagofrepereira@gmail.com>
Date: Thu, 8 Apr 2021 19:01:30 +0200
Subject: [PATCH] Integrated CBSR_NIR_VIS_2 database

---
 .../face/config/database/cbsr_nir_vis_2.py    |   9 ++
 bob/bio/face/database/__init__.py             |   2 +
 bob/bio/face/database/cbsr_nir_vis_2.py       | 117 ++++++++++++++++++
 bob/bio/face/test/test_databases.py           |  12 +-
 doc/implemented.rst                           |   1 +
 setup.py                                      |   2 +
 6 files changed, 142 insertions(+), 1 deletion(-)
 create mode 100644 bob/bio/face/config/database/cbsr_nir_vis_2.py
 create mode 100644 bob/bio/face/database/cbsr_nir_vis_2.py

diff --git a/bob/bio/face/config/database/cbsr_nir_vis_2.py b/bob/bio/face/config/database/cbsr_nir_vis_2.py
new file mode 100644
index 00000000..3476a70d
--- /dev/null
+++ b/bob/bio/face/config/database/cbsr_nir_vis_2.py
@@ -0,0 +1,9 @@
+from bob.bio.face.database import CBSRNirVis2Database
+
+# In case protocol is comming from chain loading
+# https://www.idiap.ch/software/bob/docs/bob/bob.extension/stable/py_api.html#bob.extension.config.load
+if "protocol" not in locals():
+    protocol = "view2_1"
+
+
+database = CBSRNirVis2Database(protocol=protocol)
diff --git a/bob/bio/face/database/__init__.py b/bob/bio/face/database/__init__.py
index f1abd8c7..73964627 100644
--- a/bob/bio/face/database/__init__.py
+++ b/bob/bio/face/database/__init__.py
@@ -16,6 +16,7 @@ from .meds import MEDSDatabase
 from .morph import MorphDatabase
 from .casia_africa import CasiaAfricaDatabase
 from .pola_thermal import PolaThermalDatabase
+from .cbsr_nir_vis_2 import CBSRNirVis2Database
 
 # gets sphinx autodoc done right - don't remove it
 
@@ -51,5 +52,6 @@ __appropriate__(
     MorphDatabase,
     CasiaAfricaDatabase,
     PolaThermalDatabase,
+    CBSRNirVis2Database,
 )
 __all__ = [_ for _ in dir() if not _.startswith("_")]
diff --git a/bob/bio/face/database/cbsr_nir_vis_2.py b/bob/bio/face/database/cbsr_nir_vis_2.py
new file mode 100644
index 00000000..01159b9a
--- /dev/null
+++ b/bob/bio/face/database/cbsr_nir_vis_2.py
@@ -0,0 +1,117 @@
+#!/usr/bin/env python
+# vim: set fileencoding=utf-8 :
+# Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
+
+"""
+  CBSRNirVis2Database database: database implementation 
+"""
+
+from bob.bio.base.database import CSVDataset
+from bob.bio.base.database import CSVToSampleLoaderBiometrics
+from bob.bio.face.database.sample_loaders import EyesAnnotations
+from bob.extension import rc
+from bob.extension.download import get_file
+import bob.io.base
+from sklearn.pipeline import make_pipeline
+import os
+
+
+class CBSRNirVis2Database(CSVDataset):
+    """
+    This package contains the access API and descriptions for the `CASIA NIR-VIS 2.0 Database <http://www.cbsr.ia.ac.cn/english/NIR-VIS-2.0-Database.html>`. 
+    The actual raw data for the database should be downloaded from the original URL. 
+    This package only contains the Bob accessor methods to use the DB directly from python, with the original protocol of the database.
+
+    CASIA NIR-VIS 2.0 database offers pairs of mugshot images and their correspondent NIR photos.
+    The images of this database were collected in four recording sessions: 2007 spring, 2009 summer, 2009 fall and 2010 summer,
+    in which the first session is identical to the CASIA HFB database. It consists of 725 subjects in total.
+    There are [1-22] VIS and [5-50] NIR face images per subject. The eyes positions are also distributed with the images.
+
+
+    .. code-block:: latex
+
+        @inproceedings{li2013casia,
+        title={The casia nir-vis 2.0 face database},
+        author={Li, Stan Z and Yi, Dong and Lei, Zhen and Liao, Shengcai},
+        booktitle={Computer Vision and Pattern Recognition Workshops (CVPRW), 2013 IEEE Conference on},
+        pages={348--353},
+        year={2013},
+        organization={IEEE}     
+        }
+
+
+    .. warning::
+        Use the command below to set the path of the real data::
+
+            $ bob config set bob.db.cbsr-nir-vis-2.directory [PATH-TO-CBSR-DATA]
+
+
+
+    Parameters
+    ----------
+
+    protocol: str
+        One of the database protocols.
+    """
+
+    def __init__(self, protocol):
+
+        # Downloading model if not exists
+        urls = CBSRNirVis2Database.urls()
+        filename = get_file(
+            "cbsr_nir_vis_2.tar.gz", urls, file_hash="1fc247626f88d6f36a2675516eb745a5",
+        )
+        self.annotation_type = "eyes-center"
+        self.fixed_positions = None
+
+        directory = (
+            rc["bob.db.cbsr-nir-vis-2.directory"]
+            if rc["bob.db.cbsr-nir-vis-2.directory"]
+            else ""
+        )
+
+        def load(filename):
+            extensions = [".jpg", ".bmp"]
+            for e in extensions:
+                f = os.path.splitext(filename)[0]
+                new_filename = f + e
+                if os.path.exists(new_filename):
+                    return bob.io.base.load(new_filename)
+            else:
+                raise ValueError("File `{0}` not found".format(str(new_filename)))
+
+        super().__init__(
+            filename,
+            protocol,
+            csv_to_sample_loader=make_pipeline(
+                CSVToSampleLoaderBiometrics(
+                    data_loader=load,
+                    dataset_original_directory=directory,
+                    extension=".jpg",
+                ),
+                EyesAnnotations(),
+            ),
+        )
+
+    @staticmethod
+    def protocols():
+        # TODO: Until we have (if we have) a function that dumps the protocols, let's use this one.
+        return [
+            "view2_1",
+            "view2_2",
+            "view2_3",
+            "view2_4",
+            "view2_5",
+            "view2_6",
+            "view2_7",
+            "view2_8",
+            "view2_9",
+            "view2_10",
+        ]
+
+    @staticmethod
+    def urls():
+        return [
+            "https://www.idiap.ch/software/bob/databases/latest/cbsr_nir_vis_2.tar.gz",
+            "http://www.idiap.ch/software/bob/databases/latest/cbsr_nir_vis_2.tar.gz",
+        ]
diff --git a/bob/bio/face/test/test_databases.py b/bob/bio/face/test/test_databases.py
index 8cfad46a..3342e11e 100644
--- a/bob/bio/face/test/test_databases.py
+++ b/bob/bio/face/test/test_databases.py
@@ -406,10 +406,20 @@ def test_casia_africa():
     assert len(database.probes()) == 2426
 
 
-def test_casia_polathermal():
+def test_polathermal():
 
     from bob.bio.face.database import PolaThermalDatabase
 
     database = PolaThermalDatabase("VIS-thermal-overall-split1")
     assert len(database.references()) == 35
     assert len(database.probes()) == 1680
+
+
+def test_cbsr_nir_vis_2():
+
+    from bob.bio.face.database import CBSRNirVis2Database
+
+    database = CBSRNirVis2Database("view2_1")
+
+    assert len(database.references()) == 358
+    assert len(database.probes()) == 6123
diff --git a/doc/implemented.rst b/doc/implemented.rst
index d4a4165f..cf5801e3 100644
--- a/doc/implemented.rst
+++ b/doc/implemented.rst
@@ -24,6 +24,7 @@ Databases
    bob.bio.face.database.MEDSDatabase
    bob.bio.face.database.MorphDatabase
    bob.bio.face.database.PolaThermalDatabase
+   bob.bio.face.database.CBSRNirVis2Database
 
 
 Face Image Annotators
diff --git a/setup.py b/setup.py
index c6c37209..ded13e23 100644
--- a/setup.py
+++ b/setup.py
@@ -114,6 +114,7 @@ setup(
             "morph = bob.bio.face.config.database.morph:database",
             "casia-africa = bob.bio.face.config.database.casia_africa:database",
             "pola-thermal = bob.bio.face.config.database.pola_thermal:database",
+            "cbsr-nir-vis-2 = bob.bio.face.config.database.cbsr_nir_vis_2:database",
         ],
         "bob.bio.annotator": [
             "facedetect               = bob.bio.face.config.annotator.facedetect:annotator",
@@ -181,6 +182,7 @@ setup(
             "morph = bob.bio.face.config.database.morph",
             "casia-africa = bob.bio.face.config.database.casia_africa",
             "pola-thermal = bob.bio.face.config.database.pola_thermal",
+            "cbsr-nir-vis-2 = bob.bio.face.config.database.cbsr_nir_vis_2",
             "resnet50-msceleb-arcface-2021 = bob.bio.face.config.baseline.resnet50_msceleb_arcface_2021",
             "resnet50-vgg2-arcface-2021 = bob.bio.face.config.baseline.resnet50_vgg2_arcface_2021",
             "mobilenetv2-msceleb-arcface-2021 = bob.bio.face.config.baseline.mobilenetv2_msceleb_arcface_2021",
-- 
GitLab