recursion is detected during loading of "cv2" binary extensions

I have a small program that throws an opencv error after compiling with pyinstaller, but it works without compilation. I use Python 3.8.10 on Windows 10.

Program:

import pyautogui
import numpy as np
import cv2
try:
    from PIL import Image
except ImportError:
    import Image

screenshot = pyautogui.screenshot('screenshot.png', region=(970, 591, 184, 101)) # start
img = cv2.imread('screenshot.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.bitwise_not(img)

Error:

['C:\\Users\\n1kro\\AppData\\Local\\Temp\\_MEI83642\\base_library.zip', 'C:\\Users\\n1kro\\AppData\\Local\\Temp\\_MEI83642\\lib-dynload', 'C:\\Users\\n1kro\\AppData\\Local\\Temp\\_MEI83642']
Traceback (most recent call last):
File "test.py", line 3, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "cv2\__init__.py", line 180, in <module>
File "cv2\__init__.py", line 75, in bootstrap
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.         [3416] Failed to execute script 'test' due to unhandled exception!

I found this GitHub issue, but I don't understand how to fix the problem. Reinstalling everything didn't help. How can I properly use cv2?

Upvotes: 12

Views: 30909

Answers (3)

Silveri
Silveri

Reputation: 5261

To get around this issue, you can either:

  • Use an older version of OpenCV (e.g. opencv-python==4.5.3.56 instead of 4.6.0.66) as mentioned by the others.
  • Wait for a new version of PyInstaller and pyinstaller-hooks-contrib (possibly v5.3 and v2022.9 according to https://github.com/pyinstaller/pyinstaller/issues/6964#issuecomment-1193333632).
  • Install and compile the development build of pyInstaller that contains a fix for this issue (see thread in comment linked above).

Upvotes: 7

Nemo Vetinari
Nemo Vetinari

Reputation: 181

open cmd and use pip to install a different version:

 pip install opencv-python==4.5.3.56

no problems with pyinstaller after it

Upvotes: 18

pizzadude190
pizzadude190

Reputation: 31

I was able to resolve this by uninstalling opencv and installing an older version. Version 4.5.3 worked well.

Upvotes: 3

Related Questions