From a4ea2852f4b46566a61f988342aa04e4059ccef9 Mon Sep 17 00:00:00 2001 From: adamhrv Date: Tue, 8 Oct 2019 16:02:33 +0200 Subject: add notebooks --- .../datasets/helen/facial_landmarks.ipynb | 211 +++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 megapixels/notebooks/datasets/helen/facial_landmarks.ipynb (limited to 'megapixels/notebooks/datasets/helen') diff --git a/megapixels/notebooks/datasets/helen/facial_landmarks.ipynb b/megapixels/notebooks/datasets/helen/facial_landmarks.ipynb new file mode 100644 index 00000000..d9bbc24b --- /dev/null +++ b/megapixels/notebooks/datasets/helen/facial_landmarks.ipynb @@ -0,0 +1,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 +} -- cgit v1.2.3-70-g09d2