galatasaray commited on
Commit
ed3a4b5
1 Parent(s): ff45a16

ipyn eklendi

Browse files
Files changed (1) hide show
  1. Gradio.ipynb +474 -0
Gradio.ipynb ADDED
@@ -0,0 +1,474 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 5,
6
+ "metadata": {
7
+ "colab": {
8
+ "base_uri": "https://localhost:8080/"
9
+ },
10
+ "id": "68CgGISA0OQu",
11
+ "outputId": "5529450e-9f82-479a-b398-9570a0969314"
12
+ },
13
+ "outputs": [
14
+ {
15
+ "output_type": "stream",
16
+ "name": "stdout",
17
+ "text": [
18
+ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
19
+ ]
20
+ }
21
+ ],
22
+ "source": [
23
+ "from google.colab import drive\n",
24
+ "drive.mount('/content/drive')"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": 6,
30
+ "metadata": {
31
+ "colab": {
32
+ "base_uri": "https://localhost:8080/"
33
+ },
34
+ "id": "NN8c3dXt0hfN",
35
+ "outputId": "83a5e277-e7d3-498a-e080-a573efdb2ce1"
36
+ },
37
+ "outputs": [
38
+ {
39
+ "output_type": "stream",
40
+ "name": "stdout",
41
+ "text": [
42
+ "datasets README.md\t\tsaved_model yolov5-flask-master\n",
43
+ "Gradio.ipynb requirements.txt\tyolov5\n",
44
+ "[Errno 2] No such file or directory: 'drive/My Drive/Colab Notebooks/Model Representation/'\n",
45
+ "/content/drive/My Drive/Colab Notebooks/Model Representation\n"
46
+ ]
47
+ }
48
+ ],
49
+ "source": [
50
+ "!ls \"/content/drive/My Drive/Colab Notebooks/Model Representation/\"\n",
51
+ "%cd drive/My Drive/Colab Notebooks/Model Representation/"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": null,
57
+ "metadata": {
58
+ "colab": {
59
+ "base_uri": "https://localhost:8080/",
60
+ "height": 35
61
+ },
62
+ "id": "vN36F7c50mF4",
63
+ "outputId": "cffb7408-84e1-41a2-b6dd-b4256c374449"
64
+ },
65
+ "outputs": [
66
+ {
67
+ "output_type": "execute_result",
68
+ "data": {
69
+ "text/plain": [
70
+ "'/content/drive/My Drive/Colab Notebooks/Model Representation'"
71
+ ],
72
+ "application/vnd.google.colaboratory.intrinsic+json": {
73
+ "type": "string"
74
+ }
75
+ },
76
+ "metadata": {},
77
+ "execution_count": 3
78
+ }
79
+ ],
80
+ "source": [
81
+ "import os\n",
82
+ "cwd = os.getcwd()\n",
83
+ "cwd"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": null,
89
+ "metadata": {
90
+ "id": "8xT1CQBM1Afx",
91
+ "colab": {
92
+ "base_uri": "https://localhost:8080/"
93
+ },
94
+ "outputId": "b8593c94-3223-480c-910a-0667a2e019f1"
95
+ },
96
+ "outputs": [
97
+ {
98
+ "output_type": "stream",
99
+ "name": "stdout",
100
+ "text": [
101
+ "\u001b[K |████████████████████████████████| 5.6 MB 2.0 MB/s \n",
102
+ "\u001b[K |████████████████████████████████| 596 kB 65.4 MB/s \n",
103
+ "\u001b[K |████████████████████████████████| 140 kB 18.1 MB/s \n",
104
+ "\u001b[K |████████████████████████████████| 84 kB 3.4 MB/s \n",
105
+ "\u001b[K |████████████████████████████████| 1.1 MB 66.8 MB/s \n",
106
+ "\u001b[K |████████████████████████████████| 54 kB 2.7 MB/s \n",
107
+ "\u001b[K |████████████████████████████████| 54 kB 572 kB/s \n",
108
+ "\u001b[K |████████████████████████████████| 57 kB 4.9 MB/s \n",
109
+ "\u001b[K |████████████████████████████████| 2.3 MB 46.4 MB/s \n",
110
+ "\u001b[K |████████████████████████████████| 84 kB 3.0 MB/s \n",
111
+ "\u001b[K |████████████████████████████████| 212 kB 51.5 MB/s \n",
112
+ "\u001b[K |████████████████████████████████| 272 kB 58.7 MB/s \n",
113
+ "\u001b[K |████████████████████████████████| 271 kB 47.1 MB/s \n",
114
+ "\u001b[K |████████████████████████████████| 144 kB 67.1 MB/s \n",
115
+ "\u001b[K |████████████████████████████████| 94 kB 3.7 MB/s \n",
116
+ "\u001b[K |████���███████████████████████████| 63 kB 1.5 MB/s \n",
117
+ "\u001b[K |████████████████████████████████| 80 kB 8.6 MB/s \n",
118
+ "\u001b[K |████████████████████████████████| 68 kB 2.6 MB/s \n",
119
+ "\u001b[K |████████████████████████████████| 43 kB 1.5 MB/s \n",
120
+ "\u001b[K |████████████████████████████████| 856 kB 49.4 MB/s \n",
121
+ "\u001b[K |████████████████████████████████| 62 kB 838 kB/s \n",
122
+ "\u001b[K |████████████████████████████████| 4.1 MB 40.7 MB/s \n",
123
+ "\u001b[?25h Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
124
+ " Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
125
+ ]
126
+ }
127
+ ],
128
+ "source": [
129
+ "!pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt gradio # install dependencies"
130
+ ]
131
+ },
132
+ {
133
+ "cell_type": "code",
134
+ "execution_count": null,
135
+ "metadata": {
136
+ "colab": {
137
+ "base_uri": "https://localhost:8080/",
138
+ "height": 1000
139
+ },
140
+ "id": "FwCb9Nmk1n6n",
141
+ "outputId": "219fa614-9dda-4870-fe54-dfce5b4e4b05"
142
+ },
143
+ "outputs": [
144
+ {
145
+ "name": "stderr",
146
+ "output_type": "stream",
147
+ "text": [
148
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m PyYAML>=5.3.1 not found and is required by YOLOv5, attempting auto-update...\n",
149
+ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
150
+ "Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.7/dist-packages (6.0)\n",
151
+ "\n",
152
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m torch!=1.12.0,>=1.7.0 not found and is required by YOLOv5, attempting auto-update...\n",
153
+ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
154
+ "Requirement already satisfied: torch!=1.12.0,>=1.7.0 in /usr/local/lib/python3.7/dist-packages (1.11.0)\n",
155
+ "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch!=1.12.0,>=1.7.0) (4.1.1)\n",
156
+ "\n",
157
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m torchvision!=0.13.0,>=0.8.1 not found and is required by YOLOv5, attempting auto-update...\n",
158
+ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
159
+ "Requirement already satisfied: torchvision!=0.13.0,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (0.12.0)\n",
160
+ "Requirement already satisfied: torch==1.11.0 in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (1.11.0)\n",
161
+ "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (7.1.2)\n",
162
+ "Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (2.23.0)\n",
163
+ "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (1.21.6)\n",
164
+ "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (4.1.1)\n",
165
+ "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (3.0.4)\n",
166
+ "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (1.24.3)\n",
167
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (2022.6.15)\n",
168
+ "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (2.10)\n",
169
+ "\n",
170
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m 3 packages updated per /root/.cache/torch/hub/ultralytics_yolov5_master/requirements.txt\n",
171
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m ⚠️ \u001b[1mRestart runtime or rerun command for updates to take effect\u001b[0m\n",
172
+ "\n",
173
+ "YOLOv5 🚀 2022-7-14 Python-3.7.13 torch-1.11.0+cu102 CPU\n",
174
+ "\n",
175
+ "Fusing layers... \n",
176
+ "YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients\n",
177
+ "Adding AutoShape... \n",
178
+ "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
179
+ " warnings.warn(value)\n"
180
+ ]
181
+ },
182
+ {
183
+ "name": "stdout",
184
+ "output_type": "stream",
185
+ "text": [
186
+ "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n"
187
+ ]
188
+ },
189
+ {
190
+ "name": "stderr",
191
+ "output_type": "stream",
192
+ "text": [
193
+ "Connected (version 2.0, client OpenSSH_7.6p1)\n",
194
+ "Authentication (publickey) successful!\n"
195
+ ]
196
+ },
197
+ {
198
+ "name": "stdout",
199
+ "output_type": "stream",
200
+ "text": [
201
+ "Running on public URL: https://21879.gradio.app\n",
202
+ "\n",
203
+ "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"
204
+ ]
205
+ },
206
+ {
207
+ "data": {
208
+ "text/html": [
209
+ "<div><iframe src=\"https://21879.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"
210
+ ],
211
+ "text/plain": [
212
+ "<IPython.core.display.HTML object>"
213
+ ]
214
+ },
215
+ "metadata": {},
216
+ "output_type": "display_data"
217
+ },
218
+ {
219
+ "name": "stdout",
220
+ "output_type": "stream",
221
+ "text": [
222
+ "Keyboard interruption in main thread... closing server.\n"
223
+ ]
224
+ },
225
+ {
226
+ "data": {
227
+ "text/plain": [
228
+ "(<gradio.routes.App at 0x7f39e2421b50>,\n",
229
+ " 'http://127.0.0.1:7860/',\n",
230
+ " 'https://21879.gradio.app')"
231
+ ]
232
+ },
233
+ "execution_count": 10,
234
+ "metadata": {},
235
+ "output_type": "execute_result"
236
+ }
237
+ ],
238
+ "source": [
239
+ "import gradio as gr\n",
240
+ "import torch\n",
241
+ "from PIL import Image\n",
242
+ "\n",
243
+ "# Images\n",
244
+ "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')\n",
245
+ "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/bus.jpg', 'bus.jpg')\n",
246
+ "\n",
247
+ "# Model\n",
248
+ "#model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update\n",
249
+ "\n",
250
+ "model = torch.hub.load(cwd+'/yolov5', 'custom', path=cwd+'/saved_model/yolov5s.pt', source='local') # local model\n",
251
+ "\n",
252
+ "\n",
253
+ "\n",
254
+ "def yolo(im, size=640):\n",
255
+ " g = (size / max(im.size)) # gain\n",
256
+ " im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize\n",
257
+ "\n",
258
+ " results = model(im) # inference\n",
259
+ " results.render() # updates results.imgs with boxes and labels\n",
260
+ " return Image.fromarray(results.imgs[0])\n",
261
+ "\n",
262
+ "\n",
263
+ "inputs = gr.inputs.Image(type='pil', label=\"Original Image\")\n",
264
+ "outputs = gr.outputs.Image(type=\"pil\", label=\"Output Image\")\n",
265
+ "\n",
266
+ "title = \"YOLOv5\"\n",
267
+ "description = \"YOLOv5 Gradio demo for object detection. Upload an image or click an example image to use.\"\n",
268
+ "article = \"<p style='text-align: center'>YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset, and includes \" \\\n",
269
+ " \"simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, \" \\\n",
270
+ " \"and export to ONNX, CoreML and TFLite. <a href='https://github.com/ultralytics/yolov5'>Source code</a> |\" \\\n",
271
+ " \"<a href='https://apps.apple.com/app/id1452689527'>iOS App</a> | <a href='https://pytorch.org/hub/ultralytics_yolov5'>PyTorch Hub</a></p>\"\n",
272
+ "\n",
273
+ "examples = [[cwd+'/datasets/zidane.jpg'], [cwd+'/datasets/bus.jpg']]\n",
274
+ "gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(\n",
275
+ " debug=True)"
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": 7,
281
+ "metadata": {
282
+ "colab": {
283
+ "base_uri": "https://localhost:8080/",
284
+ "height": 1000
285
+ },
286
+ "id": "8kaOPNyz2cEr",
287
+ "outputId": "4012dc70-c529-47cb-e8c0-ef053f268629"
288
+ },
289
+ "outputs": [
290
+ {
291
+ "output_type": "stream",
292
+ "name": "stderr",
293
+ "text": [
294
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m PyYAML>=5.3.1 not found and is required by YOLOv5, attempting auto-update...\n",
295
+ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
296
+ "Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.7/dist-packages (6.0)\n",
297
+ "\n",
298
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m 1 package updated per /content/drive/MyDrive/Colab Notebooks/Model Representation/yolov5/requirements.txt\n",
299
+ "\u001b[31m\u001b[1mrequirements:\u001b[0m ⚠️ \u001b[1mRestart runtime or rerun command for updates to take effect\u001b[0m\n",
300
+ "\n",
301
+ "YOLOv5 🚀 2022-6-24 Python-3.7.13 torch-1.12.0+cu113 CPU\n",
302
+ "\n",
303
+ "Fusing layers... \n",
304
+ "Model summary: 213 layers, 7012822 parameters, 0 gradients\n",
305
+ "Adding AutoShape... \n",
306
+ "/usr/local/lib/python3.7/dist-packages/gradio/inputs.py:257: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
307
+ " \"Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\",\n",
308
+ "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
309
+ " warnings.warn(value)\n",
310
+ "/usr/local/lib/python3.7/dist-packages/gradio/outputs.py:43: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n",
311
+ " \"Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\",\n"
312
+ ]
313
+ },
314
+ {
315
+ "output_type": "stream",
316
+ "name": "stdout",
317
+ "text": [
318
+ "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n"
319
+ ]
320
+ },
321
+ {
322
+ "output_type": "stream",
323
+ "name": "stderr",
324
+ "text": [
325
+ "Connected (version 2.0, client OpenSSH_7.6p1)\n",
326
+ "Authentication (publickey) successful!\n"
327
+ ]
328
+ },
329
+ {
330
+ "output_type": "stream",
331
+ "name": "stdout",
332
+ "text": [
333
+ "Running on public URL: https://24062.gradio.app\n",
334
+ "\n",
335
+ "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"
336
+ ]
337
+ },
338
+ {
339
+ "output_type": "display_data",
340
+ "data": {
341
+ "text/plain": [
342
+ "<IPython.core.display.HTML object>"
343
+ ],
344
+ "text/html": [
345
+ "<div><iframe src=\"https://24062.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"
346
+ ]
347
+ },
348
+ "metadata": {}
349
+ },
350
+ {
351
+ "output_type": "stream",
352
+ "name": "stderr",
353
+ "text": [
354
+ "Traceback (most recent call last):\n",
355
+ " File \"/usr/local/lib/python3.7/dist-packages/gradio/routes.py\", line 256, in run_predict\n",
356
+ " fn_index, raw_input, username, session_state\n",
357
+ " File \"/usr/local/lib/python3.7/dist-packages/gradio/blocks.py\", line 599, in process_api\n",
358
+ " predictions, duration = await self.call_function(fn_index, processed_input)\n",
359
+ " File \"/usr/local/lib/python3.7/dist-packages/gradio/blocks.py\", line 515, in call_function\n",
360
+ " block_fn.fn, *processed_input, limiter=self.limiter\n",
361
+ " File \"/usr/local/lib/python3.7/dist-packages/anyio/to_thread.py\", line 32, in run_sync\n",
362
+ " func, *args, cancellable=cancellable, limiter=limiter\n",
363
+ " File \"/usr/local/lib/python3.7/dist-packages/anyio/_backends/_asyncio.py\", line 937, in run_sync_in_worker_thread\n",
364
+ " return await future\n",
365
+ " File \"/usr/local/lib/python3.7/dist-packages/anyio/_backends/_asyncio.py\", line 867, in run\n",
366
+ " result = context.run(func, *args)\n",
367
+ " File \"/usr/local/lib/python3.7/dist-packages/gradio/interface.py\", line 487, in <lambda>\n",
368
+ " if len(self.output_components) == 1\n",
369
+ " File \"/usr/local/lib/python3.7/dist-packages/gradio/interface.py\", line 666, in run_prediction\n",
370
+ " prediction = self.fn(*processed_input)\n",
371
+ " File \"<ipython-input-7-71d283d5d6e3>\", line 23, in yolo\n",
372
+ " g = (size / max(im.size)) # gain\n",
373
+ "AttributeError: 'NoneType' object has no attribute 'size'\n"
374
+ ]
375
+ },
376
+ {
377
+ "output_type": "stream",
378
+ "name": "stdout",
379
+ "text": [
380
+ "Keyboard interruption in main thread... closing server.\n"
381
+ ]
382
+ },
383
+ {
384
+ "output_type": "execute_result",
385
+ "data": {
386
+ "text/plain": [
387
+ "(<gradio.routes.App at 0x7f623ac59ad0>,\n",
388
+ " 'http://127.0.0.1:7860/',\n",
389
+ " 'https://24062.gradio.app')"
390
+ ]
391
+ },
392
+ "metadata": {},
393
+ "execution_count": 7
394
+ }
395
+ ],
396
+ "source": [
397
+ "import gradio as gr\n",
398
+ "import torch\n",
399
+ "from PIL import Image\n",
400
+ "\n",
401
+ "\n",
402
+ "import os\n",
403
+ "cwd = os.getcwd()\n",
404
+ "\n",
405
+ "# !pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt gradio\n",
406
+ "\n",
407
+ "\n",
408
+ "\n",
409
+ "# Images\n",
410
+ "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')\n",
411
+ "#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/bus.jpg', 'bus.jpg')\n",
412
+ "\n",
413
+ "# Model\n",
414
+ "#model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update\n",
415
+ "model = torch.hub.load(cwd+'/yolov5', 'custom', path=cwd+'/saved_model/s1000_best.pt', source='local') # local model\n",
416
+ "\n",
417
+ "\n",
418
+ "def yolo(im, size=640):\n",
419
+ " g = (size / max(im.size)) # gain\n",
420
+ " im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize\n",
421
+ "\n",
422
+ " results = model(im) # inference\n",
423
+ " results.render() # updates results.imgs with boxes and labels\n",
424
+ " return Image.fromarray(results.imgs[0])\n",
425
+ "\n",
426
+ "\n",
427
+ "inputs = gr.inputs.Image(type='pil', label=\"Original Image\")\n",
428
+ "outputs = gr.outputs.Image(type=\"pil\", label=\"Output Image\")\n",
429
+ "\n",
430
+ "title = \"S1000 Detection\"\n",
431
+ "description = \"YOLOv5 Gradio demo for object detection. Upload an image or click an example image to use.\"\n",
432
+ "article = \"<p style='text-align: center'>YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset, and includes \" \\\n",
433
+ " \"simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, \" \\\n",
434
+ " \"and export to ONNX, CoreML and TFLite. <a href='https://github.com/ultralytics/yolov5'>Source code</a> |\" \\\n",
435
+ " \"<a href='https://apps.apple.com/app/id1452689527'>iOS App</a> | <a href='https://pytorch.org/hub/ultralytics_yolov5'>PyTorch Hub</a></p>\"\n",
436
+ "\n",
437
+ "path_folder = cwd+'/datasets/s1000/'\n",
438
+ "examples = [[path_folder+'s1000_50_metre.jpg'], [path_folder+'s1000_100_metre.jpg'],[path_folder+'s1000_150_metre.jpg'],[path_folder+'s1000_200_metre.jpg'],[path_folder+'s1000_250_metre.jpg']]\n",
439
+ "gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(\n",
440
+ " debug=True)\n",
441
+ "\n",
442
+ "\n",
443
+ "'''\n",
444
+ "\n",
445
+ "\n",
446
+ "git init\n",
447
+ "git config user.name bulentsofttech\n",
448
+ "git config user.email bulent.softtech@gmail.com\n",
449
+ "git add *\n",
450
+ "git commit -m \"WriteCommit\"\n",
451
+ "git push origin master\n",
452
+ "\n",
453
+ "\n",
454
+ "'''"
455
+ ]
456
+ }
457
+ ],
458
+ "metadata": {
459
+ "colab": {
460
+ "collapsed_sections": [],
461
+ "name": "Gradio.ipynb",
462
+ "provenance": []
463
+ },
464
+ "kernelspec": {
465
+ "display_name": "Python 3",
466
+ "name": "python3"
467
+ },
468
+ "language_info": {
469
+ "name": "python"
470
+ }
471
+ },
472
+ "nbformat": 4,
473
+ "nbformat_minor": 0
474
+ }