Search code examples
pythontesseractpython-tesseractpyttsx

pyttsx3: OSError 2147221008 CoInitialize has not been called


I have a Flask app which picks up an image file(s), uses Tesseract OCR to extract the text then converts that text to mp3 using pyttsx3. It saves both .txt and mp3 files in the same directory.

The first time it works fine.

However, run it again without restarting the app and the pyttsx3 operation fails with:

OSError: [WinError -2147221008] CoInitialize has not been called

Traceback (most recent call last)

File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38\Lib\site-packages\pyttsx3\__init__.py", line 20, in init

eng = _activeEngines[driverName]

File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38\Lib\weakref.py", line 131, in __getitem__

o = self.data[key]()

Commenting out the call to pyttsx3 solves the error - it's now possible to upload multiple images one after the other without it crashing, so it's definitely pyttsx3 that's causing the glitch.

It seems maybe like the loop is still running and so it can't handle the next image as it's still busy with the first one.

Tried so far:

  • Used pyttsx3 version 2.71 - app failed with a different coinitialise error
  • Used 32 bit python interpreter - no difference
  • Used session instead of global variable for the text - no difference

Windows 10 Pro v1903

Browsers tried: Edge, Chrome, Opera Python version 3.8.4 64bit

pyttsx3 v 2.90

IDE = VS Code

Help appreciated


Solution

  • Turns out this is a machine specific error. Still no idea what's causing it but on another Windows 10 machine and a Linux box and the app runs fine, error-free.