I have integrated OpenVINO and PyQt5 to do the inference job like this on Windows 11.
Because we have to initialize OpenVINO environment by executing setupvars.bat first.
I think that it is bothered for user to manually initialize OpenVINO environment before doing the inference job.
Therefore, I try to build up the auto-initialized program with my PyQt5 code.
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_())
After executing my program, though I can see the initialized message on my Command Prompt, module named 'openvino' cannot be found after that.
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'
I also try to use sleep
function to make a delay for waiting OpenVINO environment setup but in vain.
But this problem can be solved by manually executing C:\Program Files (x86)\Intel\openvino_2021.4.752\bin\setupvars.bat
on the same Command Prompt.
All functions execute well if I manually initialize OpenVINO environment first.
(My environment is Windows 11 with openvino_2021.4.752 version.)
Here comes my 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))
The OpenVINO setupvars.bat below
@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
First and foremost, please take a note that Windows 11 is not officially supported by OpenVINO. Hence, issues are expected. The same goes for PyQt5 integration with OpenVINO packages.
You could automate the OpenVINO setupvars.bat initialization using batch file creation in Windows.
Create a batch file that initializes OpenVINO environment and launch the software that you want (eg: VS2019)
Write the script below in the .bat file:
@echo on
cd \bin
CALL setupvars.bat
cd \2019\Community\Common7\IDE
CALL devenv.exe
Run the .bat file as administrator
Since the integration of OpenVINO and PyQt5 is not officially supported, there's no specific documentation to be followed as a guide.
On another note, a similar kind of integration has been implemented in openvino-smart-library. You may refer to this repository for reference/workaround.
In addition to that, you'll need to integrate the OpenVINO Inference Engine into your application (only C++ or Python is supported for this purpose). You may refer to this Integrate Inference Engine with Your Python Application.