rubi242 commited on
Commit
023fc7f
·
1 Parent(s): 4d519f3

use webcam for input

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. gradio-app.ipynb +207 -11
app.py CHANGED
@@ -11,7 +11,7 @@ def classify_image(img):
11
 
12
  demo = gr.Interface(
13
  fn=classify_image,
14
- inputs=[gr.Image(shape=(192,192))],
15
  outputs=[gr.Label()],
16
  examples=['male.jpg', 'female.jpg']
17
  )
 
11
 
12
  demo = gr.Interface(
13
  fn=classify_image,
14
+ inputs=[gr.Image(shape=(192,192), source='webcam')],
15
  outputs=[gr.Label()],
16
  examples=['male.jpg', 'female.jpg']
17
  )
gradio-app.ipynb CHANGED
@@ -3,7 +3,7 @@
3
  {
4
  "cell_type": "code",
5
  "execution_count": 1,
6
- "id": "1948dee4",
7
  "metadata": {},
8
  "outputs": [],
9
  "source": [
@@ -13,7 +13,7 @@
13
  {
14
  "cell_type": "code",
15
  "execution_count": 2,
16
- "id": "74e47a6e",
17
  "metadata": {},
18
  "outputs": [
19
  {
@@ -36,7 +36,7 @@
36
  {
37
  "cell_type": "code",
38
  "execution_count": 3,
39
- "id": "d92f25a0",
40
  "metadata": {},
41
  "outputs": [],
42
  "source": [
@@ -51,7 +51,7 @@
51
  {
52
  "cell_type": "code",
53
  "execution_count": 4,
54
- "id": "25be07fd",
55
  "metadata": {},
56
  "outputs": [
57
  {
@@ -75,7 +75,7 @@
75
  {
76
  "cell_type": "code",
77
  "execution_count": 5,
78
- "id": "e4a6bcb7",
79
  "metadata": {},
80
  "outputs": [
81
  {
@@ -132,15 +132,15 @@
132
  },
133
  {
134
  "cell_type": "code",
135
- "execution_count": 6,
136
- "id": "7548045a",
137
  "metadata": {},
138
  "outputs": [
139
  {
140
  "name": "stdout",
141
  "output_type": "stream",
142
  "text": [
143
- "Running on local URL: http://127.0.0.1:7860\n",
144
  "\n",
145
  "To create a public link, set `share=True` in `launch()`.\n"
146
  ]
@@ -148,24 +148,220 @@
148
  {
149
  "data": {
150
  "text/plain": [
151
- "(<gradio.routes.App at 0x7c55b40a16c0>, 'http://127.0.0.1:7860/', None)"
152
  ]
153
  },
154
- "execution_count": 6,
155
  "metadata": {},
156
  "output_type": "execute_result"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  }
158
  ],
159
  "source": [
160
  "#|export\n",
161
  "demo = gr.Interface(\n",
162
  " fn=classify_image,\n",
163
- " inputs=[gr.Image(shape=(192,192))],\n",
164
  " outputs=[gr.Label()],\n",
165
  " examples=['male.jpg', 'female.jpg']\n",
166
  ")\n",
167
  "demo.launch(inline=False)"
168
  ]
 
 
 
 
 
 
 
 
169
  }
170
  ],
171
  "metadata": {
 
3
  {
4
  "cell_type": "code",
5
  "execution_count": 1,
6
+ "id": "83670509",
7
  "metadata": {},
8
  "outputs": [],
9
  "source": [
 
13
  {
14
  "cell_type": "code",
15
  "execution_count": 2,
16
+ "id": "66360cf1",
17
  "metadata": {},
18
  "outputs": [
19
  {
 
36
  {
37
  "cell_type": "code",
38
  "execution_count": 3,
39
+ "id": "144144fe",
40
  "metadata": {},
41
  "outputs": [],
42
  "source": [
 
51
  {
52
  "cell_type": "code",
53
  "execution_count": 4,
54
+ "id": "e5ff35b9",
55
  "metadata": {},
56
  "outputs": [
57
  {
 
75
  {
76
  "cell_type": "code",
77
  "execution_count": 5,
78
+ "id": "0d828251",
79
  "metadata": {},
80
  "outputs": [
81
  {
 
132
  },
133
  {
134
  "cell_type": "code",
135
+ "execution_count": 8,
136
+ "id": "f49cd34d",
137
  "metadata": {},
138
  "outputs": [
139
  {
140
  "name": "stdout",
141
  "output_type": "stream",
142
  "text": [
143
+ "Running on local URL: http://127.0.0.1:7861\n",
144
  "\n",
145
  "To create a public link, set `share=True` in `launch()`.\n"
146
  ]
 
148
  {
149
  "data": {
150
  "text/plain": [
151
+ "(<gradio.routes.App at 0x7c561b921630>, 'http://127.0.0.1:7861/', None)"
152
  ]
153
  },
154
+ "execution_count": 8,
155
  "metadata": {},
156
  "output_type": "execute_result"
157
+ },
158
+ {
159
+ "name": "stderr",
160
+ "output_type": "stream",
161
+ "text": [
162
+ "Traceback (most recent call last):\n",
163
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/gradio/routes.py\", line 283, in run_predict\n",
164
+ " output = await app.blocks.process_api(\n",
165
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/gradio/blocks.py\", line 892, in process_api\n",
166
+ " result = await self.call_function(fn_index, inputs, iterator)\n",
167
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/gradio/blocks.py\", line 733, in call_function\n",
168
+ " prediction = await anyio.to_thread.run_sync(\n",
169
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/anyio/to_thread.py\", line 31, in run_sync\n",
170
+ " return await get_asynclib().run_sync_in_worker_thread(\n",
171
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/anyio/_backends/_asyncio.py\", line 937, in run_sync_in_worker_thread\n",
172
+ " return await future\n",
173
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/anyio/_backends/_asyncio.py\", line 867, in run\n",
174
+ " result = context.run(func, *args)\n",
175
+ " File \"/tmp/ipykernel_24169/2631874386.py\", line 5, in classify_image\n",
176
+ " pred, pred_idx, probs = learn.predict(img)\n",
177
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/fastai/learner.py\", line 302, in predict\n",
178
+ " dl = self.dls.test_dl([item], rm_type_tfms=rm_type_tfms, num_workers=0)\n",
179
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/fastai/data/core.py\", line 532, in test_dl\n",
180
+ " test_ds = test_set(self.valid_ds, test_items, rm_tfms=rm_type_tfms, with_labels=with_labels\n",
181
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/fastai/data/core.py\", line 511, in test_set\n",
182
+ " if rm_tfms is None: rm_tfms = [tl.infer_idx(get_first(test_items)) for tl in test_tls]\n",
183
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/fastai/data/core.py\", line 511, in <listcomp>\n",
184
+ " if rm_tfms is None: rm_tfms = [tl.infer_idx(get_first(test_items)) for tl in test_tls]\n",
185
+ " File \"/home/antonrubisov/mambaforge/lib/python3.10/site-packages/fastai/data/core.py\", line 405, in infer_idx\n",
186
+ " assert idx < len(self.types), f\"Expected an input of type in \\n{pretty_types}\\n but got {type(x)}\"\n",
187
+ "AssertionError: Expected an input of type in \n",
188
+ " - <class 'pathlib.PosixPath'>\n",
189
+ " - <class 'pathlib.Path'>\n",
190
+ " - <class 'str'>\n",
191
+ " - <class 'torch.Tensor'>\n",
192
+ " - <class 'numpy.ndarray'>\n",
193
+ " - <class 'bytes'>\n",
194
+ " - <class 'fastai.vision.core.PILImage'>\n",
195
+ " but got <class 'NoneType'>\n"
196
+ ]
197
+ },
198
+ {
199
+ "data": {
200
+ "text/html": [
201
+ "\n",
202
+ "<style>\n",
203
+ " /* Turns off some styling */\n",
204
+ " progress {\n",
205
+ " /* gets rid of default border in Firefox and Opera. */\n",
206
+ " border: none;\n",
207
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
208
+ " background-size: auto;\n",
209
+ " }\n",
210
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
211
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
212
+ " }\n",
213
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
214
+ " background: #F44336;\n",
215
+ " }\n",
216
+ "</style>\n"
217
+ ],
218
+ "text/plain": [
219
+ "<IPython.core.display.HTML object>"
220
+ ]
221
+ },
222
+ "metadata": {},
223
+ "output_type": "display_data"
224
+ },
225
+ {
226
+ "data": {
227
+ "text/html": [],
228
+ "text/plain": [
229
+ "<IPython.core.display.HTML object>"
230
+ ]
231
+ },
232
+ "metadata": {},
233
+ "output_type": "display_data"
234
+ },
235
+ {
236
+ "data": {
237
+ "text/html": [
238
+ "\n",
239
+ "<style>\n",
240
+ " /* Turns off some styling */\n",
241
+ " progress {\n",
242
+ " /* gets rid of default border in Firefox and Opera. */\n",
243
+ " border: none;\n",
244
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
245
+ " background-size: auto;\n",
246
+ " }\n",
247
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
248
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
249
+ " }\n",
250
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
251
+ " background: #F44336;\n",
252
+ " }\n",
253
+ "</style>\n"
254
+ ],
255
+ "text/plain": [
256
+ "<IPython.core.display.HTML object>"
257
+ ]
258
+ },
259
+ "metadata": {},
260
+ "output_type": "display_data"
261
+ },
262
+ {
263
+ "data": {
264
+ "text/html": [],
265
+ "text/plain": [
266
+ "<IPython.core.display.HTML object>"
267
+ ]
268
+ },
269
+ "metadata": {},
270
+ "output_type": "display_data"
271
+ },
272
+ {
273
+ "data": {
274
+ "text/html": [
275
+ "\n",
276
+ "<style>\n",
277
+ " /* Turns off some styling */\n",
278
+ " progress {\n",
279
+ " /* gets rid of default border in Firefox and Opera. */\n",
280
+ " border: none;\n",
281
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
282
+ " background-size: auto;\n",
283
+ " }\n",
284
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
285
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
286
+ " }\n",
287
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
288
+ " background: #F44336;\n",
289
+ " }\n",
290
+ "</style>\n"
291
+ ],
292
+ "text/plain": [
293
+ "<IPython.core.display.HTML object>"
294
+ ]
295
+ },
296
+ "metadata": {},
297
+ "output_type": "display_data"
298
+ },
299
+ {
300
+ "data": {
301
+ "text/html": [],
302
+ "text/plain": [
303
+ "<IPython.core.display.HTML object>"
304
+ ]
305
+ },
306
+ "metadata": {},
307
+ "output_type": "display_data"
308
+ },
309
+ {
310
+ "data": {
311
+ "text/html": [
312
+ "\n",
313
+ "<style>\n",
314
+ " /* Turns off some styling */\n",
315
+ " progress {\n",
316
+ " /* gets rid of default border in Firefox and Opera. */\n",
317
+ " border: none;\n",
318
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
319
+ " background-size: auto;\n",
320
+ " }\n",
321
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
322
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
323
+ " }\n",
324
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
325
+ " background: #F44336;\n",
326
+ " }\n",
327
+ "</style>\n"
328
+ ],
329
+ "text/plain": [
330
+ "<IPython.core.display.HTML object>"
331
+ ]
332
+ },
333
+ "metadata": {},
334
+ "output_type": "display_data"
335
+ },
336
+ {
337
+ "data": {
338
+ "text/html": [],
339
+ "text/plain": [
340
+ "<IPython.core.display.HTML object>"
341
+ ]
342
+ },
343
+ "metadata": {},
344
+ "output_type": "display_data"
345
  }
346
  ],
347
  "source": [
348
  "#|export\n",
349
  "demo = gr.Interface(\n",
350
  " fn=classify_image,\n",
351
+ " inputs=[gr.Image(shape=(192,192),source='webcam')],\n",
352
  " outputs=[gr.Label()],\n",
353
  " examples=['male.jpg', 'female.jpg']\n",
354
  ")\n",
355
  "demo.launch(inline=False)"
356
  ]
357
+ },
358
+ {
359
+ "cell_type": "code",
360
+ "execution_count": null,
361
+ "id": "8ba84dab",
362
+ "metadata": {},
363
+ "outputs": [],
364
+ "source": []
365
  }
366
  ],
367
  "metadata": {