Intel® Distribution of OpenVINO™ Toolkit
Community assistance about the Intel® Distribution of OpenVINO™ toolkit, OpenCV, and all aspects of computer vision-related on Intel® platforms.

OpenVINO - Python Script in Visual Studio 2017 To run

H__anja
Beginner
1,649 Views

I'm trying to run a Pyhton script in Visual Studio, but unfortunately I can't load the library

import os
assert 'computer_vision_sdk' in os.environ ['PYTHONPATH']

from PIL import Image
import numpy as np
try:
    from openvino import inference_engine as ie
    from openvino.inference_engine import IENetwork, IEPlugin
except exception as e:
    exception_type = type (e) .__ name__
    print ("The following error happened while importing Python API module: \ n [{}] {}". format (exception_type, e))
    sys.exit (1)

the first mistake comes with the

assert 'computer_vision_sdk' in os.environ ['PYTHONPATH']

I ran

setupvars.bat

Python 3.7.3
ECHO is switched OFF.
PYTHONPATH = C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ open_model_zoo \ tools \ accuracy_checker; C: \ Program Files (x86) \ IntelSWTools \ openvino \ python \ python3.7; C: \ Program Files (x86) \ IntelSWT
ols \ openvino \ python \ python3; C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ model_optimizer; C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ open_model_zoo \ tools \ accuracy_checke
; C: \ Program Files (x86) \ IntelSWTools \ openvino \ python \ python3.7; C: \ Program Files (x86) \ IntelSWTools \ openvino \ python \ python3; C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ model_optimizer; C:
rogram Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ open_model_zoo \ tools \ accuracy_checker; C: \ Program Files (x86) \ IntelSWTools \ openvino \ python \ python3.7; C: \ Program Files (x86) \ IntelSWTools \ openvino \ p
thon \ python3; C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ model_optimizer;

[setupvars.bat] OpenVINO environment initialized

 

since I do not run the script in the same window as the Bat file, I also have the

PYTHONPATH = C: \ Program Files (x86) \ IntelSWTools \ openvino \ python \ python3.7

set as a system variable. The Script runs in Visual Studio.

 

update: 

In Visual Studio 2017, I have now also set the debug environment variables in the project

PATH = C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ inference_engine \ bin \ intel64 \ Debug; C: \ Program Files (x86) \ IntelSWTools \ openvino \ opencv \ bin;% PATH%

unfortunately I still cannot run the script.

0 Kudos
1 Solution
SIRIGIRI_V_Intel
Employee
1,649 Views

Hi Anja,

Make sure that you select the version that you have specified in the environment variables and the version used in visual studio are same. Attached the document(Environment_variables.txt) which contains the environment variables that I have used. Also attached the screenshots that might help you.

Please verify once.

If you still have error, please verify the OpenVINO installation using the verification scripts.

Hope this helps.

Regards,

Ram prasad

View solution in original post

0 Kudos
11 Replies
H__anja
Beginner
1,649 Views

hat niemand eine Idee???

0 Kudos
SIRIGIRI_V_Intel
Employee
1,649 Views

Hi Anja,

As you are using the openvino toolkit, it is installed with the name openvino_<version>.

So modify

assert ‘computer_vision_sdk’ in os.environ ['PYTHONPATH']  

to

assert ‘openvino’ in os.environ ['PYTHONPATH']

Feel free to ask any other questions.

Regards,

Ram prasad

0 Kudos
H__anja
Beginner
1,649 Views

Hi, nice that you help me ;-)

 

I had the latest openvino toolkit installed. I tried it like this:

os.putenv("PYTHONPATH", "C:\Program Files (x86)\IntelSWTools\openvino\python\python3.6")

now i get an error, if i call

from openvino import inference_engine as ie

Error when loading

__init__py from .ie_api import *

I get the following error

"main" load
"openvino" load
"openvino.inference_engine" load
The following error happened while importing Python API module:
[ ImportError ] DLL load failed: Das angegebene Modul wurde nicht gefunden.

Note: I run the python script in Visual Studio 2017 and not in a DOS window! 

0 Kudos
SIRIGIRI_V_Intel
Employee
1,649 Views

Have you set the environment variables before running the application? If not, please set the environment variables in visual studio.

Or, you can run the setupvars.bat in cmd and open the visual studio from the cmd.

Regards,

Ram prasad

0 Kudos
H__anja
Beginner
1,649 Views

Hi,

Environment variable in visual studio was set: PYTHONPATH = C: \ Program Files (x86) \ IntelSWTools \ openvino

I tried your tip.

Executed in the cmd window run setupvars.bat file.

Info: C: \ Program Files (x86) \ IntelSWTools \ openvino \ bin> setupvars.bat

The "where" command is either misspelled or could not be found. Warning: libmmd.dll couldn't be found in% PATH%. Please check if the redistributable package for Intel (R) C ++ Compiler is installed and the library path is added to the PATH environment variable. System r boot can be required to update the system environment. Python 3.7.3 ECHO is switched OFF. PYTHONPATH = C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ open_model_zoo \ tools \ accuracy_checker; C: \ Program Files (x86) \ IntelSWTools \ openvino \ python \ python3.7; C: \ Program Files (x86) \ IntelSWT ols \ openvino \ python \ python3; C: \ Program Files (x86) \ IntelSWTools \ openvino \ deployment_tools \ model_optimizer; C: \ Program Files (x86) \ IntelSWTools \ openvino \ python \ python3.7 [setupvars.bat] OpenVINO environment initialized

Then open the visual studio in the same window and start the script.

Error message: "__main__" loaded "openvino" loaded "openvino.inference_engine" loaded The following error happened while importing Python API module: [ImportError] DLL load failed: The specified module was not found.

 

The question is, which dll is not found?

0 Kudos
SIRIGIRI_V_Intel
Employee
1,649 Views

Hi Anja,

Set all the below OpenVINO variables as environment variables in visual studio to run the application.

arch_root_dir                                          C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\inference_engine\bin\intel64\arch_descriptions

hddl_install_dir                                       C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\inference_engine\external\hddl

inferenceengine_dir                               C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\inference_engine\share

intel_cvsdk_dir                                        C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033

intel_dev_redist                                      C:\Program Files (x86)\Common Files\Intel\Shared Libraries\

intel_openvino_dir                                 C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033

opencv_dir                                               C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\opencv\cmake

openvx_folder                                         C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\openvx

path                                                           C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\ngraph\lib;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\inference_engine\external\tbb\bin;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\inference_engine\bin\intel64\Release;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\inference_engine\bin\intel64\Debug;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\inference_engine\external\hddl\bin;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\model_optimizer;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\openvx\bin;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\opencv\bin;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64_win\compiler;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files\CMake\bin;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\python\python3.6\;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\python\python3.6;C:\Users\sirigi1x\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\sirigi1x\AppData\Local\Programs\Python\Python36\;C:\Users\sirigi1x\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\;C:\Users\sirigi1x\Documents\;

python_ver                                              okay

pythonpath                                              C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\open_model_zoo\tools\accuracy_checker;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\python\python3.6;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\python\python3;C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033\deployment_tools\model_optimizer;

root                                                           C:\Program Files (x86)\IntelSWTools\openvino_2020.1.033

script_name                                             setupvars.bat

 

Feel free to edit the path as per your system needs.

Hope this helps.

Regards,

Ram prasad

0 Kudos
H__anja
Beginner
1,649 Views

 

Hi Ram,

I tried my best and entered all variables in Visual Studio 2017

  • Bat file started
  • Visual Studio started via cmd
  • Script started

but unfortunately always the same mistake. I'm done normally I have no problems with the Python scripts ..... another idea?

best regards Anja

 

0 Kudos
SIRIGIRI_V_Intel
Employee
1,650 Views

Hi Anja,

Make sure that you select the version that you have specified in the environment variables and the version used in visual studio are same. Attached the document(Environment_variables.txt) which contains the environment variables that I have used. Also attached the screenshots that might help you.

Please verify once.

If you still have error, please verify the OpenVINO installation using the verification scripts.

Hope this helps.

Regards,

Ram prasad

0 Kudos
H__anja
Beginner
1,649 Views

Hi Ram,

Thanks for the fantastic help!

The import works now :-)

I don't dare to ask if you could give me a tip for my next problem?

In the example script an error occurs when calling:

net = IENetwork.from_ir (model = model_xml, weights = model_bin)

Error:
 DeprecationWarning: from_ir () method of IENetwork is deprecated. Please use IENetwork class constructor to create valid IENetwork instance

Example script:

import os
import sys
import numpy as np

assert 'openvino' in os.environ ['PYTHONPATH']
try:
    from openvino import inference_engine  as ie
    from openvino.inference_engine import IENetwork, IEPlugin
    print("Import war erfolgreich")
   
except Exception as e:
    exception_type = type(e).__name__

    print("The following error happened while importing Python API module:\n[ {} ] {}".format(exception_type, e))
    #sys.exit(1)

def pre_process_image(imagePath, img_height=224):
    # Model input format
    n, c, h, w = [1, 3, img_height, img_height]
    image = Image.open(imagePath)
    processedImg = image.resize((h, w), resample=Image.BILINEAR)

    # Normalize to keep data between 0 - 1
    processedImg = (np.array(processedImg) - 0) / 255.0

    # Change data layout from HWC to CHW
    processedImg = processedImg.transpose((2, 0, 1))
    processedImg = processedImg.reshape((n, c, h, w))

    return image, processedImg, imagePath

# Plugin initialization for specified device and load extensions library if specified.
plugin_dir = None
model_xml = "C:\\Anja_Programme\\Ehab\\PythonApplication1\\PythonApplication1\\model\\frozen_model.xml"
model_bin = "C:\\Anja_Programme\\Ehab\\PythonApplication1\\PythonApplication1\\model\\frozen_model.bin"
# Devices: GPU (intel), CPU, MYRIAD
plugin = IEPlugin("CPU", plugin_dirs=plugin_dir)
# Read IR
net = IENetwork.from_ir(model=model_xml, weights=model_bin)
assert len(net.inputs.keys()) == 1
assert len(net.outputs) == 1
input_blob = next(iter(net.inputs))
out_blob = next(iter(net.outputs))
# Load network to the plugin
exec_net = plugin.load(network=net)
del net

# Run inference
fileName = './data/elephant.jpg'
image, processedImg, imagePath = pre_process_image(fileName)
res = exec_net.infer(inputs={input_blob: processedImg})
# Access the results and get the index of the highest confidence score
output_node_name = list(res.keys())[0]
res = res[output_node_name]

# Predicted class index.
idx = np.argsort(res[0])[-1]

# decode the predictions
from tensorflow.keras.applications.inception_v3 import decode_predictions
print('Predicted:', decode_predictions(res, top=3)[0])

must be the function

net = IENetwork.from_ir ....

be called differently?

 

greetings Anja

0 Kudos
H__anja
Beginner
1,649 Views

Hi Ram,

I found the solution ....

net = IENetwork (model = model_xml, weights = model_bin)

I hope everything works now :-) and thank you very much for your help!

 

greetings Anja

0 Kudos
SIRIGIRI_V_Intel
Employee
1,649 Views

Thanks for letting us know.

Feel free to ask if you have any other questions.

Regards,

Ram prasad

0 Kudos
Reply