From 2efde746810a0264ad2cf09dc9b003bfcd17a4d5 Mon Sep 17 00:00:00 2001 From: adamhrv Date: Sun, 6 Jan 2019 14:29:35 +0100 Subject: clean up 3d face plot --- .../notebooks/face_analysis/3d_face_plot.ipynb | 460 +++++++++++---------- 1 file changed, 232 insertions(+), 228 deletions(-) mode change 100755 => 100644 megapixels/notebooks/face_analysis/3d_face_plot.ipynb (limited to 'megapixels/notebooks/face_analysis/3d_face_plot.ipynb') diff --git a/megapixels/notebooks/face_analysis/3d_face_plot.ipynb b/megapixels/notebooks/face_analysis/3d_face_plot.ipynb old mode 100755 new mode 100644 index 537e471b..f136015f --- a/megapixels/notebooks/face_analysis/3d_face_plot.ipynb +++ b/megapixels/notebooks/face_analysis/3d_face_plot.ipynb @@ -11,18 +11,15 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 88, "metadata": {}, "outputs": [ { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'face_alignment'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mrandom\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mrandint\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/work/megapixels_dev/3rdparty/face-alignment'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mface_alignment\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mmpl_toolkits\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmplot3d\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mAxes3D\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'face_alignment'" + "name": "stdout", + "output_type": "stream", + "text": [ + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n" ] } ], @@ -33,6 +30,9 @@ "from os.path import join\n", "import sys\n", "import time\n", + "from random import randint\n", + "import random\n", + "\n", "import cv2 as cv\n", "import numpy as np\n", "import imutils\n", @@ -41,22 +41,24 @@ "from glob import glob\n", "from matplotlib import cbook\n", "from matplotlib import cm\n", - "from matplotlib.colors import LightSource\n", - "from random import randint\n", - "sys.path.append('/work/megapixels_dev/3rdparty/face-alignment')\n", + "#from matplotlib.colors import LightSource\n", "import face_alignment\n", "import numpy as np\n", + "\n", "from mpl_toolkits.mplot3d import Axes3D\n", "import matplotlib.pyplot as plt\n", "import mpl_toolkits.mplot3d.axes3d as p3\n", "from matplotlib import animation\n", - "import random\n", - "from skimage import io" + "\n", + "from skimage import io\n", + "from tqdm import tqdm_notebook as tqdm\n", + "from IPython.display import clear_output\n", + "from pathlib import Path" ] }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 89, "metadata": {}, "outputs": [], "source": [ @@ -68,151 +70,102 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "# init 3d face\n", "# Run the 3D face alignment on a test image, without CUDA.\n", - "fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._3D, \n", - " enable_cuda=True, flip_input=True)" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "80\n" - ] - } - ], - "source": [ - "data_bodega = '../data_bodega/'\n", - "fp = join(data_bodega,'images/msceleb/**/*.jpg')\n", - "face_files = glob(fp,recursive=True)\n", - "face_files = [f for f in face_files if os.path.basename(f) == '0-FaceId-0.jpg']\n", - "print(len(face_files))" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "../data_bodega/images/msceleb/m.0k014/0-FaceId-0.jpg\n" - ] - } - ], - "source": [ - "fp_face = face_files[randint(0,len(face_files)-1)]\n", - "im = io.imread(fp_face)" + "fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._3D, device='cuda:0', flip_input=True)" ] }, { "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [], - "source": [ - "preds = fa.get_landmarks(im)[-1]" - ] - }, - { - "cell_type": "code", - "execution_count": 41, + "execution_count": 276, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "(31.0, 256.0) (97.0, 293.0) (-101.42858, 71.526764)\n" - ] + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 276, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "xmm = (np.min(preds[:,0]),np.max(preds[:,0]))\n", - "ymm = (np.min(preds[:,1]),np.max(preds[:,1]))\n", - "zmm = (np.min(preds[:,2]),np.max(preds[:,2]))\n", - "print(xmm,ymm,zmm)" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [], - "source": [ - "# # draw person\n", - "# fig = plt.figure(figsize=plt.figaspect(.5))\n", - "# ax = fig.add_subplot(1, 2, 1)\n", - "# ax.imshow(im)\n", - "# ax.plot(preds[0:17,0],preds[0:17,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[17:22,0],preds[17:22,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[22:27,0],preds[22:27,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[27:31,0],preds[27:31,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[31:36,0],preds[31:36,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[36:42,0],preds[36:42,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[42:48,0],preds[42:48,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[48:60,0],preds[48:60,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", - "# ax.plot(preds[60:68,0],preds[60:68,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) \n", - "# ax.axis('off')\n", - "# plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [], - "source": [ - "preds_orig = preds.copy()" + "fp_im = '/data_store_hdd/datasets/people/vgg_face2/media/original/test/n000009/0012_01.jpg'\n", + "im = cv.imread(fp_im)\n", + "plt.imshow(im)" ] }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 287, "metadata": {}, "outputs": [], - "source": [ - "xmm_sc = (1.2*np.min(preds[:,0]),1.2*np.max(preds_orig[:,0]))\n", - "xmm = (np.min(preds_orig[:,0]),np.max(preds_orig[:,0]))\n", - "ymm = (np.min(preds_orig[:,1]),np.max(preds_orig[:,1]))\n", - "zmm = (np.min(preds_orig[:,2]),np.max(preds_orig[:,2]))\n" - ] + "source": [] }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 288, "metadata": {}, "outputs": [], "source": [ - "#$preds = np.array( [p[0]-xmm[0], p[1]-ymm[1], p[2]-zmm[0]] for p in preds_orig)\n", - "preds = np.zeros_like(preds_orig).astype(np.uint8)\n", - "for i,p in enumerate(preds_orig):\n", - " x,y,z = p\n", - " preds[i] = np.array([x - xmm[0], y - ymm[0], z - zmm[0]])\n", - " preds[i] = np.array([x - xmm[0], z - zmm[0], y - ymm[0]])\n", - " #preds[i] = np.array([x - xmm[0], abs((z - zmm[0])+zmm[0]), y - ymm[0]])\n", + "def generate_3d_face_plain(im, lm):\n", + " preds = lm\n", + " fig = plt.figure(figsize=plt.figaspect(.5))\n", + " ax = fig.add_subplot(1, 2, 1)\n", + " ax.imshow(im)\n", + " ax.plot(preds[0:17,0],preds[0:17,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[17:22,0],preds[17:22,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[22:27,0],preds[22:27,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[27:31,0],preds[27:31,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[31:36,0],preds[31:36,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[36:42,0],preds[36:42,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[42:48,0],preds[42:48,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[48:60,0],preds[48:60,1],marker='o',markersize=6,linestyle='-',color='w',lw=2)\n", + " ax.plot(preds[60:68,0],preds[60:68,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) \n", + " ax.axis('off')\n", + "\n", + " ax = fig.add_subplot(1, 2, 2, projection='3d')\n", + " surf = ax.scatter(preds[:,0]*1.2,preds[:,1],preds[:,2],c=\"cyan\", alpha=1.0, edgecolor='b')\n", + " ax.plot3D(preds[:17,0]*1.2,preds[:17,1], preds[:17,2], color='blue' )\n", + " ax.plot3D(preds[17:22,0]*1.2,preds[17:22,1],preds[17:22,2], color='blue')\n", + " ax.plot3D(preds[22:27,0]*1.2,preds[22:27,1],preds[22:27,2], color='blue')\n", + " ax.plot3D(preds[27:31,0]*1.2,preds[27:31,1],preds[27:31,2], color='blue')\n", + " ax.plot3D(preds[31:36,0]*1.2,preds[31:36,1],preds[31:36,2], color='blue')\n", + " ax.plot3D(preds[36:42,0]*1.2,preds[36:42,1],preds[36:42,2], color='blue')\n", + " ax.plot3D(preds[42:48,0]*1.2,preds[42:48,1],preds[42:48,2], color='blue')\n", + " ax.plot3D(preds[48:,0]*1.2,preds[48:,1],preds[48:,2], color='blue' )\n", + " \n", + " # pad\n", + " xmm = (np.min(lm[:,0]),np.max(lm[:,0]))\n", + " ymm = (np.min(lm[:,1]),np.max(lm[:,1]))\n", + " zmm = (np.min(lm[:,2]),np.max(lm[:,2]))\n", " \n", - "#preds = np.rot90(preds,2,axes=(0,1))" + " print(xmm, ymm, zmm)\n", + "# ax.set_xticks([])\n", + "# ax.set_yticks([])\n", + "# ax.set_zticks([])\n", + " plt.setp( ax.get_xticklabels(), visible=False)\n", + " plt.setp( ax.get_yticklabels(), visible=False)\n", + " #ax.set_xlim(xmm[0]-50, xmm[1]+50)\n", + " #ax.set_ylim(ymm[0]-50, ymm[1]+50)\n", + " #ax.set_ylim(zmm[0]- .1*zmm[0],zmm[1] + .1*zmm[1])\n", + " #ax.set_ylim(103, 275)\n", + " #ax.set_zlim((-100,100))\n", + " ax.view_init(elev=15., azim=135.)\n", + "\n", + " plt.show()" ] }, { "cell_type": "code", - "execution_count": 49, - "metadata": { - "scrolled": false - }, + "execution_count": 367, + "metadata": {}, "outputs": [ { "data": { @@ -997,7 +950,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1010,123 +963,167 @@ "name": "stdout", "output_type": "stream", "text": [ - "(31.0, 256.0) (97.0, 293.0) (-101.42858, 71.526764)\n", - "143.0 195.0 -15.428581237792969\n" + "(68.0, 201.0) (110.0, 225.0) (-61.021374, 41.419292)\n" ] } ], "source": [ - "#TODO: Make this nice\n", - "fig = plt.figure(figsize=(16,16),dpi=72)\n", - "ax = fig.add_subplot(111, projection='3d')\n", - "\n", - "preds_scaled = np.array([1.2*x,y,z] for x,y,z in preds)\n", - "\n", - "preds_plot = np.zeros_like(preds)\n", - "for i,p in enumerate(preds):\n", - " x,y,z = p\n", - " preds_plot[i] = np.array([x,y,z])\n", - "\n", - "clr1 = '#00ff00'\n", - "clr2 = '#00ff00'\n", - "\n", - "clr1 = '#ff0000'\n", - "clr2 = '#ff0000'\n", - "\n", - "ax.set_facecolor('black')\n", - "#ax.set_facecolor((1, 0, 0))\n", - "\n", - "ls = LightSource(270, 45)\n", - "# To use a custom hillshading mode, override the built-in shading and pass\n", - "# in the rgb colors of the shaded surface calculated from \"shade\".\n", - "#rgb = ls.shade(preds[:,1], cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')\n", - "rh = rhex()\n", - "surf = ax.scatter(preds_plot[:,0]*1.2,preds_plot[:,1],preds_plot[:,2],c=rh, \n", - " alpha=1.0, s=65, edgecolor=rh)\n", - "#facecolors=rgb,linewidth=0, antialiased=False, shade=Fals\n", - "\n", - "\n", - "#surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,\n", - "# linewidth=0, antialiased=False, shade=False)\n", - "\n", - "\n", - "#surf.actor.property.specular = 0.1\n", - "#surf.actor.property.specular_power = 5\n", - "\n", - "lw = 3\n", - "ax.plot3D(preds_plot[:17,0]*1.2,preds_plot[:17,1], preds_plot[:17,2], color=rhex(),linewidth=lw)\n", - "ax.plot3D(preds_plot[17:22,0]*1.2,preds_plot[17:22,1],preds_plot[17:22,2], color=rhex(),linewidth=lw)\n", - "ax.plot3D(preds_plot[22:27,0]*1.2,preds_plot[22:27,1],preds_plot[22:27,2], color=rhex(),linewidth=lw)\n", - "ax.plot3D(preds_plot[27:31,0]*1.2,preds_plot[27:31,1],preds_plot[27:31,2], color=rhex(),linewidth=lw)\n", - "ax.plot3D(preds_plot[31:36,0]*1.2,preds_plot[31:36,1],preds_plot[31:36,2], color=rhex(),linewidth=lw)\n", - "ax.plot3D(preds_plot[36:42,0]*1.2,preds_plot[36:42,1],preds_plot[36:42,2], color=rhex(),linewidth=lw)\n", - "ax.plot3D(preds_plot[42:48,0]*1.2,preds_plot[42:48,1],preds_plot[42:48,2], color=rhex(),linewidth=lw)\n", - "ax.plot3D(preds_plot[48:,0]*1.2,preds_plot[48:,1],preds_plot[48:,2], color=rhex(), linewidth=lw)\n", - "\n", - "\n", - "print(xmm,ymm,zmm)\n", - "# ax.set_xlim3d(left=xmm[0],right=xmm[1])\n", - "# ax.set_ylim3d(bottom=ymm[0],top=ymm[1])\n", - "# ax.set_zlim3d(bottom=zmm[0],top=zmm[1])\n", - "\n", - "cx = ((xmm[0] - xmm[1]) // 2) + xmm[1]\n", - "cy = ((ymm[1] - ymm[0]) // 2) + ymm[0]\n", - "cz = ((zmm[1] - zmm[0]) // 2) + zmm[0]\n", - "print(cx,cy,cz)\n", - "\n", - "xpts = [cx,cx]\n", - "ypts = [cy,cy]\n", - "zpts = [zmm[0],zmm[1]]\n", - "\n", - "#ax.plot3D(xpts,ypts,zpts,color='red')\n", - "\n", - "# need to make this dynamic, sigh\n", - "ax.view_init(elev=120., azim=70.)\n", - "#ax.view_init(elev=90., azim=40.)\n", - "\n", - "margin = 20\n", + "im = cv.imread(fp_im)\n", + "im_rgb = cv.cvtColor(im, cv.COLOR_BGR2RGB)\n", + "lm = fa.get_landmarks(im_rgb)[-1]\n", + "generate_3d_face_plain(im_rgb, lm)" + ] + }, + { + "cell_type": "code", + "execution_count": 393, + "metadata": {}, + "outputs": [], + "source": [ + " # line weight\n", + "def generate_3d_face(lm, fp_out, num_frames=30, dpi=72, stroke_weight=2, size=(480,480),\n", + " mark_size=10, mark_type='.', mark_clr=(0,255,0), fps=10, transparent=False):\n", + " '''Generates 3D plot of face landmarks\n", + " '''\n", + " # convert opencv BGR numpy image to RGB\n", + " bg_color = '#%02x%02x%02x' % (0,0,0)\n", + " mark_clr = '#%02x%02x%02x' % (0,255,255)\n", + " \n", + " # scale to make larger\n", + " #lm = np.array([1.2*x,y,z] for x,y,z in list(lm))\n", + " \n", + " # center x,y,z\n", + " xmm = (np.min(lm[:,0]),np.max(lm[:,0]))\n", + " ymm = (np.min(lm[:,1]),np.max(lm[:,1]))\n", + " zmm = (np.min(lm[:,2]),np.max(lm[:,2]))\n", + " \n", + " # make copy of landmarks\n", + " lm_orig = lm.copy()\n", + " xmm = (np.min(lm_orig[:,0]),np.max(lm_orig[:,0]))\n", + " ymm = (np.min(lm_orig[:,1]),np.max(lm_orig[:,1]))\n", + " zmm = (np.min(lm_orig[:,2]),np.max(lm_orig[:,2]))\n", + " \n", + " # swap the y and z components to improve 3d rotation angles for matplotlib\n", + " lm = np.zeros_like(lm_orig).astype(np.uint8)\n", + " for i,p in enumerate(lm_orig):\n", + " x,y,z = p\n", + " lm[i] = np.array([x - xmm[0], z - zmm[0], y - ymm[0]])\n", + " \n", + " # Create plot\n", + " figsize = (size[0]/dpi, size[1]/dpi )\n", + " fig = plt.figure(figsize=figsize, dpi=dpi) # frameon=False\n", + " #fig.set_size_inches(100/100, 1, forward=False)\n", + " fig.tight_layout()\n", + " fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)\n", + " ax = fig.add_subplot(111, projection='3d')\n", + " ax.set_facecolor(bg_color) # background color\n", + " \n", + " xscale, yscale, zscale = (1.2, 1.0, 1.0)\n", + " \n", + " # scatter plot the dots\n", + " # jaw line\n", + " mark_clr = '#%02x%02x%02x' % (0,255,0) # green\n", + " ax.plot3D(lm[:17,0]*1.2,lm[:17,1], lm[:17,2],\n", + " marker=mark_type, markersize=mark_size, color=mark_clr,linewidth=stroke_weight)\n", + " # stage-right eyebrow\n", + " mark_clr = '#%02x%02x%02x' % (255,0,0) # green\n", + " ax.plot3D(lm[17:22,0]*1.2,lm[17:22,1],lm[17:22,2],\n", + " marker=mark_type, markersize=mark_size, color=mark_clr,linewidth=stroke_weight)\n", + " # stage-left eyebrow\n", + " mark_clr = '#%02x%02x%02x' % (255,255,0) # yellow\n", + " ax.plot3D(lm[22:27,0]*1.2,lm[22:27,1],lm[22:27,2], \n", + " marker=mark_type, markersize=mark_size, color=mark_clr,linewidth=stroke_weight)\n", + " # nose ridge\n", + " mark_clr = '#%02x%02x%02x' % (0,0,255) # blue\n", + " ax.plot3D(lm[27:31,0]*1.2,lm[27:31,1],lm[27:31,2],\n", + " marker=mark_type, markersize=mark_size, color=mark_clr,linewidth=stroke_weight)\n", + " # nose-bottom\n", + " mark_clr = '#%02x%02x%02x' % (255,0,255) # magenta\n", + " ax.plot3D(lm[31:36,0]*1.2,lm[31:36,1],lm[31:36,2],\n", + " marker=mark_type, markersize=mark_size, color=mark_clr,linewidth=stroke_weight)\n", + " # stage-left eye\n", + " mark_clr = '#%02x%02x%02x' % (0,255,255) # cyan\n", + " px, py, pz = lm[36:42,0]*1.2,lm[36:42,1],lm[36:42,2]\n", + " px = np.append(px, lm[36,0]*1.2)\n", + " py = np.append(py, lm[36,1])\n", + " pz = np.append(pz, lm[36,2])\n", + " ax.plot3D(px, py, pz, marker=mark_type, markersize=mark_size, color=mark_clr,linewidth=stroke_weight)\n", + " \n", + " # stage-right eye\n", + " mark_clr = '#%02x%02x%02x' % (255,255,255) # white\n", + " px, py, pz = lm[42:48,0]*1.2,lm[42:48,1],lm[42:48,2]\n", + " px = np.append(px, lm[42,0]*1.2)\n", + " py = np.append(py, lm[42,1])\n", + " pz = np.append(pz, lm[42,2])\n", + " ax.plot3D(px, py, pz, marker=mark_type, markersize=mark_size, color=mark_clr,linewidth=stroke_weight)\n", + " \n", + " # mouth\n", + " mark_clr = '#%02x%02x%02x' % (255,125,0) # orange?\n", + " px, py, pz = lm[48:,0]*1.2,lm[48:,1],lm[48:,2]\n", + " px = np.append(px, lm[48,0]*1.2)\n", + " py = np.append(py, lm[48,1])\n", + " pz = np.append(pz, lm[48,2])\n", + " ax.plot3D(px, py, pz, marker=mark_type, markersize=mark_size, color=mark_clr, linewidth=stroke_weight)\n", + " \n", + " rh = '#00ff00' # edge color\n", + " #ax.scatter(lm[:,0]*xscale,lm[:,1]*yscale,lm[:,2]*zscale, c=rh, alpha=1.0, s=35, edgecolor=rh)\n", + " #ax.scatter(lm[:,0]*xscale,lm[:,1]*yscale,lm[:,2]*zscale, c=rh, alpha=1.0, s=1)\n", + " \n", + " # center center x,y,z points\n", + " cx = ((xmm[0] - xmm[1]) // 2) + xmm[1]\n", + " cy = ((ymm[1] - ymm[0]) // 2) + ymm[0]\n", + " cz = ((zmm[1] - zmm[0]) // 2) + zmm[0]\n", + " \n", + " # set initial plot view\n", + " ax.view_init(elev=120., azim=70.)\n", + " \n", + " # remove ticks\n", + " ax.set_xticks([])\n", + " ax.set_yticks([])\n", + " ax.set_zticks([])\n", + " \n", + " # remove axis\n", + " ax.set_frame_on(False)\n", + " ax.set_axis_off()\n", "\n", - "#ax.set_xlim(xmm[0] - margin,xmm[1] + margin)\n", - "#ax.set_ylim(ymm[0] - margin,ymm[1] + margin)\n", - "#ax.set_zlim(zmm[0] - margin,zmm[1] + margin)\n", + " # rotation increments: from 0 to 360 in num_frames\n", + " phi = np.linspace(0, 2*np.pi, num_frames)\n", "\n", - "ax.set_xticks([])\n", - "ax.set_yticks([])\n", - "ax.set_zticks([])\n", - "ax.set_axis_off()\n", - "ax.set_xlabel('x')\n", - "ax.set_ylabel('y')\n", - "ax.set_zlabel('z')\n", - "plt.savefig('test.png', dpi=80)\n", - "plt.show()\n" + " def update(phi):\n", + " ax.view_init(180,phi*180./np.pi)\n", + " \n", + " ani = matplotlib.animation.FuncAnimation(fig, update, frames=phi)\n", + " savefig_kwargs = {'pad_inches': 0, 'transparent': transparent}\n", + " ani.save(fp_out, writer='imagemagick', fps=fps, savefig_kwargs=savefig_kwargs)\n", + " clear_output()" ] }, { "cell_type": "code", - "execution_count": 50, - "metadata": {}, + "execution_count": 394, + "metadata": { + "scrolled": false + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "../data_bodega/output/m.0k014.gif\n" + "time: 0.0017\n", + "Saved file to /home/adam/Downloads/0012_01.gif\n" ] } ], "source": [ - "fname_out = join(data_bodega,'output', '{}.gif'.format(os.path.basename(os.path.dirname(fp_face))))\n", - "\n", - "phi = np.linspace(0, 2*np.pi,60)\n", - "\n", - "def update(phi):\n", - " ax.view_init(180,phi*180./np.pi)\n", - " #ax.view_init(90,phi*90./np.pi)\n", - " #ax.view_init(elev=90., azim=90.)\n", + "# filepaths\n", + "dir_out = '/home/adam/Downloads/'\n", + "fp_out = join(dir_out, '{}.gif'.format(Path(fp_im).stem))\n", "\n", - "ani = matplotlib.animation.FuncAnimation(fig, update, frames=phi)\n", - "ani.save(fname_out, writer='imagemagick', fps=10)\n", - "#plt.show()" + "# generate 3D face\n", + "st = time.time()\n", + "generate_3d_face(lm,fp_out, num_frames=20, fps=10)\n", + "print('time: {:.4f}'.format((time.time()-st)/1000))\n", + "print(f'Saved file to {fp_out}')" ] }, { @@ -1136,6 +1133,13 @@ "outputs": [], "source": [] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -1146,9 +1150,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python [default]", + "display_name": "Python [conda env:megapixels]", "language": "python", - "name": "python3" + "name": "conda-env-megapixels-py" }, "language_info": { "codemirror_mode": { -- cgit v1.2.3-70-g09d2