Due to the needs of the project, the py file recognized by the picture needs to be packaged into an exe for C++ to call.
Packing uses Pyinstaller, but because of various errors prompted after packing paddleocr, it took a long time. Prompt errors include:
no mudels tools
file not found：ppocr_keys_v1.txt
Errors such as these will be displayed in the CMD window when the
.exe is running.
In order to solve these problems, many solutions have been searched on the Internet, but none of them can be completely solved. But there are still some methods that are of great reference value. Now integrate these methods and share them with everyone. Hope to help everyone.
There are probably three reasons for the above-mentioned errors:
caused by incorrect packaging statements
The statement is used as follows, and the environment path can be replaced by your own
pyinstaller -F --clean --exclude matplotlib -p C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\paddleocr;C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\paddle\libs img2txt_v3.py -i img2txt_v3.ico --add-binary C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\paddle\libs;. --add-data .\model;.\model --additional-hooks-dir=.
When the package reports an error similar to the following:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\1\\_MEI54762\\.....'
The reason is that the hook file needs to be generated when packaging, if not, we need to create it manually. The following is the hook file created when I reported an error. You only need to change to your own module name:
# hook-paddle.py from PyInstaller.utils.hooks import collect_data_files datas = collect_data_files("paddle") # hook-ppocr.py from PyInstaller.utils.hooks import collect_data_files datas = collect_data_files("ppocr")
After saving, put it in the pyinstaller folder, path: python root directory\Lib\site-packages\PyInstaller\hooks
The problem on my side is that I can’t find the ‘prompt.ppocr\utils\ppocr_keys_v1.txt file not found’ file. I have been looking for this problem for a long time, and need to modify the source code of paddle.
Some people say that you want to write an absolute path, but I have a problem with it, so I used a relative path, but if you have a problem with a relative path, you can try the absolute path ~~
Replace the original path to obtain txt with your own.
Modify the source code image.py
# if six.PY3: # import subprocess # import sys # import_cv2_proc = subprocess.Popen( # [sys.executable, "-c", "import cv2"], # stdout=subprocess.PIPE, # stderr=subprocess.PIPE) # out, err = import_cv2_proc.communicate() # retcode = import_cv2_proc.poll() # if retcode != 0: # cv2 = None # else: # import cv2 # else: # try: # import cv2 # except ImportError: # cv2 = None try: import cv2 except ImportError: cv2 = None
By default, the first time it runs, the simple model will be downloaded, and the storage path is:
But what if you need to run exe without internet? You can specify the algorithm model path, modify your code as follows:
ocr = PaddleOCR(use_angle_cls=True, lang='ch', det_model_dir=r'D:\00work\model\det', rec_model_dir=r'D:\00work\model\rec\ch', cls_model_dir=r'D:\00work\model\cls') result = ocr.ocr(image, cls=True)
Just specify the path in the code
After all the problems are solved, you can pack your py file.