Sven Kreiss, 2019
Programmatically use OpenPifPaf to run multi-person pose estimation on an image.
# Uncomment when using on Google Colab:
# !pip install --upgrade openpifpaf==0.11.1
%matplotlib inline
import io
import numpy as np
import openpifpaf
import PIL
import requests
import torch
device = torch.device('cpu')
# device = torch.device('cuda') # if cuda is available
print(openpifpaf.__version__)
print(torch.__version__)
0.12b2+5.g5d9a208.dirty 1.7.1
Image credit: "Learning to surf" by fotologic which is licensed under CC-BY-2.0.
image_response = requests.get('https://raw.githubusercontent.com/vita-epfl/openpifpaf/main/docs/coco/000000081988.jpg')
pil_im = PIL.Image.open(io.BytesIO(image_response.content)).convert('RGB')
im = np.asarray(pil_im)
with openpifpaf.show.image_canvas(im) as ax:
pass
net_cpu, _ = openpifpaf.network.Factory(checkpoint='shufflenetv2k16').factory()
net = net_cpu.to(device)
openpifpaf.decoder.utils.CifSeeds.threshold = 0.5
openpifpaf.decoder.utils.nms.Keypoints.keypoint_threshold = 0.2
openpifpaf.decoder.utils.nms.Keypoints.instance_threshold = 0.2
processor = openpifpaf.decoder.factory([hn.meta for hn in net.head_nets])
data = openpifpaf.datasets.PilImageList([pil_im])
loader = torch.utils.data.DataLoader(
data, batch_size=1, pin_memory=True,
collate_fn=openpifpaf.datasets.collate_images_anns_meta)
openpifpaf.show.Canvas.show = True
annotation_painter = openpifpaf.show.AnnotationPainter()
for images_batch, _, __ in loader:
predictions = processor.batch(net, images_batch, device=device)[0]
with openpifpaf.show.image_canvas(im) as ax:
annotation_painter.annotations(ax, predictions)