{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Skin Colour Detection\n",
    "Students: Sam van Kampen, Robin Wacanno\n",
    "\n",
    "Studentnrs: 11874716, 11741163"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import random\n",
    "from sklearn.linear_model import LogisticRegressionCV\n",
    "from skimage.color import rgb2lab\n",
    "\n",
    "data = dict(i=[], m=[])\n",
    "\n",
    "for dir in [\"FacePhoto\", \"FamilyPhoto\"]:\n",
    "    for f in [f for f in os.listdir(f\"data/SkinColor/{dir}\")]:\n",
    "        if f == \"family_matters_03.jpg\":\n",
    "            continue\n",
    "        f_png = f.split('.')[0] + \".png\"\n",
    "        data['i'].append(plt.imread(f\"data/SkinColor/{dir}/{f}\"))\n",
    "        data['i'][-1] = data['i'][-1].astype('uint8')\n",
    "        data['m'].append(plt.imread(f\"data/SkinColor/GroundT_{dir}/{f_png}\"))\n",
    "        #data['m'][-1] = data['m'][-1].astype('uint8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 83216 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.subplot(121)\n",
    "plt.imshow(data['i'][0])\n",
    "plt.subplot(122)\n",
    "plt.imshow(data['m'][0][:, :, :3])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = data['i'][0][data['m'][0][:,:,0]!=0]\n",
    "ns = data['i'][0][data['m'][0][:,:,0]==0]\n",
    "\n",
    "s_s = s[random.sample(range(len(s)), 500)]\n",
    "s_ns = ns[random.sample(range(len(ns)), 500)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 74260 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = Axes3D(fig)\n",
    "\n",
    "ax.scatter(s_s[:,:1], s_s[:,1:2], s_s[:,2:3], c='r', marker='o')\n",
    "\n",
    "ax.scatter(s_ns[:,:1], s_ns[:,1:2], s_ns[:,2:3], c='b', marker='o')\n",
    "\n",
    "ax.set_xlabel('R')\n",
    "ax.set_ylabel('G')\n",
    "ax.set_zlabel('B')\n",
    "\n",
    "ax.view_init(elev=30., azim=-35)\n",
    "\n",
    "plt.legend([\"Skin\", \"Non-skin\"])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "datalab = data.copy()\n",
    "for i in range(len(datalab['i'])):\n",
    "    datalab['i'][i] = rgb2lab(datalab['i'][i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgnum = 6\n",
    "\n",
    "s = datalab['i'][imgnum][datalab['m'][imgnum][:,:,0]!=0]\n",
    "ns = datalab['i'][imgnum][datalab['m'][imgnum][:,:,0]==0]\n",
    "\n",
    "s_s = s[random.sample(range(len(s)), 500)]\n",
    "s_ns = ns[random.sample(range(len(ns)), 500)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16532 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.clf()\n",
    "\n",
    "plt.scatter(s_s[:,1:2], s_s[:,2:3], c='r', marker='o')\n",
    "plt.scatter(s_ns[:,1:2], s_ns[:,2:3], c='b', marker='o')\n",
    "\n",
    "plt.xlabel(\"a\")\n",
    "plt.ylabel(\"b\")\n",
    "plt.legend([\"Skin\", \"Non-skin\"])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/robin/.local/lib/python3.7/site-packages/sklearn/model_selection/_split.py:2053: FutureWarning: You should specify a value for 'cv' instead of relying on the default value. The default value will change from 3 to 5 in version 0.22.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.957359375"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color = np.vstack((s[:,1:], ns[:,1:]))\n",
    "target = np.concatenate((np.ones(len(s)),np.zeros(len(ns))))\n",
    "\n",
    "learn_color = color[1::2]\n",
    "test_color = color[0::2]\n",
    "learn_target = target[1::2]\n",
    "test_target = target[0::2]\n",
    "\n",
    "logregr = LogisticRegressionCV()\n",
    "logregr.fit(learn_color, learn_target)\n",
    "\n",
    "logregr.score(test_color, test_target)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "allcol = np.concatenate((s[:,1:], ns[:,1:]))\n",
    "\n",
    "pred_s = []\n",
    "pred_ns = []\n",
    "\n",
    "for c in allcol:\n",
    "    pred = logregr.predict(c.reshape(1, -1))\n",
    "    if pred:\n",
    "        pred_s.append(c)\n",
    "    else:\n",
    "        pred_ns.append(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "pred_s = np.array(pred_s)\n",
    "pred_ns = np.array(pred_ns)\n",
    "\n",
    "# pred_s[:,1:]\n",
    "plt.clf()\n",
    "\n",
    "plt.scatter(pred_s[:,:1], pred_s[:,1:], c='tab:cyan', marker='o')\n",
    "plt.scatter(pred_ns[:,:1], pred_ns[:,1:], c='tab:brown', marker='o')\n",
    "\n",
    "plt.scatter(s_s[:,1:2], s_s[:,2:3], c='r', marker='o')\n",
    "plt.scatter(s_ns[:,1:2], s_ns[:,2:3], c='b', marker='o')\n",
    "\n",
    "plt.xlabel(\"a\")\n",
    "plt.ylabel(\"b\")\n",
    "plt.legend([\"Skin (pred)\", \"Non-skin (pred)\", \"Skin (GT)\", \"Non-skin (GT)\"])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bonus III"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.958390625"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.naive_bayes import GaussianNB\n",
    "\n",
    "clf = GaussianNB()\n",
    "clf.fit(learn_color, learn_target)\n",
    "clf.score(test_color, test_target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_s = []\n",
    "pred_ns = []\n",
    "\n",
    "for c in allcol:\n",
    "    pred = clf.predict(c.reshape(1, -1))\n",
    "    if pred:\n",
    "        pred_s.append(c)\n",
    "    else:\n",
    "        pred_ns.append(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32428 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "pred_s = np.array(pred_s)\n",
    "pred_ns = np.array(pred_ns)\n",
    "\n",
    "# pred_s[:,1:]\n",
    "plt.clf()\n",
    "\n",
    "plt.scatter(pred_s[:,:1], pred_s[:,1:], c='tab:cyan', marker='o')\n",
    "plt.scatter(pred_ns[:,:1], pred_ns[:,1:], c='tab:brown', marker='o')\n",
    "\n",
    "plt.scatter(s_s[:,1:2], s_s[:,2:3], c='r', marker='o')\n",
    "plt.scatter(s_ns[:,1:2], s_ns[:,2:3], c='b', marker='o')\n",
    "\n",
    "plt.xlabel(\"a\")\n",
    "plt.ylabel(\"b\")\n",
    "plt.legend([\"Skin (pred)\", \"Non-skin (pred)\", \"Skin (GT)\", \"Non-skin (GT)\"])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
