3

I was able to run the Flask app with yolov5 on a PC with an internet connection. I followed the steps mentioned in yolov5 docs and used this file: yolov5/utils/flask_rest_api/restapi.py,

But I need to achieve the same offline(On a particular PC). Now the issue is, when I am using the following:

model = torch.hub.load("ultralytics/yolov5", "yolov5", force_reload=True)

It tries to download model from internet. And throws an error.

Urllib.error.URLError: <urlopen error [Errno - 2] name or service not known>

How to get the same results offline.

Thanks in advance.

rs_punia
  • 421
  • 2
  • 6
  • 17
  • 1
    Are you able to connect and retrieve this data manually without the app. If yes, then there is an issue with the code. If no, then the issue is with getting the data via a different method. – D.L Feb 24 '22 at 14:49

2 Answers2

7

If you want to run detection offline, you need to have the model already downloaded.

So, download the model (for example yolov5s.pt) from https://github.com/ultralytics/yolov5/releases and store it for example to the yolov5/models.

After that, replace

# model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=True)  # force_reload to recache

with

model = torch.hub.load(r'C:\Users\Milan\Projects\yolov5', 'custom', path=r'C:\Users\Milan\Projects\yolov5\models\yolov5s.pt', source='local')

With this line, you can run detection also offline.

Note: When you start the app for the first time with the updated torch.hub.load, it will download the model if not present (so you do not need to download it from https://github.com/ultralytics/yolov5/releases).

enter image description here

Milan Hlinák
  • 4,260
  • 1
  • 30
  • 41
  • This helped a lot. For me, it worked smoothly in Windows, but on Linux, I faced a problem as no Arial.ttf file was available on Linux at the location `/home//.config/Ultralytics`, but the same was available in Windows at the location `C:\Windows\Fonts`. Yolov5 tries to download the Arial.ttf file from the URL `https://ultralytics.com/assets/Arial.ttf`. Failing to do so, on an offline PC generates an error. – rs_punia Feb 28 '22 at 18:41
  • Is there some way to download the model also before, so that we don't use the internet even once? – Vikram Mar 09 '23 at 14:20
0

There is one more issue involved here. When this code is run on a machine that has no internet connection at all. Then you may face the following error.

Downloading https://ultralytics.com/assets/Arial.ttf to /home/<local_user>/.config/Ultralytics/Arial.ttf...
Traceback (most recent call last):
  File "/home/<local_user>/Py_Prac_WSL/yolov5-flask-master/yolov5/utils/plots.py", line 56, in check_pil_font
    return ImageFont.truetype(str(font) if font.exists() else font.name, size)
  File "/home/<local_user>/.local/share/virtualenvs/23_Jun-82xb8nrB/lib/python3.8/site-packages/PIL/ImageFont.py", line 836, in truetype
    return freetype(font)
  File "/home/<local_user>/.local/share/virtualenvs/23_Jun-82xb8nrB/lib/python3.8/site-packages/PIL/ImageFont.py", line 833, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/<local_user>/.local/share/virtualenvs/23_Jun-82xb8nrB/lib/python3.8/site-packages/PIL/ImageFont.py", line 193, in __init__
    self.font = core.getfont(
OSError: cannot open resource

To overcome this error, you need to download manually, the Arial.ttf file from https://ultralytics.com/assets/Arial.ttf and paste it to the following location, on Linux:

/home/<your_pc_user>/.config/Ultralytics

On windows, paste Arial.ttf here:

C:\Windows\Fonts

The first line of the error message mentions the same thing. After this, the code runs smoothly in offline mode.

Further as mentioned at https://docs.ultralytics.com/tutorials/pytorch-hub/, any custom-trained-model other than the one uploaded at PyTorch-model-hub can be accessed by this code.

path_hubconfig = 'absolute/path/to/yolov5'
path_trained_model = 'absolute/path/to/best.pt'
model = torch.hub.load(path_hubconfig, 'custom', path=path_trained_model, source='local')  # local repo

With this code, object detection is carried out by the locally saved custom-trained model. Once, the custom trained model is saved locally this piece of code access it directly avoiding any necessity of the internet.

rs_punia
  • 421
  • 2
  • 6
  • 17