1

below is my code fragment

from PIL import Image
import cv2
import numpy as np
from keras.models import load_model


    def main():
        image = Image.open(
            r"D:\Python Projects\ValidateCodeCrash\HDImagesForPredict\e6dm.png")
        img = cv2.cvtColor(np.asarray(
            image.convert("L")), cv2.COLOR_RGB2BGR)
        _, gray = cv2.threshold(img, 96, 255, cv2.THRESH_BINARY)
        model = load_model(
            r'D:\Python Projects\ValidateCodeRecognition\RPA\Image_Class2.h5')
        width, height = 100, 25
        X = np.zeros((1, height, width, 3), dtype=np.uint8)
        X[0] = gray
        y_pred = model.predict(X)
        y_pred = np.argmax(y_pred, axis=2)
        characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

        output = characters[y_pred[:, 0][0]]+characters[y_pred[:, 0][1]
                                                        ] + characters[y_pred[:, 0][2]]+characters[y_pred[:, 0][3]]
        print(output)


    if __name__ == "__main__":
        main()

I can run it by the command python myfile.py, but when I compiled it to exe file by command pyinstaller -F myfile.py and run it, I got errors like below

[20776] Failed to execute script myfile
Using TensorFlow backend.
Traceback (most recent call last):
  File "hb56crash.py", line 4, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\keras\__init__.py", line 3, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\keras\utils\__init__.py", line 6, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\keras\utils\conv_utils.py", line 9, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\keras\backend\__init__.py", line 1, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\keras\backend\load_backend.py", line 90, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\tensorflow\__init__.py", line 98, in <module>
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\tensorflow_core\__init__.py", line 40, in <module>
  File "site-packages\tensorflow\__init__.py", line 50, in __getattr__
  File "site-packages\tensorflow\__init__.py", line 44, in _load
  File "importlib\__init__.py", line 126, in import_module
  File "c:\users\michael_gao\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
ImportError: cannot import name 'pywrap_tensorflow'

I used pyinstaller to compile python files before. It's Ok. It seems the difference is I use keras in this python file(I guess.....). How can I fix the problem? thanks!

user2155362
  • 1,657
  • 5
  • 18
  • 30
  • possible duplicate: https://stackoverflow.com/questions/35953210/error-running-basic-tensorflow-example – Harshal Parekh Dec 07 '19 at 07:55
  • There is no problem with the code, here Keras using Tensorflow backend. Please `pip install tensorflow --upgrade --force-reinstall` or `!pip install tensorflow==2.0 --force-reinstall`, will resolves your problem –  May 13 '20 at 16:28

0 Answers0