Skip to content

Commit 73b3336

Browse files
committed
feat: Implement basic face detection inference
Demonstrates OpenVINO inference on a sample image using the face-detection-retail-0004 model.
1 parent 715c14e commit 73b3336

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
src/__pycache__
2-
models
3-
main.log
2+
main.log
3+
bin/models
4+
5+
venv

requirements.txt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
image==1.5.27
2-
ipdb==0.12.3
3-
ipython==7.10.2
4-
numpy==1.17.4
5-
Pillow==6.2.1
6-
requests==2.22.0
7-
virtualenv==16.7.9
8-
pyyaml==5.3.1
9-
opencv-python==4.3.0.36
10-
PyAutoGUI==0.9.50
11-
python3-xlib==0.15
1+
openvino
2+
opencv-python
3+
black

sample inference.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)