pyinstaller打包openvino 2021.4.2

打包准备

1. 测试环境准备

conda create -n opinstall python=3.7 -y
conda activate opinstall

pip install openvino==2021.4.2
pip install pyinstaller

PyCharm新建openvino_install,选择虚拟环境opinstall,编写测试代码

app.py

import numpy as np
from openvino.inference_engine import IECore

if __name__ == '__main__':
    print("start")
    e = IECore()
    print("end")

2. 将openvino文件夹复制到代码同级目录下
D:\ProgramData\anaconda3\envs\opinstall\Lib\site-packages\openvino 拷贝至 F:\openvino_install

pyinstaller打包openvino 2021.4.2_第1张图片

3. 修改配置
# 将运行库路径修改为同级目录下的openvino目录
打开openvino/inference_engine/_init_.py

》修改:openvino_libs = [os.path.join(os.path.dirname(__file__), "..", "..", "openvino", "libs")] 为:openvino_libs = [os.path.join(os.getcwd(),'openvino\\libs') ]

》增加python版本小于等于3.8环境配置,否则报错DLL load failed while importing ie_api: 找不到指定的模块。
if (3, 8) <= sys.version_info:
os.add_dll_directory(os.path.abspath(lib_path))
os.environ["PATH"] = os.path.abspath(lib_path) + ";" + os.environ["PATH"]
else:
os.environ["PATH"] = os.path.abspath(lib_path) + ";" + os.environ["PATH"]
》增加:
from .ie_api import *
from .constants import *

# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import os
import sys

if sys.platform == 'win32':
    # Installer, yum, pip installs openvino dlls to the different directories
    # and those paths need to be visible to the openvino modules
    #
    # If you're using a custom installation of openvino,
    # add the location of openvino dlls to your system PATH.
    #
    # looking for the libs in the pip installation path by default.
    openvino_libs = [os.path.join(os.getcwd(),'openvino\\libs') ]
    # setupvars.bat script set all libs paths to OPENVINO_LIB_PATHS environment variable.
    openvino_libs_installer = os.getenv('OPENVINO_LIB_PATHS')
    if openvino_libs_installer:
        openvino_libs.extend(openvino_libs_installer.split(';'))
    for lib in openvino_libs:
        lib_path = os.path.join(os.path.dirname(__file__), lib)
        if os.path.isdir(lib_path):
            # On Windows, with Python >= 3.8, DLLs are no longer imported from the PATH.
            if (3, 8) <= sys.version_info:
                os.add_dll_directory(os.path.abspath(lib_path))
                os.environ["PATH"] = os.path.abspath(lib_path) + ";" + os.environ["PATH"]
            else:
                os.environ["PATH"] = os.path.abspath(lib_path) + ";" + os.environ["PATH"] 

from .ie_api import *
from .constants import *

__all__ = ['IENetwork', 'TensorDesc', 'IECore', 'Blob', 'PreProcessInfo', 'get_version']
__version__ = get_version()  # type: ignore

开始打包 -> dist下生成exe文件 -> 根目录下的openvino文件夹拷贝到dist

G:\openvino_install>conda activate opinstall

(opinstall) G:\openvino_install>pyinstaller --onefile --hidden-import pkgutil app.py

pyinstaller打包openvino 2021.4.2_第2张图片

运行方式1:执行openvino/bin/setupvars.bat脚本去初始化环境,再运行

G:\openvino_install\dist>conda activate opinstall

(opinstall) G:\openvino_install\dist>cd C:\Program Files (x86)\Intel\openvino_2021.4.752\bin

(opinstall) G:\openvino_install\dist>C:

(opinstall) C:\Program Files (x86)\Intel\openvino_2021.4.752\bin>setupvars.bat
Python 3.7.12
[setupvars.bat] OpenVINO environment initialized

(opinstall) C:\Program Files (x86)\Intel\openvino_2021.4.752\bin>cd G:\openvino_install\dist

(opinstall) C:\Program Files (x86)\Intel\openvino_2021.4.752\bin>G: 

(opinstall) G:\openvino_install\dist>app.exe
start
end

(opinstall) G:\openvino_install\dist>

运行方式2:不使用setupvars.bat脚本去初始化环境,打包openvino开发库,在代码中加载环境路径

conda activate opinstall
cd C:\Program Files (x86)\Intel\openvino_2021.4.752\deployment_tools\tools\deployment_manager
python deployment_manager.py

pyinstaller打包openvino 2021.4.2_第3张图片

在该界面下输入1回车,8回车,最后在回车(我使用的py3.7)按照自己的py版本进行选择,执行完,生成openvino_deploy_package.zip包

pyinstaller打包openvino 2021.4.2_第4张图片

解压压缩包下的文件夹 openvino_deploy_package\deployment_tools到dist目录下

pyinstaller打包openvino 2021.4.2_第5张图片

 修改代码,自动加载环境路径

import os

cur_path = os.getcwd().replace('\\', '/')
hdll_path = cur_path + '/deployment_tools/inference_engine/external/hddl/bin;'
tbb_path = cur_path + '/deployment_tools/inference_engine/external/tbb/bin;'
Debug_path = cur_path + '/deployment_tools/inference_engine/bin/intel64/Debug;'
Release_path = cur_path + '/deployment_tools/inference_engine/bin/intel64/Release;'
ngraph_path = cur_path + '/deployment_tools/ngraph/lib;'
model_optimizer_path = cur_path + '/deployment_tools/model_optimizer;'
os.environ['Path'] += hdll_path + tbb_path + Debug_path + Release_path + ngraph_path + model_optimizer_path

import numpy as np
from openvino.inference_engine import IECore

if __name__ == '__main__':
    print("start auto")
    e = IECore()
    print("end")

pyinstaller打包openvino 2021.4.2_第6张图片

你可能感兴趣的:(openvino,python,人工智能)