使用 PyQt5 自动初始化的 OpenVINO 环境无法正常工作
OpenVINO environment initialized automatically with PyQt5 could not work normally
我已经集成了 OpenVINO 和 PyQt5 来完成推理工作
like this 在 Windows 11.
因为我们必须先执行 setupvars.bat 来初始化 OpenVINO 环境。
我认为用户在进行推理工作之前手动初始化 OpenVINO 环境很麻烦。
因此,我尝试用我的 PyQt5 代码构建自动初始化程序。
from PyQt5 import QtWidgets
import os
#import time
import sys
if __name__ == '__main__':
cwd = os.getcwd()
os.chdir(r"C:\Program Files (x86)\Intel\openvino_2021.4.752\bin")
os.system("setupvars.bat")
#time.sleep(1)
os.chdir(cwd)
from controller import MainWindow
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
执行我的程序后,虽然我可以在命令提示符上看到已初始化的消息,但之后找不到名为 'openvino' 的模块。
Python 3.9.7
[setupvars.bat] OpenVINO environment initialized
Traceback (most recent call last):
File "C:\Users\Hsien\Desktop\pyqt\start.py", line 16, in <module>
from controller import MainWindow
File "C:\Users\Hsien\Desktop\pyqt\controller.py", line 8, in <module>
from inference_engine import openvino_inference_engine
File "C:\Users\Hsien\Desktop\pyqt\inference_engine.py", line 1, in <module>
from openvino.inference_engine import IECore, Blob, TensorDesc
ModuleNotFoundError: No module named 'openvino'
我也尝试使用sleep
功能来延迟等待OpenVINO环境设置但徒劳。
但是这个问题可以通过在同一个命令提示符下手动执行 C:\Program Files (x86)\Intel\openvino_2021.4.752\bin\setupvars.bat
来解决。
如果我先手动初始化 OpenVINO 环境,所有功能都能正常执行。
(我的环境是Windows11,版本是openvino_2021.4.752。)
我的controller.py
来了
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog, QListWidgetItem, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtCore import QDir
from PyQt5.QtGui import QPixmap, QImage
import cv2
from ui import Ui_Window
import global_variable as gv
from inference_engine import openvino_inference_engine
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_Window()
self.ui.setupUi(self)
self.setup_control()
def setup_control(self):
self.ui.BrowseFolderPath.clicked.connect(self.choose_folder)
self.ui.Reload.clicked.connect(self.refresh_list)
self.ui.ShowFileName.itemClicked.connect(self.show_which_image)
self.ui.Inference.clicked.connect(self.show_inference_result)
self.display_logo()
def choose_folder(self):
gv.gFpath = QFileDialog.getExistingDirectory(self, "Please choose OCT InImages folder")
self.ui.ShowFolderPath.setText(gv.gFpath)
gv.gIpath = QDir(gv.gFpath).entryList(['*.jpg', '*.jpeg', '*.png'])
self.ui.ShowFileName.clear()
for i in gv.gIpath:
QListWidgetItem(self.ui.ShowFileName).setText(i)
def refresh_list(self):
gv.gIpath = QDir(gv.gFpath).entryList(['*.jpg', '*.jpeg', '*.png'])
self.ui.ShowFileName.clear()
for i in gv.gIpath:
QListWidgetItem(self.ui.ShowFileName).setText(i)
def show_which_image(self, item):
gv.gInImgPath = gv.gFpath + '/' + item.text()
self.InImage = QPixmap()
self.InImage.load(gv.gInImgPath)
self.ui.ShowInputImage.scene = QGraphicsScene()
self.ui.ShowInputImage.scene.addItem(QGraphicsPixmapItem(self.InImage.scaledToWidth(600)))
self.ui.ShowInputImage.setScene(self.ui.ShowInputImage.scene)
self.reset_output_image()
def reset_output_image(self):
self.OutImage = QPixmap()
self.OutImage.load("inferencing.jpg")
self.ui.ShowOutputImage.scene = QGraphicsScene()
self.ui.ShowOutputImage.scene.addItem(QGraphicsPixmapItem(self.OutImage))
self.ui.ShowOutputImage.setScene(self.ui.ShowOutputImage.scene)
def show_inference_result(self):
openvino_inference_engine();
self.OutImage = QPixmap()
self.OutImage.load("tmp.jpg")
self.ui.ShowOutputImage.scene = QGraphicsScene()
self.ui.ShowOutputImage.scene.addItem(QGraphicsPixmapItem(self.OutImage.scaledToWidth(430)))
self.ui.ShowOutputImage.setScene(self.ui.ShowOutputImage.scene)
def display_logo(self):
self.img = cv2.imread("logo.png")
height, width, channel = self.img.shape
bytesPerline = 3 * width
self.qimg = QImage(self.img, width, height, bytesPerline, QImage.Format_RGB888).rgbSwapped()
self.ui.logo.setPixmap(QPixmap.fromImage(self.qimg))
OpenVINOsetupvars.bat下面
@echo off
:: Copyright (C) 2018-2021 Intel Corporation
:: SPDX-License-Identifier: Apache-2.0
set ROOT=%~dp0
call :GetFullPath "%ROOT%\.." ROOT
set SCRIPT_NAME=%~nx0
set "INTEL_OPENVINO_DIR=%ROOT%"
set "INTEL_CVSDK_DIR=%INTEL_OPENVINO_DIR%"
set "python_version="
:: command line arguments parsing
:input_arguments_loop
if not "%1"=="" (
if "%1"=="-pyver" (
set "python_version=%2"
shift
)
shift
goto :input_arguments_loop
)
:: OpenCV
if exist "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat" (
call "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat"
) else (
set "OpenCV_DIR=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\lib"
set "PATH=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\bin;%PATH%"
)
:: Model Optimizer
if exist %INTEL_OPENVINO_DIR%\deployment_tools\model_optimizer (
set PYTHONPATH=%INTEL_OPENVINO_DIR%\deployment_tools\model_optimizer;%PYTHONPATH%
set "PATH=%INTEL_OPENVINO_DIR%\deployment_tools\model_optimizer;%PATH%"
)
:: Inference Engine
set "InferenceEngine_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\share"
set "HDDL_INSTALL_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\hddl"
set "OPENMP_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\omp\lib"
set "GNA_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\gna\lib"
set "OPENVINO_LIB_PATHS=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Release;%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Debug;%HDDL_INSTALL_DIR%\bin;%OPENMP_DIR%;%GNA_DIR%;%OPENVINO_LIB_PATHS%"
if exist %INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions (
set ARCH_ROOT_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions
)
if exist %INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions (
set ARCH_ROOT_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions
)
:: TBB
if exist %INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\tbb (
set "OPENVINO_LIB_PATHS=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\tbb\bin;%OPENVINO_LIB_PATHS%"
set "TBB_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\tbb\cmake"
)
:: nGraph
if exist %INTEL_OPENVINO_DIR%\deployment_tools\ngraph (
set "OPENVINO_LIB_PATHS=%INTEL_OPENVINO_DIR%\deployment_tools\ngraph\lib;%OPENVINO_LIB_PATHS%"
set "ngraph_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\ngraph\cmake"
)
:: Add libs dirs to the PATH
set "PATH=%OPENVINO_LIB_PATHS%;%PATH%"
:: Check if Python is installed
python --version 2>NUL
if errorlevel 1 (
echo Error^: Python is not installed. Please install one of Python 3.6 - 3.8 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
:: Check Python version if user did not pass -pyver
if "%python_version%" == "" (
for /F "tokens=* USEBACKQ" %%F IN (`python -c "import sys; print(str(sys.version_info[0])+'.'+str(sys.version_info[1]))" 2^>^&1`) DO (
set python_version=%%F
)
)
for /F "tokens=1,2 delims=. " %%a in ("%python_version%") do (
set pyversion_major=%%a
set pyversion_minor=%%b
)
if "%pyversion_major%" geq "3" (
if "%pyversion_minor%" geq "6" (
set check_pyversion=okay
)
)
if not "%check_pyversion%"=="okay" (
echo Unsupported Python version. Please install one of Python 3.6 - 3.8 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
:: Check Python bitness
python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2 > NUL
if errorlevel 1 (
echo Error^: Error during installed Python bitness detection
exit /B 1
)
for /F "tokens=* USEBACKQ" %%F IN (`python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2^>^&1`) DO (
set bitness=%%F
)
if not "%bitness%"=="64" (
echo Unsupported Python bitness. Please install one of Python 3.6 - 3.8 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
set PYTHONPATH=%INTEL_OPENVINO_DIR%\python\python%pyversion_major%.%pyversion_minor%;%INTEL_OPENVINO_DIR%\python\python3;%PYTHONPATH%
if exist %INTEL_OPENVINO_DIR%\deployment_tools\open_model_zoo\tools\accuracy_checker (
set PYTHONPATH=%INTEL_OPENVINO_DIR%\deployment_tools\open_model_zoo\tools\accuracy_checker;%PYTHONPATH%
)
if exist %INTEL_OPENVINO_DIR%\deployment_tools\tools\post_training_optimization_toolkit (
set PYTHONPATH=%INTEL_OPENVINO_DIR%\deployment_tools\tools\post_training_optimization_toolkit;%PYTHONPATH%
)
echo [setupvars.bat] OpenVINO environment initialized
exit /B 0
:GetFullPath
SET %2=%~f1
GOTO :EOF
首先,请注意 Windows11 不受 OpenVINO 官方支持。因此,预计会出现问题。 PyQt5 与 OpenVINO 包的集成也是如此。
您可以在 Windows 中使用批处理文件创建自动执行 OpenVINO setupvars.bat 初始化。
创建一个批处理文件来初始化 OpenVINO 环境并启动您想要的软件(例如:VS2019)
在.bat文件中写入如下脚本:
@echo on
cd \bin
呼叫setupvars.bat
cd 19\Community\Common7\IDE
呼叫devenv.exe
运行 .bat 文件作为管理员
由于官方不支持 OpenVINO 和 PyQt5 的集成,因此没有具体的文档作为指导。
另一方面,openvino-smart-library 中也实施了类似的集成。您可以参考此存储库以获取 reference/workaround.
除此之外,您还需要将 OpenVINO 推理引擎集成到您的应用程序中(为此仅支持 C++ 或 Python)。你可以参考这个Integrate Inference Engine with Your Python Application.
我已经集成了 OpenVINO 和 PyQt5 来完成推理工作 like this 在 Windows 11.
因为我们必须先执行 setupvars.bat 来初始化 OpenVINO 环境。
我认为用户在进行推理工作之前手动初始化 OpenVINO 环境很麻烦。
因此,我尝试用我的 PyQt5 代码构建自动初始化程序。
from PyQt5 import QtWidgets
import os
#import time
import sys
if __name__ == '__main__':
cwd = os.getcwd()
os.chdir(r"C:\Program Files (x86)\Intel\openvino_2021.4.752\bin")
os.system("setupvars.bat")
#time.sleep(1)
os.chdir(cwd)
from controller import MainWindow
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
执行我的程序后,虽然我可以在命令提示符上看到已初始化的消息,但之后找不到名为 'openvino' 的模块。
Python 3.9.7
[setupvars.bat] OpenVINO environment initialized
Traceback (most recent call last):
File "C:\Users\Hsien\Desktop\pyqt\start.py", line 16, in <module>
from controller import MainWindow
File "C:\Users\Hsien\Desktop\pyqt\controller.py", line 8, in <module>
from inference_engine import openvino_inference_engine
File "C:\Users\Hsien\Desktop\pyqt\inference_engine.py", line 1, in <module>
from openvino.inference_engine import IECore, Blob, TensorDesc
ModuleNotFoundError: No module named 'openvino'
我也尝试使用sleep
功能来延迟等待OpenVINO环境设置但徒劳。
但是这个问题可以通过在同一个命令提示符下手动执行 C:\Program Files (x86)\Intel\openvino_2021.4.752\bin\setupvars.bat
来解决。
如果我先手动初始化 OpenVINO 环境,所有功能都能正常执行。
(我的环境是Windows11,版本是openvino_2021.4.752。)
我的controller.py
来了from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog, QListWidgetItem, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtCore import QDir
from PyQt5.QtGui import QPixmap, QImage
import cv2
from ui import Ui_Window
import global_variable as gv
from inference_engine import openvino_inference_engine
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_Window()
self.ui.setupUi(self)
self.setup_control()
def setup_control(self):
self.ui.BrowseFolderPath.clicked.connect(self.choose_folder)
self.ui.Reload.clicked.connect(self.refresh_list)
self.ui.ShowFileName.itemClicked.connect(self.show_which_image)
self.ui.Inference.clicked.connect(self.show_inference_result)
self.display_logo()
def choose_folder(self):
gv.gFpath = QFileDialog.getExistingDirectory(self, "Please choose OCT InImages folder")
self.ui.ShowFolderPath.setText(gv.gFpath)
gv.gIpath = QDir(gv.gFpath).entryList(['*.jpg', '*.jpeg', '*.png'])
self.ui.ShowFileName.clear()
for i in gv.gIpath:
QListWidgetItem(self.ui.ShowFileName).setText(i)
def refresh_list(self):
gv.gIpath = QDir(gv.gFpath).entryList(['*.jpg', '*.jpeg', '*.png'])
self.ui.ShowFileName.clear()
for i in gv.gIpath:
QListWidgetItem(self.ui.ShowFileName).setText(i)
def show_which_image(self, item):
gv.gInImgPath = gv.gFpath + '/' + item.text()
self.InImage = QPixmap()
self.InImage.load(gv.gInImgPath)
self.ui.ShowInputImage.scene = QGraphicsScene()
self.ui.ShowInputImage.scene.addItem(QGraphicsPixmapItem(self.InImage.scaledToWidth(600)))
self.ui.ShowInputImage.setScene(self.ui.ShowInputImage.scene)
self.reset_output_image()
def reset_output_image(self):
self.OutImage = QPixmap()
self.OutImage.load("inferencing.jpg")
self.ui.ShowOutputImage.scene = QGraphicsScene()
self.ui.ShowOutputImage.scene.addItem(QGraphicsPixmapItem(self.OutImage))
self.ui.ShowOutputImage.setScene(self.ui.ShowOutputImage.scene)
def show_inference_result(self):
openvino_inference_engine();
self.OutImage = QPixmap()
self.OutImage.load("tmp.jpg")
self.ui.ShowOutputImage.scene = QGraphicsScene()
self.ui.ShowOutputImage.scene.addItem(QGraphicsPixmapItem(self.OutImage.scaledToWidth(430)))
self.ui.ShowOutputImage.setScene(self.ui.ShowOutputImage.scene)
def display_logo(self):
self.img = cv2.imread("logo.png")
height, width, channel = self.img.shape
bytesPerline = 3 * width
self.qimg = QImage(self.img, width, height, bytesPerline, QImage.Format_RGB888).rgbSwapped()
self.ui.logo.setPixmap(QPixmap.fromImage(self.qimg))
OpenVINOsetupvars.bat下面
@echo off
:: Copyright (C) 2018-2021 Intel Corporation
:: SPDX-License-Identifier: Apache-2.0
set ROOT=%~dp0
call :GetFullPath "%ROOT%\.." ROOT
set SCRIPT_NAME=%~nx0
set "INTEL_OPENVINO_DIR=%ROOT%"
set "INTEL_CVSDK_DIR=%INTEL_OPENVINO_DIR%"
set "python_version="
:: command line arguments parsing
:input_arguments_loop
if not "%1"=="" (
if "%1"=="-pyver" (
set "python_version=%2"
shift
)
shift
goto :input_arguments_loop
)
:: OpenCV
if exist "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat" (
call "%INTEL_OPENVINO_DIR%\opencv\setupvars.bat"
) else (
set "OpenCV_DIR=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\lib"
set "PATH=%INTEL_OPENVINO_DIR%\opencv\x64\vc14\bin;%PATH%"
)
:: Model Optimizer
if exist %INTEL_OPENVINO_DIR%\deployment_tools\model_optimizer (
set PYTHONPATH=%INTEL_OPENVINO_DIR%\deployment_tools\model_optimizer;%PYTHONPATH%
set "PATH=%INTEL_OPENVINO_DIR%\deployment_tools\model_optimizer;%PATH%"
)
:: Inference Engine
set "InferenceEngine_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\share"
set "HDDL_INSTALL_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\hddl"
set "OPENMP_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\omp\lib"
set "GNA_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\gna\lib"
set "OPENVINO_LIB_PATHS=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Release;%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\Debug;%HDDL_INSTALL_DIR%\bin;%OPENMP_DIR%;%GNA_DIR%;%OPENVINO_LIB_PATHS%"
if exist %INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions (
set ARCH_ROOT_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions
)
if exist %INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions (
set ARCH_ROOT_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\bin\intel64\arch_descriptions
)
:: TBB
if exist %INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\tbb (
set "OPENVINO_LIB_PATHS=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\tbb\bin;%OPENVINO_LIB_PATHS%"
set "TBB_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\inference_engine\external\tbb\cmake"
)
:: nGraph
if exist %INTEL_OPENVINO_DIR%\deployment_tools\ngraph (
set "OPENVINO_LIB_PATHS=%INTEL_OPENVINO_DIR%\deployment_tools\ngraph\lib;%OPENVINO_LIB_PATHS%"
set "ngraph_DIR=%INTEL_OPENVINO_DIR%\deployment_tools\ngraph\cmake"
)
:: Add libs dirs to the PATH
set "PATH=%OPENVINO_LIB_PATHS%;%PATH%"
:: Check if Python is installed
python --version 2>NUL
if errorlevel 1 (
echo Error^: Python is not installed. Please install one of Python 3.6 - 3.8 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
:: Check Python version if user did not pass -pyver
if "%python_version%" == "" (
for /F "tokens=* USEBACKQ" %%F IN (`python -c "import sys; print(str(sys.version_info[0])+'.'+str(sys.version_info[1]))" 2^>^&1`) DO (
set python_version=%%F
)
)
for /F "tokens=1,2 delims=. " %%a in ("%python_version%") do (
set pyversion_major=%%a
set pyversion_minor=%%b
)
if "%pyversion_major%" geq "3" (
if "%pyversion_minor%" geq "6" (
set check_pyversion=okay
)
)
if not "%check_pyversion%"=="okay" (
echo Unsupported Python version. Please install one of Python 3.6 - 3.8 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
:: Check Python bitness
python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2 > NUL
if errorlevel 1 (
echo Error^: Error during installed Python bitness detection
exit /B 1
)
for /F "tokens=* USEBACKQ" %%F IN (`python -c "import sys; print(64 if sys.maxsize > 2**32 else 32)" 2^>^&1`) DO (
set bitness=%%F
)
if not "%bitness%"=="64" (
echo Unsupported Python bitness. Please install one of Python 3.6 - 3.8 ^(64-bit^) from https://www.python.org/downloads/
exit /B 1
)
set PYTHONPATH=%INTEL_OPENVINO_DIR%\python\python%pyversion_major%.%pyversion_minor%;%INTEL_OPENVINO_DIR%\python\python3;%PYTHONPATH%
if exist %INTEL_OPENVINO_DIR%\deployment_tools\open_model_zoo\tools\accuracy_checker (
set PYTHONPATH=%INTEL_OPENVINO_DIR%\deployment_tools\open_model_zoo\tools\accuracy_checker;%PYTHONPATH%
)
if exist %INTEL_OPENVINO_DIR%\deployment_tools\tools\post_training_optimization_toolkit (
set PYTHONPATH=%INTEL_OPENVINO_DIR%\deployment_tools\tools\post_training_optimization_toolkit;%PYTHONPATH%
)
echo [setupvars.bat] OpenVINO environment initialized
exit /B 0
:GetFullPath
SET %2=%~f1
GOTO :EOF
首先,请注意 Windows11 不受 OpenVINO 官方支持。因此,预计会出现问题。 PyQt5 与 OpenVINO 包的集成也是如此。
您可以在 Windows 中使用批处理文件创建自动执行 OpenVINO setupvars.bat 初始化。
创建一个批处理文件来初始化 OpenVINO 环境并启动您想要的软件(例如:VS2019)
在.bat文件中写入如下脚本:
@echo on
cd \bin
呼叫setupvars.bat
cd 19\Community\Common7\IDE
呼叫devenv.exe
运行 .bat 文件作为管理员
由于官方不支持 OpenVINO 和 PyQt5 的集成,因此没有具体的文档作为指导。
另一方面,openvino-smart-library 中也实施了类似的集成。您可以参考此存储库以获取 reference/workaround.
除此之外,您还需要将 OpenVINO 推理引擎集成到您的应用程序中(为此仅支持 C++ 或 Python)。你可以参考这个Integrate Inference Engine with Your Python Application.