Spaces:
Runtime error
Runtime error
File size: 7,762 Bytes
9484fc5 |
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 |
{
"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": [
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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
}
|