|
| 1 | +""" |
| 2 | +Just a sample code that demonstrate inference using OpenVino |
| 3 | +""" |
| 4 | + |
| 5 | +import openvino as ov |
| 6 | +import cv2 |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +## download models |
| 10 | +# curl --create-dirs https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/face-detection-retail-0004/FP32/face-detection-retail-0004.bin -o model/1/face-detection-retail-0004.bin |
| 11 | +# curl --create-dirs https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/face-detection-retail-0004/FP32/face-detection-retail-0004.xml -o model/1/face-detection-retail-0004.xml |
| 12 | + |
| 13 | +core = ov.Core() |
| 14 | + |
| 15 | +compiled_model = core.compile_model(r"model\1\face-detection-retail-0004.xml", "AUTO") |
| 16 | + |
| 17 | +infer_request = compiled_model.create_infer_request() |
| 18 | + |
| 19 | +print(infer_request.get_input_tensor()) |
| 20 | +print(infer_request.get_output_tensor()) |
| 21 | + |
| 22 | + |
| 23 | +## read image and preprocessing |
| 24 | +# Read image |
| 25 | +img = cv2.imread(r"bin\face.png") |
| 26 | +# Resize image |
| 27 | +img = cv2.resize(img, (300, 300)) |
| 28 | +# Convert to float32 |
| 29 | +img = img.astype(np.float32) |
| 30 | +# Normalize pixel values to [0, 1] |
| 31 | +img = img / 255.0 |
| 32 | +# Convert to numpy array |
| 33 | +img = np.array(img) |
| 34 | +img = img.reshape(3, 300, 300) |
| 35 | +img = np.expand_dims(img, axis=0) |
| 36 | + |
| 37 | + |
| 38 | +## Inference |
| 39 | +# Create tensor from external memory |
| 40 | +input_tensor = ov.Tensor(array=img, shared_memory=True) |
| 41 | +# Set input tensor for model with one input |
| 42 | +infer_request.set_input_tensor(input_tensor) |
| 43 | +# run inference |
| 44 | +infer_request.start_async() |
| 45 | +infer_request.wait() |
| 46 | + |
| 47 | + |
| 48 | +## output post processing |
| 49 | +# get output |
| 50 | +# Get output tensor for model with one output |
| 51 | +output = infer_request.get_output_tensor() |
| 52 | +output_buffer = output.data |
| 53 | +print(output_buffer.shape) |
0 commit comments