Sunday, April 07, 2024

ModuleNotFoundError: No module named 'torch._custom_ops'

Did you ever feel like you were the only one on earth who has ever seen this error?  Well, just because your favorite search engines are not giving you results, does not mean you are alone.

Traceback (most recent call last):
  File "/usr/local/bin/easyocr", line 5, in <module>
    from easyocr.cli import main
  File "/usr/local/lib/python3.8/dist-packages/easyocr/__init__.py", line 1, in <module>
    from .easyocr import Reader
  File "/usr/local/lib/python3.8/dist-packages/easyocr/easyocr.py", line 3, in <module>
    from .recognition import get_recognizer, get_text
  File "/usr/local/lib/python3.8/dist-packages/easyocr/recognition.py", line 6, in <module>
    import torchvision.transforms as transforms
  File "/usr/local/lib/python3.8/dist-packages/torchvision/__init__.py", line 6, in <module>
    from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils
  File "/usr/local/lib/python3.8/dist-packages/torchvision/_meta_registrations.py", line 4, in <module>
    import torch._custom_ops
ModuleNotFoundError: No module named 'torch._custom_ops'

Got this after installing easyocr version 1.7.1 and executing the command "easyocr -l en -f image.png" in accord with these instructions.  To "solve" this problem after finding nothing with search engines, i simply commented out the lines in the file /usr/local/lib/python3.8/dist-packages/torchvision/_meta_registrations.py that referred to _custom_ops.

Then I started getting all the normal errors that you get with easyocr as follows:
$ easyocr -l en -f test_en.jpg 
/usr/local/lib/python3.8/dist-packages/torchvision/io/image.py:13: UserWarning: Failed to load image Python extension: '/usr/local/lib/python3.8/dist-packages/torchvision/image.so: undefined symbol: _ZN3c104cuda9SetDeviceEi'If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?
  warn(
Neither CUDA nor MPS are available - defaulting to CPU. Note: This module is much faster with a GPU.
([[2, 2], [1002, 2], [1002, 34], [2, 34]], 'As Warren Buffett; one of the most successful investors of all time, said: "If you don\'t find', 0.6927859396455398)
([[2, 46], [724, 46], [724, 78], [2, 78]], 'way to make money while you sleep; you will work until you die"', 0.5957261107425325)

No idea about the "normal" errors, but at least you can start using easyocr like this:

$ easyocr -l en --detail 0 -f test_en.jpg  2>>/dev/null
As Warren Buffett; one of the most successful investors of all time, said: "If you don't find
way to make money while you sleep; you will work until you die"

Hope that helps.

P.S. If any of you know of an easy way to add Hebrew to my easyocr install, please let me know.
Currently getting this when trying to use easyocr to get Hebrew:

File "/usr/local/lib/python3.8/dist-packages/easyocr/easyocr.py", line 109, in __init__
    raise ValueError(unknown_lang, 'is not supported')
ValueError: ({'he'}, 'is not supported')

My favorite search engines are not suggesting anything to help me with this issue.  Imagine that.