File size: 11,961 Bytes
61cdcf9
 
 
 
 
 
 
 
 
 
 
 
 
 
2add774
61cdcf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2add774
61cdcf9
 
 
 
 
 
 
2add774
61cdcf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2add774
 
61cdcf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2add774
61cdcf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2add774
61cdcf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2add774
61cdcf9
 
 
 
 
2add774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61cdcf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": 3.0293430767166755e+38,
   "metadata": {
    "id": 3.0293430767166755e+38
   },
   "source": [
    "# Gradio Demo: blocks_flipper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": 2.8891853944186117e+38,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 616
    },
    "id": 2.8891853944186117e+38,
    "outputId": "b60a6d5e-045d-4b40-bfd8-6caa407a34df",
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7868\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import gradio as gr\n",
    "import os\n",
    "from PIL import Image\n",
    "from functools import partial\n",
    "\n",
    "def retrieve_input_image(dataset, inputs):\n",
    "    img_id = inputs\n",
    "    img_path = os.path.join('online_demo', dataset, 'step-100_scale-6.0', img_id, 'input.png')\n",
    "    image = Image.open(img_path)\n",
    "    return image\n",
    "\n",
    "def retrieve_novel_view(dataset, img_id, polar, azimuth, zoom, seed):\n",
    "    polar = polar // 30 + 1\n",
    "    azimuth = azimuth // 30\n",
    "    zoom = int(zoom * 2 + 1)\n",
    "    img_path = os.path.join('online_demo', dataset, 'step-100_scale-6.0', img_id,\\\n",
    "                            'polar-%d_azimuth-%d_distance-%d_seed-%d.png' % (polar, azimuth, zoom, seed))\n",
    "    image = Image.open(img_path)\n",
    "    return image\n",
    "    \n",
    "\n",
    "with gr.Blocks() as demo:\n",
    "    gr.Markdown(\"Flip text or image files using this demo.\")\n",
    "    with gr.Tab(\"In-the-wild Images\"):\n",
    "        with gr.Row():\n",
    "            with gr.Column(scale=1):\n",
    "                default_input_image = Image.open( os.path.join('online_demo', 'nerf_wild', 'step-100_scale-6.0', 'car1', 'input.png'))\n",
    "                input_image = gr.Image(default_input_image, shape=[256, 256])\n",
    "                options = sorted(os.listdir('online_demo/nerf_wild/step-100_scale-6.0'))\n",
    "                img_id = gr.Dropdown(options, value='car1', label='options')\n",
    "                text_button = gr.Button(\"Load Input Image\")\n",
    "                retrieve_input_image_dataset = partial(retrieve_input_image, 'nerf_wild')\n",
    "                text_button.click(retrieve_input_image_dataset, inputs=img_id, outputs=input_image)\n",
    "\n",
    "            with gr.Column(scale=1):\n",
    "                novel_view = gr.Image(shape=[256, 256])\n",
    "                inputs = [img_id,\n",
    "                gr.Slider(-30, 30, value=0, step=30, label='Polar angle (vertical rotation in degrees)'),\n",
    "                gr.Slider(0, 330, value=0, step=30, label='Azimuth angle (horizontal rotation in degrees)'),\n",
    "                gr.Slider(-0.5, 0.5, value=0, step=0.5, label='Zoom'),\n",
    "                gr.Slider(1, 4, value=1, step=1, label='Random seed')]\n",
    "                \n",
    "                submit_button = gr.Button(\"Get Novel View\")\n",
    "                retrieve_novel_view_dataset = partial(retrieve_novel_view, 'nerf_wild')\n",
    "                submit_button.click(retrieve_novel_view_dataset, inputs=inputs, outputs=novel_view)\n",
    "        \n",
    "    with gr.Tab(\"Google Scanned Objects\"):\n",
    "        with gr.Row():\n",
    "            with gr.Column(scale=1):\n",
    "                default_input_image = Image.open( os.path.join('online_demo', 'GSO', 'step-100_scale-6.0', 'SAMBA_HEMP', 'input.png'))\n",
    "                input_image = gr.Image(default_input_image, shape=[256, 256])\n",
    "                options = sorted(os.listdir('online_demo/GSO/step-100_scale-6.0'))\n",
    "                img_id = gr.Dropdown(options, value='SAMBA_HEMP', label='options')\n",
    "                text_button = gr.Button(\"Choose Input Image\")\n",
    "                retrieve_input_image_dataset = partial(retrieve_input_image, 'GSO')\n",
    "                text_button.click(retrieve_input_image_dataset, inputs=img_id, outputs=input_image)\n",
    "\n",
    "            with gr.Column(scale=1):\n",
    "                novel_view = gr.Image(shape=[256, 256])\n",
    "                inputs = [img_id,\n",
    "                gr.Slider(-30, 30, value=0, step=30, label='Polar angle (vertical rotation in degrees)'),\n",
    "                gr.Slider(0, 330, value=0, step=30, label='Azimuth angle (horizontal rotation in degrees)'),\n",
    "                gr.Slider(-0.5, 0.5, value=0, step=0.5, label='Zoom'),\n",
    "                gr.Slider(1, 4, value=1, step=1, label='Random seed')]\n",
    "                \n",
    "                submit_button = gr.Button(\"Get Novel View\")\n",
    "                retrieve_novel_view_dataset = partial(retrieve_novel_view, 'GSO')\n",
    "                submit_button.click(retrieve_novel_view_dataset, inputs=inputs, outputs=novel_view)\n",
    "        \n",
    "    with gr.Tab(\"RTMV\"):\n",
    "        with gr.Row():\n",
    "            with gr.Column(scale=1):\n",
    "                default_input_image = Image.open( os.path.join('online_demo', 'RTMV', 'step-100_scale-6.0', '00000', 'input.png'))\n",
    "                input_image = gr.Image(default_input_image, shape=[256, 256])\n",
    "                options = sorted(os.listdir('online_demo/RTMV/step-100_scale-6.0'))\n",
    "                img_id = gr.Dropdown(options, value='00000', label='options')\n",
    "                text_button = gr.Button(\"Choose Input Image\")\n",
    "                retrieve_input_image_dataset = partial(retrieve_input_image, 'RTMV')\n",
    "                text_button.click(retrieve_input_image_dataset, inputs=img_id, outputs=input_image)\n",
    "\n",
    "            with gr.Column(scale=1):\n",
    "                novel_view = gr.Image(shape=[256, 256])\n",
    "                inputs = [img_id,\n",
    "                gr.Slider(-30, 30, value=0, step=30, label='Polar angle (vertical rotation in degrees)'),\n",
    "                gr.Slider(0, 330, value=0, step=30, label='Azimuth angle (horizontal rotation in degrees)'),\n",
    "                gr.Slider(-0.5, 0.5, value=0, step=0.5, label='Zoom'),\n",
    "                gr.Slider(1, 4, value=1, step=1, label='Random seed')]\n",
    "                \n",
    "                submit_button = gr.Button(\"Get Novel View\")\n",
    "                retrieve_novel_view_dataset = partial(retrieve_novel_view, 'RTMV')\n",
    "                submit_button.click(retrieve_novel_view_dataset, inputs=inputs, outputs=novel_view)\n",
    "    \n",
    "    \n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    demo.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bk8_q39r_iGt",
   "metadata": {
    "id": "bk8_q39r_iGt"
   },
   "outputs": [],
   "source": [
    "img_id = gr.Dropdown(options, value='car1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e91860b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/rliu/anaconda3/lib/python3.9/site-packages/gradio/utils.py:805: UserWarning: Expected 2 arguments for function <function retrieve_input_image at 0x7ffabc3310d0>, received 0.\n",
      "  warnings.warn(\n",
      "/home/rliu/anaconda3/lib/python3.9/site-packages/gradio/utils.py:809: UserWarning: Expected at least 2 arguments for function <function retrieve_input_image at 0x7ffabc3310d0>, received 0.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "change() and other events can only be called within a Blocks context.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_2769604/1401407332.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mimg_id\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mretrieve_input_image\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/anaconda3/lib/python3.9/site-packages/gradio/events.py\u001b[0m in \u001b[0;36mchange\u001b[0;34m(self, fn, inputs, outputs, api_name, status_tracker, scroll_to_output, show_progress, queue, batch, max_batch_size, preprocess, postprocess, cancels, every, _js)\u001b[0m\n\u001b[1;32m     79\u001b[0m                 \u001b[0;34m\"The 'status_tracker' parameter has been deprecated and has no effect.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     80\u001b[0m             )\n\u001b[0;32m---> 81\u001b[0;31m         dep = self.set_event_trigger(\n\u001b[0m\u001b[1;32m     82\u001b[0m             \u001b[0;34m\"change\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     83\u001b[0m             \u001b[0mfn\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.9/site-packages/gradio/blocks.py\u001b[0m in \u001b[0;36mset_event_trigger\u001b[0;34m(self, event_name, fn, inputs, outputs, preprocess, postprocess, scroll_to_output, show_progress, api_name, js, no_target, queue, batch, max_batch_size, cancels, every)\u001b[0m\n\u001b[1;32m    192\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    193\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mContext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mroot_block\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 194\u001b[0;31m             raise AttributeError(\n\u001b[0m\u001b[1;32m    195\u001b[0m                 \u001b[0;34mf\"{event_name}() and other events can only be called within a Blocks context.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    196\u001b[0m             )\n",
      "\u001b[0;31mAttributeError\u001b[0m: change() and other events can only be called within a Blocks context."
     ]
    }
   ],
   "source": [
    "img_id.change(retrieve_input_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ab10a43f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'car1'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img_id.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2febec07",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "gpuClass": "standard",
  "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.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}