summaryrefslogtreecommitdiff
path: root/megapixels/notebooks/datasets/helen/facial_landmarks.ipynb
blob: d9bbc24b7b98eeb7425bfac6cf46b1c154ae0fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HELEN Facial Landmarks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "from os.path import join\n",
    "from glob import glob, iglob\n",
    "from pathlib import Path\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "import random\n",
    "from datetime import datetime\n",
    "\n",
    "import h5py\n",
    "from scipy import misc\n",
    "from io import BytesIO\n",
    "from base64 import b64decode\n",
    "\n",
    "from PIL import Image, ImageDraw\n",
    "import imutils\n",
    "import cv2 as cv\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.io as sio\n",
    "import h5py\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sys\n",
    "sys.path.append('/work/megapixels_dev/megapixels/')\n",
    "from app.utils import file_utils, draw_utils, im_utils\n",
    "from app.models.bbox import BBox"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "fp_anno_dir = '/data_store/datasets/people/helen/downloads/annotation/'\n",
    "fp_annos = glob(join(fp_anno_dir, '*.txt'))\n",
    "fp_dir_ims = '/data_store/datasets/people/helen/media/original/'\n",
    "fp_dir_out = '/data_store/datasets/people/helen/processed/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_landmark_im(fp_anno, fp_dir_ims, size=(640,640), margin=20, radius=2):\n",
    "  \n",
    "  # parse annotation\n",
    "  _annos = file_utils.load_text(fp_anno)\n",
    "  fn = _annos[0]\n",
    "  annos = [list(map(float, a.split(','))) for a in _annos[1:]]\n",
    "  fp_im = join(fp_dir_ims, f'{fn}.jpg')\n",
    "\n",
    "  # load image\n",
    "  im = cv.imread(fp_im)\n",
    "  im_pil = Image.open(fp_im)\n",
    "  h, w = im.shape[:2]\n",
    "  \n",
    "  # normalize points and get min max and bbox\n",
    "  points_norm = [(x/w, y/h) for x,y in annos]\n",
    "  points_norm_np = np.array(points_norm)\n",
    "  points_x, points_y = (points_norm_np[:,0], points_norm_np[:,1])\n",
    "  x1, y1 = (min(points_x), min(points_y))\n",
    "  x2, y2 = (max(points_x), max(points_y))\n",
    "  bbox = BBox.from_xyxy(x1, y1, x2, y2)\n",
    "  #bbox_exp = bbox.expand(.2).to_square()\n",
    "  \n",
    "  # create offset and redraw\n",
    "  adjw, adjh = (1 + (2*margin)/im_size[0], 1 + (2*margin)/im_size[1])\n",
    "  points_norm_offset = [((x - bbox.x1) / (bbox.w * adjw), (y - bbox.y1) / (bbox.h * adjh)) for x, y in points_norm]\n",
    "  points_norm_offset  = [(x + margin/im_size[0], y + margin/im_size[1]) for x, y in points_norm_offset]\n",
    "  im_border = np.zeros([size[0], size[1], 3], dtype=np.uint8)\n",
    "  #im_border = draw_utils.draw_landmarks2D(im_border, points_norm_offset, radius=radius, color=(255,255,255))\n",
    "  im_border = draw_utils.draw_landmarks2D_pil(im_border, points_norm_offset, radius=radius, color=(255,255,255))\n",
    "  return im_border\n",
    "\n",
    "def make_transparent(im, bg_color=(0,0,0)):\n",
    "  indices = np.all(im == bg_color, axis=-1)\n",
    "  im = cv.cvtColor(im, cv.COLOR_BGR2BGRA)\n",
    "  im[indices] = [0, 0, 0, 0]\n",
    "  return im"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a montage\n",
    "for n_iter in range(20):\n",
    "  ims = []\n",
    "  im_size = (360,360)\n",
    "  cols, rows = (3, 4)\n",
    "  \n",
    "  for i in range(cols*rows):\n",
    "    fp_anno_rn = fp_annos[random.randint(0, len(fp_annos))]\n",
    "    im_lms = create_landmark_im(fp_anno_rn, fp_dir_ims, size=im_size, margin=50)\n",
    "    ims.append(im_lms)\n",
    "  \n",
    "  ims_montaged = imutils.build_montages(ims, im_size, (cols, rows))\n",
    "  im_montage = ims_montaged[0]\n",
    "  \n",
    "  # make transparent\n",
    "  im_montage = make_transparent(im_montage)\n",
    "  \n",
    "  # save\n",
    "  d = datetime.now()\n",
    "  fname = f'montage_lms_{d.day}_{d.hour}_{d.hour}_{d.minute}_{d.second}_{n_iter}.png'\n",
    "  fp_out = join(fp_dir_out, fname)\n",
    "  cv.imwrite(fp_out, im_montage)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single Faces"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "im_size = (640, 640)\n",
    "fp_anno_rn = fp_annos[random.randint(0, len(fp_annos))]\n",
    "im_lms = create_landmark_im(fp_anno_rn, fp_dir_ims, size=im_size, margin=100, radius=4)\n",
    "im_lms = make_transparent(im_lms)\n",
    "d = datetime.now()\n",
    "fname = f'single_{d.day}_{d.hour}_{d.hour}_{d.minute}_{d.second}_{n_iter}.png'\n",
    "fname = 'single.png'\n",
    "fp_out = join(fp_dir_out, fname)\n",
    "cv.imwrite(fp_out, im_lms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "megapixels",
   "language": "python",
   "name": "megapixels"
  },
  "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}