From a41ad81106728bb2b8cd5c8ed377909b38edf5fd Mon Sep 17 00:00:00 2001 From: Paul Ruiz Date: Fri, 15 Sep 2023 14:54:06 -0600 Subject: [PATCH] added python face stylizer example --- ...ediaPipe_Python_Tasks]_Face_Stylizer.ipynb | 243 ++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 examples/face_stylizer/python/[MediaPipe_Python_Tasks]_Face_Stylizer.ipynb diff --git a/examples/face_stylizer/python/[MediaPipe_Python_Tasks]_Face_Stylizer.ipynb b/examples/face_stylizer/python/[MediaPipe_Python_Tasks]_Face_Stylizer.ipynb new file mode 100644 index 00000000..790e49b8 --- /dev/null +++ b/examples/face_stylizer/python/[MediaPipe_Python_Tasks]_Face_Stylizer.ipynb @@ -0,0 +1,243 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "h2q27gKz1H20" + }, + "source": [ + "##### Copyright 2023 The MediaPipe Authors. All Rights Reserved." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "TUfAcER1oUS6" + }, + "outputs": [], + "source": [ + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L_cQX8dWu4Dv" + }, + "source": [ + "# Face Stylizer\n", + "\n", + "This notebook shows you how to use the MediaPipe Tasks Python API to generate stylize faces given a model and input image. This is an experimental feature, so please report any bugs or issues in this sample through the [GitHub sample repo](https://github.com/googlesamples/mediapipe)." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O6PN9FvIx614" + }, + "source": [ + "## Imports and Setup\n", + "Let's start with installing MediaPipe and the related dependencies." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "gxbHBsF-8Y_l" + }, + "outputs": [], + "source": [ + "!pip install mediapipe" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a49D7h4TVmru" + }, + "source": [ + "## Download the face stylizer model" + ] + }, + { + "cell_type": "markdown", + "source": [ + "The next thing you will need to do is download the face stylizer model that will be used for this demo. This model is already trained with a pre-determined style that will be used for face stylization." + ], + "metadata": { + "id": "QHXsuWDxpOj4" + } + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "OMjuVQiDYJKF" + }, + "outputs": [], + "source": [ + "#@title Start downloading here.\n", + "!wget -O face_stylizer.task -q https://storage.googleapis.com/mediapipe-models/face_stylizer/blaze_face_stylizer/float32/latest/face_stylizer_color_sketch.task" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "83PEJNp9yPBU" + }, + "source": [ + "## Download a test image\n", + "\n", + "After downloading the model, you will need to download an image that you can use for testing! It's worth noting that while this is working with a single image, you can download a collection of images to store in the `IMAGE_FILENAMES` array." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "tzXuqyIBlXer" + }, + "outputs": [], + "source": [ + "import urllib\n", + "IMAGE_FILENAMES = ['portrait.jpg']\n", + "\n", + "for name in IMAGE_FILENAMES:\n", + " url = f'https://storage.googleapis.com/mediapipe-assets/{name}'\n", + " urllib.request.urlretrieve(url, name)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "P8XRmapjySMN" + }, + "source": [ + "## Preview the downloaded image" + ] + }, + { + "cell_type": "markdown", + "source": [ + "You can ensure that everything has downloaded correctly by using the following code to display the downloaded image." + ], + "metadata": { + "id": "Eu_q-Z03r-ed" + } + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "8rjHk72-lmHX" + }, + "outputs": [], + "source": [ + "import cv2\n", + "from google.colab.patches import cv2_imshow\n", + "import math\n", + "\n", + "# Height and width that will be used by the model\n", + "DESIRED_HEIGHT = 480\n", + "DESIRED_WIDTH = 480\n", + "\n", + "# Performs resizing and showing the image\n", + "def resize_and_show(image):\n", + " h, w = image.shape[:2]\n", + " if h < w:\n", + " img = cv2.resize(image, (DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH))))\n", + " else:\n", + " img = cv2.resize(image, (math.floor(w/(h/DESIRED_HEIGHT)), DESIRED_HEIGHT))\n", + " cv2_imshow(img)\n", + "\n", + "\n", + "# Preview the image(s)\n", + "images = {name: cv2.imread(name) for name in IMAGE_FILENAMES}\n", + "for name, image in images.items():\n", + " print(name)\n", + " resize_and_show(image)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Iy4r2_ePylIa" + }, + "source": [ + "## Running inference and visualizing the results\n", + "To run inference using the face stylizer MediaPipe Task, you will need to initialize the `FaceStylizer` using the model. Once the stylizer has been initialized, you can use the sytlize method to apply the pre-trained model style onto your image." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Yl_Oiye4mUuo" + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import mediapipe as mp\n", + "\n", + "from mediapipe.tasks import python\n", + "from mediapipe.tasks.python import vision\n", + "\n", + "\n", + "# Create the options that will be used for FaceStylizer\n", + "base_options = python.BaseOptions(model_asset_path='face_stylizer.task')\n", + "options = vision.FaceStylizerOptions(base_options=base_options)\n", + "\n", + "# Create the face stylizer\n", + "with vision.FaceStylizer.create_from_options(options) as stylizer:\n", + "\n", + " # Loop through demo image(s)\n", + " for image_file_name in IMAGE_FILENAMES:\n", + "\n", + " # Create the MediaPipe image file that will be stylized\n", + " image = mp.Image.create_from_file(image_file_name)\n", + " # Retrieve the stylized image\n", + " stylized_image = stylizer.stylize(image)\n", + "\n", + " # Show the stylized image\n", + " rgb_stylized_image = cv2.cvtColor(stylized_image.numpy_view(), cv2.COLOR_BGR2RGB)\n", + " resize_and_show(rgb_stylized_image)" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file