Live 3D Human Pose Estimation with OpenVINO#
This Jupyter notebook can be launched on-line, opening an interactive environment in a browser window. You can also make a local installation. Choose one of the following options:
This notebook demonstrates live 3D Human Pose Estimation with OpenVINO via a webcam. We utilize the model human-pose-estimation-3d-0001 from Open Model Zoo. At the end of this notebook, you will see live inference results from your webcam (if available). Alternatively, you can also upload a video file to test out the algorithms. Make sure you have properly installed theJupyter extensionand been using JupyterLab to run the demo as suggested in the ``README.md``
NOTE: To use a webcam, you must run this Jupyter notebook on a computer with a webcam. If you run on a remote server, the webcam will not work. However, you can still do inference on a video file in the final step. This demo utilizes the Python interface in ``Three.js`` integrated with WebGL to process data from the model inference. These results are processed and displayed in the notebook.
To ensure that the results are displayed correctly, run the code in a recommended browser on one of the following operating systems: Ubuntu, Windows: Chrome macOS: Safari
Table of contents:
Installation Instructions#
This is a self-contained example that relies solely on its own code.
We recommend running the notebook in a virtual environment. You only need a Jupyter server to start. For details, please refer to Installation Guide.
Make sure your Jupyter extension is working properly. To avoid errors that may arise from the version of the dependency package, it is recommended to use the JupyterLab instead of the Jupyter notebook to display image results.
- pip install --upgrade pip && pip install -r requirements.txt
- jupyter labextension install --no-build @jupyter-widgets/jupyterlab-manager
- jupyter labextension install --no-build jupyter-datawidgets/extension
- jupyter labextension install jupyter-threejs
- jupyter labextension list
You should see:
JupyterLab v...
...
jupyterlab-datawidgets v... enabled OK
@jupyter-widgets/jupyterlab-manager v... enabled OK
jupyter-threejs v... enabled OK
Prerequisites#
The ``pythreejs`` extension may not display properly when using a Jupyter Notebook release. Therefore, it is recommended to use Jupyter Lab instead.
import platform
if platform.system() == "Darwin":
%pip install -q "numpy<2.0.0"
%pip install pythreejs "openvino>=2024.4.0" "opencv-python" "torch" "tqdm" --extra-index-url https://download.pytorch.org/whl/cpu
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cpu
Collecting pythreejs
Using cached pythreejs-2.4.2-py3-none-any.whl.metadata (5.4 kB)
Collecting openvino>=2024.4.0
Using cached openvino-2024.4.0-16579-cp38-cp38-manylinux2014_x86_64.whl.metadata (8.3 kB)
Collecting opencv-python
Using cached opencv_python-4.10.0.84-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (20 kB)
Collecting torch
Using cached https://download.pytorch.org/whl/cpu/torch-2.4.1%2Bcpu-cp38-cp38-linux_x86_64.whl (194.9 MB)
Collecting tqdm
Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)
Requirement already satisfied: ipywidgets>=7.2.1 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from pythreejs) (8.1.5)
Collecting ipydatawidgets>=1.1.1 (from pythreejs)
Using cached ipydatawidgets-4.3.5-py2.py3-none-any.whl.metadata (1.4 kB)
Collecting numpy (from pythreejs)
Using cached numpy-1.24.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (5.6 kB)
Requirement already satisfied: traitlets in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from pythreejs) (5.14.3)
Collecting openvino-telemetry>=2023.2.1 (from openvino>=2024.4.0)
Using cached openvino_telemetry-2024.5.0-py3-none-any.whl.metadata (2.3 kB)
Requirement already satisfied: packaging in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from openvino>=2024.4.0) (24.2)
Collecting filelock (from torch)
Using cached filelock-3.16.1-py3-none-any.whl.metadata (2.9 kB)
Requirement already satisfied: typing-extensions>=4.8.0 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from torch) (4.12.2)
Collecting sympy (from torch)
Using cached sympy-1.13.3-py3-none-any.whl.metadata (12 kB)
Collecting networkx (from torch)
Using cached https://download.pytorch.org/whl/networkx-3.2.1-py3-none-any.whl (1.6 MB)
Requirement already satisfied: jinja2 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from torch) (3.1.4)
Collecting fsspec (from torch)
Using cached fsspec-2024.10.0-py3-none-any.whl.metadata (11 kB)
Collecting traittypes>=0.2.0 (from ipydatawidgets>=1.1.1->pythreejs)
Using cached traittypes-0.2.1-py2.py3-none-any.whl.metadata (1.0 kB)
Requirement already satisfied: comm>=0.1.3 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipywidgets>=7.2.1->pythreejs) (0.2.2)
Requirement already satisfied: ipython>=6.1.0 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipywidgets>=7.2.1->pythreejs) (8.12.3)
Requirement already satisfied: widgetsnbextension~=4.0.12 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipywidgets>=7.2.1->pythreejs) (4.0.13)
Requirement already satisfied: jupyterlab-widgets~=3.0.12 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipywidgets>=7.2.1->pythreejs) (3.0.13)
Requirement already satisfied: MarkupSafe>=2.0 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from jinja2->torch) (2.1.5)
INFO: pip is looking at multiple versions of networkx to determine which version is compatible with other requirements. This could take a while.
Collecting networkx (from torch)
Using cached networkx-3.1-py3-none-any.whl.metadata (5.3 kB)
Collecting mpmath<1.4,>=1.1.0 (from sympy->torch)
Using cached https://download.pytorch.org/whl/mpmath-1.3.0-py3-none-any.whl (536 kB)
Requirement already satisfied: backcall in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.2.0)
Requirement already satisfied: decorator in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (5.1.1)
Requirement already satisfied: jedi>=0.16 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.19.2)
Requirement already satisfied: matplotlib-inline in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.1.7)
Requirement already satisfied: pickleshare in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.7.5)
Requirement already satisfied: prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (3.0.48)
Requirement already satisfied: pygments>=2.4.0 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (2.18.0)
Requirement already satisfied: stack-data in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.6.3)
Requirement already satisfied: pexpect>4.3 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (4.9.0)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.8.4)
Requirement already satisfied: ptyprocess>=0.5 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.7.0)
Requirement already satisfied: wcwidth in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30->ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.2.13)
Requirement already satisfied: executing>=1.2.0 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from stack-data->ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (2.1.0)
Requirement already satisfied: asttokens>=2.1.0 in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from stack-data->ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (3.0.0)
Requirement already satisfied: pure-eval in /opt/home/k8sworker/ci-ai/cibuilds/jobs/ov-notebook/jobs/OVNotebookOps/builds/835/archive/.workspace/scm/ov-notebook/.venv/lib/python3.8/site-packages (from stack-data->ipython>=6.1.0->ipywidgets>=7.2.1->pythreejs) (0.2.3)
Using cached pythreejs-2.4.2-py3-none-any.whl (3.4 MB)
Using cached openvino-2024.4.0-16579-cp38-cp38-manylinux2014_x86_64.whl (42.6 MB)
Using cached opencv_python-4.10.0.84-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.5 MB)
Using cached tqdm-4.67.1-py3-none-any.whl (78 kB)
Using cached ipydatawidgets-4.3.5-py2.py3-none-any.whl (271 kB)
Using cached numpy-1.24.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
Using cached openvino_telemetry-2024.5.0-py3-none-any.whl (23 kB)
Using cached filelock-3.16.1-py3-none-any.whl (16 kB)
Using cached fsspec-2024.10.0-py3-none-any.whl (179 kB)
Using cached networkx-3.1-py3-none-any.whl (2.1 MB)
Using cached sympy-1.13.3-py3-none-any.whl (6.2 MB)
Using cached traittypes-0.2.1-py2.py3-none-any.whl (8.6 kB)
Installing collected packages: openvino-telemetry, mpmath, traittypes, tqdm, sympy, numpy, networkx, fsspec, filelock, torch, openvino, opencv-python, ipydatawidgets, pythreejs
Successfully installed filelock-3.16.1 fsspec-2024.10.0 ipydatawidgets-4.3.5 mpmath-1.3.0 networkx-3.1 numpy-1.24.4 opencv-python-4.10.0.84 openvino-2024.4.0 openvino-telemetry-2024.5.0 pythreejs-2.4.2 sympy-1.13.3 torch-2.4.1+cpu tqdm-4.67.1 traittypes-0.2.1
Note: you may need to restart the kernel to use updated packages.
Imports#
import collections
import time
from pathlib import Path
import cv2
import ipywidgets as widgets
import numpy as np
from IPython.display import clear_output, display
import openvino as ov
# Fetch `notebook_utils` module
import requests
if not Path("notebook_utils.py").exists():
r = requests.get(
url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/notebook_utils.py",
)
with open("notebook_utils.py", "w") as f:
f.write(r.text)
if not Path("engine3js.py").exists():
r = requests.get(
url="https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/utils/engine3js.py",
)
with open("engine3js.py", "w") as f:
f.write(r.text)
import notebook_utils as utils
import engine3js as engine
The model#
Download the model#
from notebook_utils import download_file
import tarfile
# directory where model will be downloaded
base_model_dir = Path("model")
if not base_model_dir.exists():
download_file(
"https://storage.openvinotoolkit.org/repositories/open_model_zoo/public/2022.1/human-pose-estimation-3d-0001/human-pose-estimation-3d.tar.gz",
directory=base_model_dir,
)
ckpt_file = base_model_dir / "human-pose-estimation-3d-0001.pth"
if not ckpt_file.exists():
with tarfile.open(base_model_dir / "human-pose-estimation-3d.tar.gz") as f:
f.extractall(base_model_dir)
human-pose-estimation-3d.tar.gz: 0%| | 0.00/17.6M [00:00<?, ?B/s]
Convert Model to OpenVINO IR format#
import torch
ov_model_path = Path(base_model_dir) / "human-pose-estimation-3d-0001.xml"
if not ov_model_path.exists():
from model.model import PoseEstimationWithMobileNet
pose_estimation_model = PoseEstimationWithMobileNet(is_convertible_by_mo=True)
pose_estimation_model.load_state_dict(torch.load(ckpt_file, map_location="cpu"))
pose_estimation_model.eval()
with torch.no_grad():
ov_model = ov.convert_model(pose_estimation_model, example_input=torch.zeros([1, 3, 256, 448]), input=[1, 3, 256, 448])
ov.save_model(ov_model, ov_model_path)
/tmp/ipykernel_2156338/2723667668.py:9: FutureWarning: You are using torch.load with weights_only=False (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See pytorch/pytorch for more details). In a future release, the default value for weights_only will be flipped to True. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via torch.serialization.add_safe_globals. We recommend you start setting weights_only=True for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. pose_estimation_model.load_state_dict(torch.load(ckpt_file, map_location="cpu"))
Select inference device#
select device from dropdown list for running inference using OpenVINO
device = utils.device_widget()
device
Dropdown(description='Device:', index=1, options=('CPU', 'AUTO'), value='AUTO')
Load the model#
Converted models are located in a fixed structure, which indicates vendor, model name and precision.
First, initialize the inference engine, OpenVINO Runtime. Then, read the
network architecture and model weights from the .bin
and .xml
files to compile for the desired device. An inference request is then
created to infer the compiled model.
# initialize inference engine
core = ov.Core()
# read the network and corresponding weights from file
model = core.read_model(ov_model_path)
# load the model on the specified device
compiled_model = core.compile_model(model=model, device_name=device.value)
Processing#
Model Inference#
Frames captured from video files or the live webcam are used as the input for the 3D model. This is how you obtain the output heat maps, PAF (part affinity fields) and features.
def model_infer(scaled_img, stride):
"""
Run model inference on the input image
Parameters:
scaled_img: resized image according to the input size of the model
stride: int, the stride of the window
"""
# Remove excess space from the picture
img = scaled_img[
0 : scaled_img.shape[0] - (scaled_img.shape[0] % stride),
0 : scaled_img.shape[1] - (scaled_img.shape[1] % stride),
]
mean_value = 128.0
scale_value = 255.0
img = (img - mean_value) / scale_value
img = np.transpose(img, (2, 0, 1))[None,]
result = compiled_model(img)
# Get the results
results = (result[0][0], result[1][0], result[2][0])
return results
Draw 2D Pose Overlays#
We need to define some connections between the joints in advance, so that we can draw the structure of the human body in the resulting image after obtaining the inference results. Joints are drawn as circles and limbs are drawn as lines. The code is based on the 3D Human Pose Estimation Demo from Open Model Zoo.
# 3D edge index array
body_edges = np.array(
[
[0, 1],
[0, 9],
[9, 10],
[10, 11], # neck - r_shoulder - r_elbow - r_wrist
[0, 3],
[3, 4],
[4, 5], # neck - l_shoulder - l_elbow - l_wrist
[1, 15],
[15, 16], # nose - l_eye - l_ear
[1, 17],
[17, 18], # nose - r_eye - r_ear
[0, 6],
[6, 7],
[7, 8], # neck - l_hip - l_knee - l_ankle
[0, 12],
[12, 13],
[13, 14], # neck - r_hip - r_knee - r_ankle
]
)
body_edges_2d = np.array(
[
[0, 1], # neck - nose
[1, 16],
[16, 18], # nose - l_eye - l_ear
[1, 15],
[15, 17], # nose - r_eye - r_ear
[0, 3],
[3, 4],
[4, 5], # neck - l_shoulder - l_elbow - l_wrist
[0, 9],
[9, 10],
[10, 11], # neck - r_shoulder - r_elbow - r_wrist
[0, 6],
[6, 7],
[7, 8], # neck - l_hip - l_knee - l_ankle
[0, 12],
[12, 13],
[13, 14], # neck - r_hip - r_knee - r_ankle
]
)
def draw_poses(frame, poses_2d, scaled_img, use_popup):
"""
Draw 2D pose overlays on the image to visualize estimated poses.
Joints are drawn as circles and limbs are drawn as lines.
:param frame: the input image
:param poses_2d: array of human joint pairs
"""
for pose in poses_2d:
pose = np.array(pose[0:-1]).reshape((-1, 3)).transpose()
was_found = pose[2] > 0
pose[0], pose[1] = (
pose[0] * frame.shape[1] / scaled_img.shape[1],
pose[1] * frame.shape[0] / scaled_img.shape[0],
)
# Draw joints.
for edge in body_edges_2d:
if was_found[edge[0]] and was_found[edge[1]]:
cv2.line(
frame,
tuple(pose[0:2, edge[0]].astype(np.int32)),
tuple(pose[0:2, edge[1]].astype(np.int32)),
(255, 255, 0),
4,
cv2.LINE_AA,
)
# Draw limbs.
for kpt_id in range(pose.shape[1]):
if pose[2, kpt_id] != -1:
cv2.circle(
frame,
tuple(pose[0:2, kpt_id].astype(np.int32)),
3,
(0, 255, 255),
-1,
cv2.LINE_AA,
)
return frame
Main Processing Function#
Run 3D pose estimation on the specified source. It could be either a webcam feed or a video file.
def run_pose_estimation(source=0, flip=False, use_popup=False, skip_frames=0):
"""
2D image as input, using OpenVINO as inference backend,
get joints 3D coordinates, and draw 3D human skeleton in the scene
:param source: The webcam number to feed the video stream with primary webcam set to "0", or the video path.
:param flip: To be used by VideoPlayer function for flipping capture image.
:param use_popup: False for showing encoded frames over this notebook, True for creating a popup window.
:param skip_frames: Number of frames to skip at the beginning of the video.
"""
focal_length = -1 # default
stride = 8
player = None
skeleton_set = None
try:
# create video player to play with target fps video_path
# get the frame from camera
# You can skip first N frames to fast forward video. change 'skip_first_frames'
player = utils.VideoPlayer(source, flip=flip, fps=30, skip_first_frames=skip_frames)
# start capturing
player.start()
input_image = player.next()
# set the window size
resize_scale = 450 / input_image.shape[1]
windows_width = int(input_image.shape[1] * resize_scale)
windows_height = int(input_image.shape[0] * resize_scale)
# use visualization library
engine3D = engine.Engine3js(grid=True, axis=True, view_width=windows_width, view_height=windows_height)
if use_popup:
# display the 3D human pose in this notebook, and origin frame in popup window
display(engine3D.renderer)
title = "Press ESC to Exit"
cv2.namedWindow(title, cv2.WINDOW_KEEPRATIO | cv2.WINDOW_AUTOSIZE)
else:
# set the 2D image box, show both human pose and image in the notebook
imgbox = widgets.Image(format="jpg", height=windows_height, width=windows_width)
display(widgets.HBox([engine3D.renderer, imgbox]))
skeleton = engine.Skeleton(body_edges=body_edges)
processing_times = collections.deque()
while True:
# grab the frame
frame = player.next()
if frame is None:
print("Source ended")
break
# resize image and change dims to fit neural network input
# (see https://github.com/openvinotoolkit/open_model_zoo/tree/master/models/public/human-pose-estimation-3d-0001)
scaled_img = cv2.resize(frame, dsize=(model.inputs[0].shape[3], model.inputs[0].shape[2]))
if focal_length < 0: # Focal length is unknown
focal_length = np.float32(0.8 * scaled_img.shape[1])
# inference start
start_time = time.time()
# get results
inference_result = model_infer(scaled_img, stride)
# inference stop
stop_time = time.time()
processing_times.append(stop_time - start_time)
# Process the point to point coordinates of the data
poses_3d, poses_2d = engine.parse_poses(inference_result, 1, stride, focal_length, True)
# use processing times from last 200 frames
if len(processing_times) > 200:
processing_times.popleft()
processing_time = np.mean(processing_times) * 1000
fps = 1000 / processing_time
if len(poses_3d) > 0:
# From here, you can rotate the 3D point positions using the function "draw_poses",
# or you can directly make the correct mapping below to properly display the object image on the screen
poses_3d_copy = poses_3d.copy()
x = poses_3d_copy[:, 0::4]
y = poses_3d_copy[:, 1::4]
z = poses_3d_copy[:, 2::4]
poses_3d[:, 0::4], poses_3d[:, 1::4], poses_3d[:, 2::4] = (
-z + np.ones(poses_3d[:, 2::4].shape) * 200,
-y + np.ones(poses_3d[:, 2::4].shape) * 100,
-x,
)
poses_3d = poses_3d.reshape(poses_3d.shape[0], 19, -1)[:, :, 0:3]
people = skeleton(poses_3d=poses_3d)
try:
engine3D.scene_remove(skeleton_set)
except Exception:
pass
engine3D.scene_add(people)
skeleton_set = people
# draw 2D
frame = draw_poses(frame, poses_2d, scaled_img, use_popup)
else:
try:
engine3D.scene_remove(skeleton_set)
skeleton_set = None
except Exception:
pass
cv2.putText(
frame,
f"Inference time: {processing_time:.1f}ms ({fps:.1f} FPS)",
(10, 30),
cv2.FONT_HERSHEY_COMPLEX,
0.7,
(0, 0, 255),
1,
cv2.LINE_AA,
)
if use_popup:
cv2.imshow(title, frame)
key = cv2.waitKey(1)
# escape = 27, use ESC to exit
if key == 27:
break
else:
# encode numpy array to jpg
imgbox.value = cv2.imencode(
".jpg",
frame,
params=[cv2.IMWRITE_JPEG_QUALITY, 90],
)[1].tobytes()
engine3D.renderer.render(engine3D.scene, engine3D.cam)
except KeyboardInterrupt:
print("Interrupted")
except RuntimeError as e:
print(e)
finally:
clear_output()
if player is not None:
# stop capturing
player.stop()
if use_popup:
cv2.destroyAllWindows()
if skeleton_set:
engine3D.scene_remove(skeleton_set)
Run#
Run, using a webcam as the video input. By default, the primary webcam
is set with source=0
. If you have multiple webcams, each one will be
assigned a consecutive number starting at 0. Set flip=True
when
using a front-facing camera. Some web browsers, especially Mozilla
Firefox, may cause flickering. If you experience flickering, set
use_popup=True
.
NOTE:
1. To use this notebook with a webcam, you need to run the notebook on a computer with a webcam. If you run the notebook on a server (e.g. Binder), the webcam will not work.
2. Popup mode may not work if you run this notebook on a remote computer (e.g. Binder).
If you do not have a webcam, you can still run this demo with a video file. Any format supported by OpenCV will work.
Using the following method, you can click and move your mouse over the picture on the left to interact.
from notebook_utils import download_file
USE_WEBCAM = False
cam_id = 0
if not Path("face-demographics-walking.mp4").exists():
download_file(
"https://storage.openvinotoolkit.org/data/test_data/videos/face-demographics-walking.mp4",
)
video_path = "face-demographics-walking.mp4"
source = cam_id if USE_WEBCAM else video_path
run_pose_estimation(source=source, flip=isinstance(source, int), use_popup=False)