From fc98ae018745451f0ec4ffe80dcf0c244fe50cc4 Mon Sep 17 00:00:00 2001
From: Samuel Gaist <samuel.gaist@idiap.ch>
Date: Wed, 27 Jan 2021 17:47:02 +0100
Subject: [PATCH] [advanced][databases][livdet-iris-2020] Add new version
 following V2 implementation

---
 advanced/databases/livdet-iris-2020/2.json | 21 ++++++++++++
 advanced/databases/livdet-iris-2020/2.py   | 40 ++++++++++++++++++++++
 advanced/databases/livdet-iris-2020/2.rst  |  1 +
 3 files changed, 62 insertions(+)
 create mode 100644 advanced/databases/livdet-iris-2020/2.json
 create mode 100644 advanced/databases/livdet-iris-2020/2.py
 create mode 100644 advanced/databases/livdet-iris-2020/2.rst

diff --git a/advanced/databases/livdet-iris-2020/2.json b/advanced/databases/livdet-iris-2020/2.json
new file mode 100644
index 0000000..76bb6dd
--- /dev/null
+++ b/advanced/databases/livdet-iris-2020/2.json
@@ -0,0 +1,21 @@
+{
+    "description": "LivDet Iris 2020 test database",
+    "environment": {
+        "name": "Example databases",
+        "version": "1.4.1"
+    },
+    "protocols": [
+        {
+            "name": "Main",
+            "template": "iris_pad/1",
+            "views": {
+                "test": {
+                    "view": "Test",
+                    "parameters": {}
+                }
+            }
+        }
+    ],
+    "root_folder": "/somewhere",
+    "schema_version": 2
+}
\ No newline at end of file
diff --git a/advanced/databases/livdet-iris-2020/2.py b/advanced/databases/livdet-iris-2020/2.py
new file mode 100644
index 0000000..f846013
--- /dev/null
+++ b/advanced/databases/livdet-iris-2020/2.py
@@ -0,0 +1,40 @@
+import os
+
+import numpy as np
+import pandas as pd
+from beat.backend.python.database import View
+from PIL import Image
+
+
+class Test(View):
+    def index(self, root_folder, parameters):
+        """Creates the data for the database indexation"""
+
+        csv_path = os.path.join(root_folder, "test.csv")
+        df = pd.read_csv(csv_path)
+
+        df["filename"] = df["filename"].apply(lambda x: os.path.join(root_folder, x))
+        df = df.rename(columns={"filename": "image"})
+
+        # ------------- v1 random labels -----
+        # remove this part for v2 release
+        num_files = len(df)
+        np.random.seed(0)
+        df["label"] = np.random.randint(0, 2, size=num_files, dtype=bool)
+        df["category"] = np.random.randint(1, 7, size=num_files, dtype=int)
+        # ------------------------------------
+
+        return list(df.itertuples(index=False))
+
+    def get(self, output, index):
+        """Returns the data for the output based on the index content"""
+
+        obj = self.objs[index]
+
+        if output == "image":
+            img = np.asarray(Image.open(obj.image))
+            return {"value": img}
+        elif output == "label":
+            return {"value": bool(obj.label)}
+        elif output == "category":
+            return {"value": np.cast["int32"](obj.category)}
diff --git a/advanced/databases/livdet-iris-2020/2.rst b/advanced/databases/livdet-iris-2020/2.rst
new file mode 100644
index 0000000..298357b
--- /dev/null
+++ b/advanced/databases/livdet-iris-2020/2.rst
@@ -0,0 +1 @@
+LivDet Iris 2020 test database
\ No newline at end of file
-- 
GitLab