Spaces:
Runtime error
Runtime error
Delete gradio.ipynb
Browse files- gradio.ipynb +0 -209
gradio.ipynb
DELETED
@@ -1,209 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "code",
|
5 |
-
"execution_count": 14,
|
6 |
-
"metadata": {},
|
7 |
-
"outputs": [],
|
8 |
-
"source": [
|
9 |
-
"# Imports\n",
|
10 |
-
"from PIL import Image, ImageFilter\n",
|
11 |
-
"import numpy as np\n",
|
12 |
-
"from transformers import pipeline\n",
|
13 |
-
"import gradio as gr\n",
|
14 |
-
"import os\n",
|
15 |
-
"\n",
|
16 |
-
"model = pipeline(\"image-segmentation\", model=\"facebook/detr-resnet-50-panoptic\")\n",
|
17 |
-
"\n",
|
18 |
-
"pred = []\n",
|
19 |
-
"\n",
|
20 |
-
"def img_resize(image):\n",
|
21 |
-
" width = 1000\n",
|
22 |
-
" width_percent = (width / float(image.size[0]))\n",
|
23 |
-
" height = int((float(image.size[1]) * float(width_percent)))\n",
|
24 |
-
" return image.resize((width, height))\n",
|
25 |
-
"\n",
|
26 |
-
"def image_objects(image):\n",
|
27 |
-
" global pred\n",
|
28 |
-
" image = img_resize(image)\n",
|
29 |
-
" pred = model(image)\n",
|
30 |
-
" pred_object_list = [str(i)+'_'+x['label'] for i, x in enumerate(pred)]\n",
|
31 |
-
" return gr.Dropdown.update(choices = pred_object_list, interactive = True)\n",
|
32 |
-
"\n",
|
33 |
-
"def blurr_object(image, object, blur_strength):\n",
|
34 |
-
" image = img_resize(image)\n",
|
35 |
-
"\n",
|
36 |
-
" object_number = int(object.split('_')[0])\n",
|
37 |
-
" mask_array = np.asarray(pred[object_number]['mask'])/255\n",
|
38 |
-
" image_array = np.asarray(image)\n",
|
39 |
-
"\n",
|
40 |
-
" mask_array_three_channel = np.zeros_like(image_array)\n",
|
41 |
-
" mask_array_three_channel[:,:,0] = mask_array\n",
|
42 |
-
" mask_array_three_channel[:,:,1] = mask_array\n",
|
43 |
-
" mask_array_three_channel[:,:,2] = mask_array\n",
|
44 |
-
"\n",
|
45 |
-
" segmented_image = image_array*mask_array_three_channel\n",
|
46 |
-
"\n",
|
47 |
-
" blur_image = np.asarray(image.filter(ImageFilter.GaussianBlur(radius=blur_strength)))\n",
|
48 |
-
" mask_array_three_channel_invert = 1-mask_array_three_channel\n",
|
49 |
-
" blur_image_reverse_mask = blur_image*mask_array_three_channel_invert\n",
|
50 |
-
"\n",
|
51 |
-
" blurred_output_image = Image.fromarray((blur_image_reverse_mask).astype(np.uint8)+segmented_image.astype(np.uint8))\n",
|
52 |
-
"\n",
|
53 |
-
" return blurred_output_image\n",
|
54 |
-
"\n"
|
55 |
-
]
|
56 |
-
},
|
57 |
-
{
|
58 |
-
"cell_type": "code",
|
59 |
-
"execution_count": 18,
|
60 |
-
"metadata": {},
|
61 |
-
"outputs": [
|
62 |
-
{
|
63 |
-
"name": "stdout",
|
64 |
-
"output_type": "stream",
|
65 |
-
"text": [
|
66 |
-
"Running on local URL: http://127.0.0.1:7862/\n",
|
67 |
-
"\n",
|
68 |
-
"To create a public link, set `share=True` in `launch()`.\n"
|
69 |
-
]
|
70 |
-
},
|
71 |
-
{
|
72 |
-
"data": {
|
73 |
-
"text/html": [
|
74 |
-
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
75 |
-
],
|
76 |
-
"text/plain": [
|
77 |
-
"<IPython.core.display.HTML object>"
|
78 |
-
]
|
79 |
-
},
|
80 |
-
"metadata": {},
|
81 |
-
"output_type": "display_data"
|
82 |
-
},
|
83 |
-
{
|
84 |
-
"name": "stderr",
|
85 |
-
"output_type": "stream",
|
86 |
-
"text": [
|
87 |
-
"Traceback (most recent call last):\n",
|
88 |
-
" File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/gradio/routes.py\", line 256, in run_predict\n",
|
89 |
-
" fn_index, raw_input, username, session_state\n",
|
90 |
-
" File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/gradio/blocks.py\", line 599, in process_api\n",
|
91 |
-
" predictions, duration = await self.call_function(fn_index, processed_input)\n",
|
92 |
-
" File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/gradio/blocks.py\", line 515, in call_function\n",
|
93 |
-
" block_fn.fn, *processed_input, limiter=self.limiter\n",
|
94 |
-
" File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/anyio/to_thread.py\", line 32, in run_sync\n",
|
95 |
-
" func, *args, cancellable=cancellable, limiter=limiter\n",
|
96 |
-
" 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",
|
97 |
-
" return await future\n",
|
98 |
-
" File \"/home/nybsysml/anaconda3/envs/mediapipe3_7/lib/python3.7/site-packages/anyio/_backends/_asyncio.py\", line 867, in run\n",
|
99 |
-
" result = context.run(func, *args)\n",
|
100 |
-
" File \"/tmp/ipykernel_6335/646230931.py\", line 20, in image_objects\n",
|
101 |
-
" image = img_resize(image)\n",
|
102 |
-
" File \"/tmp/ipykernel_6335/646230931.py\", line 14, in img_resize\n",
|
103 |
-
" width_percent = (width / float(image.size[0]))\n",
|
104 |
-
"AttributeError: 'NoneType' object has no attribute 'size'\n"
|
105 |
-
]
|
106 |
-
}
|
107 |
-
],
|
108 |
-
"source": [
|
109 |
-
"app = gr.Blocks()\n",
|
110 |
-
"\n",
|
111 |
-
"\n",
|
112 |
-
"with app:\n",
|
113 |
-
" gr.Markdown(\n",
|
114 |
-
" \"\"\"\n",
|
115 |
-
" ## Portrait Photo Generator\n",
|
116 |
-
" - Create stunning portrait photos by blurring the background of your selected object.\n",
|
117 |
-
" - Adjust the blurring strength using the slider.\n",
|
118 |
-
" \"\"\")\n",
|
119 |
-
" with gr.Row():\n",
|
120 |
-
" with gr.Column():\n",
|
121 |
-
" gr.Markdown(\n",
|
122 |
-
" \"\"\"\n",
|
123 |
-
" ### Input Image\n",
|
124 |
-
" \"\"\")\n",
|
125 |
-
" image_input = gr.Image(type=\"pil\")\n",
|
126 |
-
" \n",
|
127 |
-
"\n",
|
128 |
-
" with gr.Column():\n",
|
129 |
-
" with gr.Row():\n",
|
130 |
-
" gr.Markdown(\n",
|
131 |
-
" \"\"\"\n",
|
132 |
-
" ### Found Objects\n",
|
133 |
-
" \"\"\")\n",
|
134 |
-
" with gr.Row():\n",
|
135 |
-
" blur_slider = gr.Slider(minimum=0.5, maximum=10, value=3, label=\"Adject Blur Strength\")\n",
|
136 |
-
" with gr.Row():\n",
|
137 |
-
" object_output = gr.Dropdown(label=\"Select Object From Dropdown\")\n",
|
138 |
-
"\n",
|
139 |
-
" \n",
|
140 |
-
" with gr.Row():\n",
|
141 |
-
" with gr.Column():\n",
|
142 |
-
" gr.Markdown(\n",
|
143 |
-
" \"\"\"\n",
|
144 |
-
" ### Blurred Image Output\n",
|
145 |
-
" \"\"\")\n",
|
146 |
-
" image_output = gr.Image()\n",
|
147 |
-
" with gr.Column():\n",
|
148 |
-
" gr.Markdown(\n",
|
149 |
-
" \"\"\"\n",
|
150 |
-
" ### Example Images\n",
|
151 |
-
" \"\"\")\n",
|
152 |
-
" gr.Examples(\n",
|
153 |
-
" examples=[\n",
|
154 |
-
" \"test_images/dog_horse_cowboy.jpg\",\n",
|
155 |
-
" \"test_images/woman_and_dog.jpg\",\n",
|
156 |
-
" \"test_images/family_in_sofa.jpg\",\n",
|
157 |
-
" \"test_images/group_of_friends.jpg\",\n",
|
158 |
-
" \"test_images/people_group.jpg\"\n",
|
159 |
-
" ],\n",
|
160 |
-
" fn=image_objects,\n",
|
161 |
-
" inputs=image_input, \n",
|
162 |
-
" outputs=object_output)\n",
|
163 |
-
"\n",
|
164 |
-
" image_input.change(fn=image_objects, \n",
|
165 |
-
" inputs=image_input, \n",
|
166 |
-
" outputs=object_output\n",
|
167 |
-
" )\n",
|
168 |
-
"\n",
|
169 |
-
" object_output.change(fn=blurr_object, \n",
|
170 |
-
" inputs=[image_input, object_output, blur_slider],\n",
|
171 |
-
" outputs=image_output)\n",
|
172 |
-
"\n",
|
173 |
-
" blur_slider.change(fn=blurr_object, \n",
|
174 |
-
" inputs=[image_input, object_output, blur_slider],\n",
|
175 |
-
" outputs=image_output)\n",
|
176 |
-
" \n",
|
177 |
-
"\n",
|
178 |
-
"app.launch(debug=True)"
|
179 |
-
]
|
180 |
-
}
|
181 |
-
],
|
182 |
-
"metadata": {
|
183 |
-
"kernelspec": {
|
184 |
-
"display_name": "Python 3.9.7 ('base')",
|
185 |
-
"language": "python",
|
186 |
-
"name": "python3"
|
187 |
-
},
|
188 |
-
"language_info": {
|
189 |
-
"codemirror_mode": {
|
190 |
-
"name": "ipython",
|
191 |
-
"version": 3
|
192 |
-
},
|
193 |
-
"file_extension": ".py",
|
194 |
-
"mimetype": "text/x-python",
|
195 |
-
"name": "python",
|
196 |
-
"nbconvert_exporter": "python",
|
197 |
-
"pygments_lexer": "ipython3",
|
198 |
-
"version": "3.9.7"
|
199 |
-
},
|
200 |
-
"orig_nbformat": 4,
|
201 |
-
"vscode": {
|
202 |
-
"interpreter": {
|
203 |
-
"hash": "d03b47a5a9bfc09262d9700ac41deff9ead9ff3e21241983433bdf2900140cd2"
|
204 |
-
}
|
205 |
-
}
|
206 |
-
},
|
207 |
-
"nbformat": 4,
|
208 |
-
"nbformat_minor": 2
|
209 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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