{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "turned-listening",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "from skimage import io, img_as_ubyte\n",
    "from skimage.filters import threshold_otsu\n",
    "from skimage.color import rgb2gray\n",
    "from skimage.measure import label, regionprops\n",
    "from skimage.transform import resize, rotate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "healthy-curve",
   "metadata": {},
   "outputs": [],
   "source": [
    "folder = \"./imgs/new\"\n",
    "\n",
    "files = [os.path.join(folder, i) for i in os.listdir(folder) if not i == '.DS_Store']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "legal-rebate",
   "metadata": {},
   "outputs": [],
   "source": [
    "def im_to_binary(im, w=1000, h=1000, mask=None):\n",
    "    \n",
    "    i = io.imread(im)\n",
    "    i = rgb2gray(i)\n",
    "    if mask is not None:\n",
    "        i = resize(i, (mask.shape[0], mask.shape[1]))\n",
    "        i = i & mask\n",
    "    else:\n",
    "        i = resize(i, (w, h))\n",
    "\n",
    "    t = threshold_otsu(i)\n",
    "    return i > t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "interior-climb",
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath = 'imgs/mask.jpeg'\n",
    "im = io.imread(filepath)\n",
    "mask = im > threshold_otsu(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fantastic-attention",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-11-446e94b5d707>:4: FutureWarning: Non RGB image conversion is now deprecated. For RGBA images, please use rgb2gray(rgba2rgb(rgb)) instead. In version 0.19, a ValueError will be raised if input image last dimension length is not 3.\n",
      "  i = rgb2gray(i)\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'NoneType' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-14-6ab48c8c6871>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mim_to_binary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfiles\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mfiles\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mim_to_binary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0mj\u001b[0m \u001b[0;34m&=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg_as_ubyte\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-11-446e94b5d707>\u001b[0m in \u001b[0;36mim_to_binary\u001b[0;34m(im, w, h, mask)\u001b[0m\n\u001b[1;32m      7\u001b[0m         \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m&\u001b[0m \u001b[0mmask\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m         \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mw\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mh\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mthreshold_otsu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.9/site-packages/skimage/transform/_warps.py\u001b[0m in \u001b[0;36mresize\u001b[0;34m(image, output_shape, order, mode, cval, clip, preserve_range, anti_aliasing, anti_aliasing_sigma)\u001b[0m\n\u001b[1;32m    160\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    161\u001b[0m             \u001b[0;31m# 3 control points necessary to estimate exact AffineTransform\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 162\u001b[0;31m             \u001b[0msrc_corners\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrows\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mcols\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrows\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    163\u001b[0m             \u001b[0mdst_corners\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzeros\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msrc_corners\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdouble\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    164\u001b[0m             \u001b[0;31m# take into account that 0th pixel is at position (0.5, 0.5)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'NoneType' and 'int'"
     ]
    }
   ],
   "source": [
    "j = im_to_binary(files[0], None)\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f, None)\n",
    "    j &= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "measured-baking",
   "metadata": {},
   "outputs": [],
   "source": [
    "io.imsave('and.jpeg', img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "resident-product",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-3-66fbb490d3b8>:3: FutureWarning: Non RGB image conversion is now deprecated. For RGBA images, please use rgb2gray(rgba2rgb(rgb)) instead. In version 0.19, a ValueError will be raised if input image last dimension length is not 3.\n",
      "  i = rgb2gray(i)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x197afee20>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 7008 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "j = im_to_binary(files[0])\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f)\n",
    "    j |= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "broken-rotation",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['./imgs/codes/snapcode.png', './imgs/codes/snapcode 15.png', './imgs/codes/snapcode 14.png', './imgs/codes/snapcode 13.png', './imgs/codes/snapcode 12.png', './imgs/codes/snapcode 9.png', './imgs/codes/snapcode 10.png', './imgs/codes/snapcode 11.png', './imgs/codes/snapcode 8.png', './imgs/codes/snapcode 5.png', './imgs/codes/snapcode 4.png', './imgs/codes/snapcode 6.png', './imgs/codes/snapcode 7.png', './imgs/codes/snapcode 3.png', './imgs/codes/snapcode 2.png']\n"
     ]
    }
   ],
   "source": [
    "folder = \"./imgs/codes\"\n",
    "\n",
    "files = [os.path.join(folder, i) for i in os.listdir(folder) if not i == '.DS_Store']\n",
    "print(files)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "collectible-senator",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-3-66fbb490d3b8>:3: FutureWarning: Non RGB image conversion is now deprecated. For RGBA images, please use rgb2gray(rgba2rgb(rgb)) instead. In version 0.19, a ValueError will be raised if input image last dimension length is not 3.\n",
      "  i = rgb2gray(i)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x197b62340>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9252 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "j = im_to_binary(files[0])\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f)\n",
    "    j &= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "owned-array",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-3-66fbb490d3b8>:3: FutureWarning: Non RGB image conversion is now deprecated. For RGBA images, please use rgb2gray(rgba2rgb(rgb)) instead. In version 0.19, a ValueError will be raised if input image last dimension length is not 3.\n",
      "  i = rgb2gray(i)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x197b7d940>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6816 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "j = im_to_binary(files[0])\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f)\n",
    "    j |= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "anonymous-bishop",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "streaming-company",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['./imgs/codes/snapcode.png', './imgs/codes/snapcode 15.png', './imgs/codes/snapcode 14.png', './imgs/codes/snapcode 13.png', './imgs/codes/snapcode 12.png', './imgs/codes/snapcode 9.png', './imgs/codes/snapcode 10.png', './imgs/codes/snapcode 11.png', './imgs/codes/snapcode 8.png', './imgs/codes/snapcode 5.png', './imgs/codes/snapcode 4.png', './imgs/codes/snapcode 6.png', './imgs/codes/snapcode 7.png', './imgs/codes/snapcode 3.png', './imgs/codes/snapcode 2.png']\n"
     ]
    }
   ],
   "source": [
    "folder = \"./imgs/codes\"\n",
    "\n",
    "files = [os.path.join(folder, i) for i in os.listdir(folder) if not i == '.DS_Store']\n",
    "print(files)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "conscious-waters",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-3-66fbb490d3b8>:3: FutureWarning: Non RGB image conversion is now deprecated. For RGBA images, please use rgb2gray(rgba2rgb(rgb)) instead. In version 0.19, a ValueError will be raised if input image last dimension length is not 3.\n",
      "  i = rgb2gray(i)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x197c013a0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9252 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "j = im_to_binary(files[0])\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f)\n",
    "    j &= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "announced-authority",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-3-66fbb490d3b8>:3: FutureWarning: Non RGB image conversion is now deprecated. For RGBA images, please use rgb2gray(rgba2rgb(rgb)) instead. In version 0.19, a ValueError will be raised if input image last dimension length is not 3.\n",
      "  i = rgb2gray(i)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x197c59130>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6816 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "j = im_to_binary(files[0])\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f)\n",
    "    j |= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "corporate-classic",
   "metadata": {},
   "source": [
    "## Cropped people"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "controlling-petite",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['./imgs/people/cropped/FEIu0F9XMAcFnnC.jpeg', './imgs/people/cropped/FEK1L9cXoAgulGK.png', './imgs/people/cropped/person_E_1SliPVkAU9LRZ.jpeg', './imgs/people/cropped/person_FD9XmqWXIAYvYz6.jpeg', './imgs/people/cropped/FEJNLkmXIAMnJQP.jpeg', './imgs/people/cropped/person_FA4M9uIWQBQ7Y09.jpeg', './imgs/people/cropped/person_E_y5sW7UYAUhvVJ.jpeg', './imgs/people/cropped/personFD_8QyqXwAALMdW.jpeg', './imgs/people/cropped/FECYsfjXwAIEYmJ.png', './imgs/people/cropped/person_66c49c2d2e645826cd079524d1109bbf.jpeg', './imgs/people/cropped/FEJjiv8XwAInJVO.jpeg']\n"
     ]
    }
   ],
   "source": [
    "folder = \"./imgs/people/cropped\"\n",
    "\n",
    "files = [os.path.join(folder, i) for i in os.listdir(folder) if not i == '.DS_Store']\n",
    "print(files)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "intensive-relief",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x197d4c610>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9956 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "j = im_to_binary(files[0])\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f)\n",
    "    j &= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "personal-color",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x197da6250>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6872 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "j = im_to_binary(files[0])\n",
    "for f in files[1:]:\n",
    "    a = im_to_binary(f)\n",
    "    j |= a\n",
    "io.imshow(img_as_ubyte(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "attached-funds",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aggregate-mongolia",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "material-support",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "smart-norway",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "corresponding-girlfriend",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
