open-mmlab / mmocr

OpenMMLab Text Detection, Recognition and Understanding Toolbox
https://mmocr.readthedocs.io/en/dev-1.x/
Apache License 2.0
4.3k stars 745 forks source link

[Bug] mmocr not working on mac anymore (MacBook M3 chip) #2045

Open R4Ajeti opened 4 months ago

R4Ajeti commented 4 months ago

Prerequisite

Task

I'm using the official example scripts/configs for the officially supported tasks/models/datasets.

Branch

main branch https://github.com/open-mmlab/mmocr

Environment

python mmocr/utils/collect_env.py sys.platform: darwin Python: 3.12.1 (main, Apr 10 2024, 22:20:38) [Clang 15.0.0 (clang-1500.3.9.4)] CUDA available: False MUSA available: False numpy_random_seed: 2147483648 GCC: Apple clang version 15.0.0 (clang-1500.3.9.4) PyTorch: 2.2.2 PyTorch compiling details: PyTorch built with:

TorchVision: 0.17.2 OpenCV: 4.9.0 MMEngine: 0.10.4 MMOCR: 1.0.1+2caab0a

Reproduces the problem - code sample

Device: MacBook M3 chip

Following installation using this link: https://github.com/open-mmlab/mmocr

Also following discussion from here: https://github.com/open-mmlab/mmcv/issues/204

Nothing worked for me to make OCR work.

The installation process is going all just fine but the package is not working when It comes to OCR samples.

I am getting this error: File "../env/lib/python3.12/site-packages/mmcv/utils/ext_loader.py", line 13, in load_ext ext = importlib.import_module('mmcv.' + name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File " File "../.pyenv/versions/3.12.1/lib/python3.12/importlib/init.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ModuleNotFoundError: No module named 'mmcv._ext'"

$ pip list | grep mm Is giving me

mmcv 2.1.0 mmdet 3.3.0 mmengine 0.10.4 mmocr 1.0.1 ../repositories/mmocr

What am I doing wrong? Does anyone, have any experience on mac releted to this one?

BTW: I am using python3.12.1

Reproduces the problem - command or script

from mmocr.apis import MMOCRInferencer from mmocr.utils import poly2bbox import numpy as np import time import cv2

Load models into memory

ocr = MMOCRInferencer(det='DBNet', rec=None, device='cpu') # Time taken: 1.3455498218536377 seconds

Perform inference

startTime = time.time() aaa = ocr('../mmocr/images/00_04_24_000-00_04_24_375.jpg') print(aaa["predictions"]) print([poly2bbox(poly) for poly in aaa["predictions"][0]["det_polygons"]]) print(f"Time taken: {time.time() - startTime} seconds")

img = cv2.imread('images/00_04_24_000-00_04_24_375.jpg', cv2.IMREAD_COLOR)

window_name = 'Image'

for text_lines_one in aaa["predictions"][0]["det_polygons"]:

Polygon corner points coordinates

pts = np.array(text_lines_one,
            np.int32)

pts = pts.reshape((-1, 1, 2))

isClosed = True

# Blue color in BGR
color = (255, 0, 0)

# Line thickness of 2 px
thickness = 2

# Using cv2.polylines() method
# Draw a Blue polygon with 
# thickness of 1 px
image = cv2.polylines(img, [pts], 
                isClosed, color, thickness)

Displaying the image

while(1):

cv2.imshow('image', image)
if cv2.waitKey(20) & 0xFF == 27:
    break

cv2.destroyAllWindows()

Reproduces the problem - error message

I am using "DBNet"

Additional information

Device: MacBook M3 chip

Following installation using this link: https://github.com/open-mmlab/mmocr

Also following discussion from here: https://github.com/open-mmlab/mmcv/issues/204

Nothing worked for me to make OCR work.

The installation process is going all just fine but the package is not working when It comes to OCR samples.

I am getting this error: File "../env/lib/python3.12/site-packages/mmcv/utils/ext_loader.py", line 13, in load_ext ext = importlib.import_module('mmcv.' + name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File " File "../.pyenv/versions/3.12.1/lib/python3.12/importlib/init.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ModuleNotFoundError: No module named 'mmcv._ext'"

$ pip list | grep mm Is giving me

mmcv 2.1.0 mmdet 3.3.0 mmengine 0.10.4 mmocr 1.0.1 ../repositories/mmocr

What am I doing wrong? Does anyone, have any experience on mac releted to this one?

BTW: I am using python3.12.1

R4Ajeti commented 4 months ago

I found a temporary fix while debugging:

  1. go to file: "env/lib/python3.12/site-packages/mmcv/utils/ext_loader.py"
  2. change line 13 - 17 with this code:

From:

ext = importlib.import_module('mmcv.' + name)

        for fun in funcs:
            assert hasattr(ext, fun), f'{fun} miss in module {name}'
        return ext

to:

if not name == "_ext":
            ext = importlib.import_module('mmcv.' + name)

            for fun in funcs:
                assert hasattr(ext, fun), f'{fun} miss in module {name}'
            return ext
zhouzaida commented 4 months ago

Hi @R4Ajeti , how did you install mmcv? You could try to uninstall mmcv and follow the below commands to install it again.

pip install openmim
mim install mmcv==2.1.0