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/tutorial_opencv_4_0.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/tutorial_opencv_6_0.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.