{ "cells": [ { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# Imports\n", "from PIL import Image, ImageFilter\n", "import numpy as np\n", "from transformers import pipeline\n", "import gradio as gr\n", "import os\n", "\n", "model = pipeline(\"image-segmentation\", model=\"facebook/detr-resnet-50-panoptic\")\n", "\n", "pred = []\n", "\n", "def img_resize(image):\n", " width = 1000\n", " width_percent = (width / float(image.size[0]))\n", " height = int((float(image.size[1]) * float(width_percent)))\n", " return image.resize((width, height))\n", "\n", "def image_objects(image):\n", " global pred\n", " image = img_resize(image)\n", " pred = model(image)\n", " pred_object_list = [str(i)+'_'+x['label'] for i, x in enumerate(pred)]\n", " return gr.Dropdown.update(choices = pred_object_list, interactive = True)\n", "\n", "def blurr_object(image, object, blur_strength):\n", " image = img_resize(image)\n", "\n", " object_number = int(object.split('_')[0])\n", " mask_array = np.asarray(pred[object_number]['mask'])/255\n", " image_array = np.asarray(image)\n", "\n", " mask_array_three_channel = np.zeros_like(image_array)\n", " mask_array_three_channel[:,:,0] = mask_array\n", " mask_array_three_channel[:,:,1] = mask_array\n", " mask_array_three_channel[:,:,2] = mask_array\n", "\n", " segmented_image = image_array*mask_array_three_channel\n", "\n", " blur_image = np.asarray(image.filter(ImageFilter.GaussianBlur(radius=blur_strength)))\n", " mask_array_three_channel_invert = 1-mask_array_three_channel\n", " blur_image_reverse_mask = blur_image*mask_array_three_channel_invert\n", "\n", " blurred_output_image = Image.fromarray((blur_image_reverse_mask).astype(np.uint8)+segmented_image.astype(np.uint8))\n", "\n", " return blurred_output_image\n", "\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7862/\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/gradio/routes.py\", line 256, in run_predict\n", " fn_index, raw_input, username, session_state\n", " File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/gradio/blocks.py\", line 599, in process_api\n", " predictions, duration = await self.call_function(fn_index, processed_input)\n", " File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/gradio/blocks.py\", line 515, in call_function\n", " block_fn.fn, *processed_input, limiter=self.limiter\n", " File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/anyio/to_thread.py\", line 32, in run_sync\n", " func, *args, cancellable=cancellable, limiter=limiter\n", " File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/anyio/_backends/_asyncio.py\", line 937, in run_sync_in_worker_thread\n", " return await future\n", " File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/anyio/_backends/_asyncio.py\", line 867, in run\n", " result = context.run(func, *args)\n", " File \"/tmp/ipykernel_6335/646230931.py\", line 20, in image_objects\n", " image = img_resize(image)\n", " File \"/tmp/ipykernel_6335/646230931.py\", line 14, in img_resize\n", " width_percent = (width / float(image.size[0]))\n", "AttributeError: 'NoneType' object has no attribute 'size'\n" ] } ], "source": [ "app = gr.Blocks()\n", "\n", "\n", "with app:\n", " gr.Markdown(\n", " \"\"\"\n", " ## Portrait Photo Generator\n", " - Create stunning portrait photos by blurring the background of your selected object.\n", " - Adjust the blurring strength using the slider.\n", " \"\"\")\n", " with gr.Row():\n", " with gr.Column():\n", " gr.Markdown(\n", " \"\"\"\n", " ### Input Image\n", " \"\"\")\n", " image_input = gr.Image(type=\"pil\")\n", " \n", "\n", " with gr.Column():\n", " with gr.Row():\n", " gr.Markdown(\n", " \"\"\"\n", " ### Found Objects\n", " \"\"\")\n", " with gr.Row():\n", " blur_slider = gr.Slider(minimum=0.5, maximum=10, value=3, label=\"Adject Blur Strength\")\n", " with gr.Row():\n", " object_output = gr.Dropdown(label=\"Select Object From Dropdown\")\n", "\n", " \n", " with gr.Row():\n", " with gr.Column():\n", " gr.Markdown(\n", " \"\"\"\n", " ### Blurred Image Output\n", " \"\"\")\n", " image_output = gr.Image()\n", " with gr.Column():\n", " gr.Markdown(\n", " \"\"\"\n", " ### Example Images\n", " \"\"\")\n", " gr.Examples(\n", " examples=[\n", " \"test_images/dog_horse_cowboy.jpg\",\n", " \"test_images/woman_and_dog.jpg\",\n", " \"test_images/family_in_sofa.jpg\",\n", " \"test_images/group_of_friends.jpg\",\n", " \"test_images/people_group.jpg\"\n", " ],\n", " fn=image_objects,\n", " inputs=image_input, \n", " outputs=object_output)\n", "\n", " image_input.change(fn=image_objects, \n", " inputs=image_input, \n", " outputs=object_output\n", " )\n", "\n", " object_output.change(fn=blurr_object, \n", " inputs=[image_input, object_output, blur_slider],\n", " outputs=image_output)\n", "\n", " blur_slider.change(fn=blurr_object, \n", " inputs=[image_input, object_output, blur_slider],\n", " outputs=image_output)\n", " \n", "\n", "app.launch(debug=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('base')", "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" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "d03b47a5a9bfc09262d9700ac41deff9ead9ff3e21241983433bdf2900140cd2" } } }, "nbformat": 4, "nbformat_minor": 2 }