# OpenVINO Quick Get Started on Windows 10
###### tags: `OpenVINO` `2021.4` `Windows 10`
## 在 Windows 10 電腦上安裝 OpenVINO Toolkit 2021.4
- Table of Content
[ToC]
### :memo: 系統需求
硬體 :
Intel CPU 筆電/桌機/NUC 
軟體 :
Microsoft Windows 10
Intel Distribution OpenVINO Toolkit 2021.4
Python 3.7.9 64-bit

### 下載 Intel® Distribution of OpenVINO™ Toolkit
https://software.intel.com/en-us/openvino-toolkit/choose-download

### 安裝 Intel® Distribution of OpenVINO™ Toolkit




### 檢查裝置管理員中是否有CPU內建的Intel Graphics顯示卡以及驅動程式的版本是否為21.20以上

### 檢查NCS2 / Movidius Myriad X是否可被系統辨識 (新版的Windows 10已內建NCS2驅動程式)

### 下載並安裝Python 3.7.9
https://www.python.org/downloads/windows/



### 安裝OpenVINO toolkit所需的Python套件
```
"c:\Program Files (x86)\Intel\openvino_2021.4.582\bin\setupvars.bat"
cd "C:\ProgramProgram Files (x86)\intel\openvino_2021.4.582\deployment_tools\model_optimizer\install_prerequisites"
install_prerequisites_tf2.bat
install_prerequisites_onnx.bat
cd "c:\Program Files (x86)\intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\"
pip install -r requirements.in
```
### 安裝OpenVINO toolkit所需的Python套件 : 使用virtualenv (建議使用,非必須)
```
pip install virtualenv
python -m venv c:\Users\Public\ov_venv
c:\Users\Public\ov_venv\Scripts\activate
"c:\Program Files (x86)\Intel\openvino_2021.4.582\bin\setupvars.bat"
pip install -r "C:\Program Files (x86)\intel\openvino_2021.4.582\deployment_tools\model_optimizer\requirements_tf2.txt
pip install -r "C:\Program Files (x86)\intel\openvino_2021.4.582\deployment_tools\model_optimizer\requirements_onnx.txt
cd "c:\Program Files (x86)\intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\"
pip install -r requirements.in
````
### 使用OpenVINO Toolkit做影像辨識
使用神經網路模型執行影像辨識只要三個步驟
1. 取得神經網路模型
1. 對神經網路模型進行推論優化
1. 執行影像辨識推論 (在Intel CPU, Intel GPU以及VPU)
### 步驟1 取得神經網路模型

```
cd c:\Users\Public
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\downloader.py" --name mobilenet-v2-1.0-224
```
### 步驟2 對神經網路模型進行推論優化

```
cd c:\Users\Public
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\converter.py" --name mobilenet-v2-1.0-224
```
### 步驟3 執行影像辨識推論 (18 lines of Python code)

### *** car.png 可以在C:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\demo\找到.
```
cd c:\Users\Public
copy c:\Users\Public\public\mobilenet-v2-1.0-224\FP16\* .
copy "C:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\demo\car.png" .
notepad getstarted.py # Copy and paste python below to getstarted.py code
python getstarted.py
```
### *** getstarted.py python code
```python=
import cv2
import numpy as np
from openvino.inference_engine import IENetwork, IECore
ie = IECore()
net = ie.read_network(model='mobilenet-v2-1.0-224.xml')
input_blob = next(iter(net.inputs))
out_blob = next(iter(net.outputs))
batch,channel,height,width = net.inputs[input_blob].shape
image = cv2.imread('car.png')
cv2.imshow("input", image)
image = cv2.resize(image, (width, height))
image = image.transpose((2, 0, 1)) # Change data layout from HWC to CHW
exec_net = ie.load_network(network=net, device_name='CPU')
res = exec_net.infer(inputs={input_blob: image})
idx = np.argsort(np.squeeze(res[out_blob][0]))[::-1]
for i in range(5):
print(idx[i]+1, res[out_blob][0][idx[i]])
cv2.waitKey(3*1000)
```
### 挑戰題 : 如何在GPU上執行影像辨識? (Python)
```
exec_net = ie.load_network(network=net, device_name='GPU')
```
### 挑戰題 : 如何在NCS2上執行影像辨識? (Python)
```
exec_net = ie.load_network(network=net, device_name='MYRIAD')
```
### 挑戰題 :如何更改影像辨識模型
```
net = ie.read_network(model='googlenet-v3.xml')
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\downloader.py" --name googlenet-v3
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\converterer.py" --name googlenet-v3
```
### 挑戰題 : 如何使用USB WebCam執行影像物件偵測?
```
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\downloader.py" --name person-detection-0201
copy intel\person-detection-0201\FP16\* .
notepad objectdetect_webcam.py # Copy and paste python below to objectdetect_webcam.py code
python objectdetect_webcam.py
```
### *** objectdetect_webcam.py python code
```python=
import cv2
import numpy as np
from openvino.inference_engine import IENetwork, IECore
threshold = 0.4
ie = IECore()
net = ie.read_network(model='person-detection-0201.xml') # Object detection model
input_blob = next(iter(net.inputs))
out_blob = next(iter(net.outputs))
batch,channel,height,width = net.inputs[input_blob].shape
cap = cv2.VideoCapture(0) # USB WebCam
while(True):
ret, frame = cap.read()
image = cv2.resize(frame, (width, height))
image = image.transpose((2, 0, 1)) # Change data layout from HWC to CHW
exec_net = ie.load_network(network=net, device_name='CPU')
res = exec_net.infer(inputs={input_blob: image})
size = frame.shape[:2]
for detection in res['detection_out'][0][0]:
if detection[2] > threshold:
print("detection[2]:{}".format(detection[2]))
xmin = max(int(detection[3]*size[0]), 0)
ymin = max(int(detection[4]*size[1]), 0)
xmax = min(int(detection[5]*size[0]), size[0])
ymax = min(int(detection[6]*size[1]), size[1])
class_id = int(detection[1])
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2) # Draw rectangle
cv2.imshow("detection_output", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
### 挑戰題 :如何更改物件偵測模型
```
import cv2
import numpy as np
from openvino.inference_engine import IENetwork, IECore
threshold = 0.6
ie = IECore()
net = ie.read_network(model='ssd_mobilenet_v2_coco.xml’) # Object detection model
input_blob = next(iter(net.inputs))
out_blob = next(iter(net.outputs))
batch,channel,height,width = net.inputs[input_blob].shape
cap = cv2.VideoCapture(0) # USB WebCam
while(True):
ret, frame = cap.read()
image = cv2.resize(frame, (width, height))
image = image.transpose((2, 0, 1)) # Change data layout from HWC to CHW
exec_net = ie.load_network(network=net, device_name='CPU')
res = exec_net.infer(inputs={input_blob: image})
size = frame.shape[:2]
for detection in res['DetectionOutput'][0][0]:
if detection[2] > threshold:
xmin = max(int(detection[3]*size[0]), 0)
ymin = max(int(detection[4]*size[1]), 0)
xmax = min(int(detection[5]*size[0]), size[0])
ymax = min(int(detection[6]*size[1]), size[1])
class_id = int(detection[1])
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2) # Draw rectangle
cv2.imshow("detection_output", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
### 安裝Visual Studio Community 2019
(Accuracy Checker 以及 Post-Training Optimization Tool (POT) 會使用到VS 2019)
Download Visual Studio Community version, https://visualstudio.microsoft.com/zh-hant/vs/express/
只需安裝

### 編譯OpenVINO C++ Demo Code
```
"c:\Program Files (x86)\Intel\openvino_2021.4.582\bin\setupvars.bat"
c:\Users\Public\ov_venv\Scripts\activate (如果使用virtualenv才需要)
pip install cmake
cd "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\inference_engine\demos"
build_demos_msvc.bat VS2019
cd c:\Users\openvino\Documents\Intel\OpenVINO\omz_demos_build\intel64\Release
```
### 使用USB WebCam 進行年齡,性別與情緒辨識

```
cd c:\Users\Public
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\downloader.py" --name face-detection-adas-0001
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\downloader.py" --name age-gender-recognition-retail-0013
python "c:\Program Files (x86)\Intel\openvino_2021.4.582\deployment_tools\tools\model_downloader\downloader.py" --name emotions-recognition-retail-0003
cd c:\Users\openvino\Documents\Intel\OpenVINO\omz_demos_build\intel64\Release
```
### 挑戰題 : 如何把物件偵測模型跑在GPU上?
```
interactive_face_detection_demo.exe -i 0 -m c:\Users\Public\intel\face-detection-adas-0001\FP16\face-detection-adas-0001.xml -m_ag c:\Users\Public\intel\age-gender-recognition-retail-0013\FP16\age-gender-recognition-retail-0013.xml -m_em c:\Users\Public\intel\emotions-recognition-retail-0003\FP16\emotions-recognition-retail-0003.xml -d GPU
```
### 挑戰題 : 如何把物件偵測模型跑在NCS2上?
```
interactive_face_detection_demo.exe -i 0 -m c:\Users\Public\intel\face-detection-adas-0001\FP16\face-detection-adas-0001.xml -m_ag c:\Users\Public\intel\age-gender-recognition-retail-0013\FP16\age-gender-recognition-retail-0013.xml -m_em c:\Users\Public\intel\emotions-recognition-retail-0003\FP16\emotions-recognition-retail-0003.xml -d MYRIAD
```