OpenCV#

OpenCV is a popular framework for image and video processing. On this tutorial, we show how OpenPifPaf can integrate with a workflow from OpenCV. OpenPifPaf also comes with a video tool for processing videos from files or usb cameras that is based on OpenCV, openpifpaf.video.

Source#

The cv2.VideoCapture class supports an enourmous amount of sources (files, cameras, rtmp, etc, …) and abstracts the details away. Here, we will just pass in a single image.

capture = cv2.VideoCapture('coco/000000081988.jpg')
_, image = capture.read()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
with openpifpaf.show.Canvas.image(image) as ax:
    pass
_images/cebd1f43780588c4159d19b68084bc4ee64349c25baf5a7d303a81bb8901b385.png

Prediction#

Now that we have the image, we can use the openpifpaf.Predictor and then visualize its predicted annotations with an AnnotationPainter:

predictor = openpifpaf.Predictor(checkpoint='shufflenetv2k16')
predictions, gt_anns, meta = predictor.numpy_image(image)

annotation_painter = openpifpaf.show.AnnotationPainter()
with openpifpaf.show.Canvas.image(image) as ax:
    annotation_painter.annotations(ax, predictions)
_images/c1712d44c4855755c16ecb93166709854f080c79558acec35d5cc264c25f61c3.png

This example is intentionally left to be quite basic. If you are interested to accelerate this process with a GPU or you have many images that should be pre-loaded in parallel, please have a look at the Prediction API or use the openpifpaf.video command line tool.