Skip to main content
Version: Torizon OS 6.x.y

Torizon Sample: Using OpenCV for Computer Vision

Introduction

Through Torizon, Toradex provides Debian Docker images with support for packages that greatly ease the development process for several embedded computing applications, including computer vision.

In this article, we will show how you can quickly build an application with OpenCV using Python for NXP's i.MX SoC, such as i.MX8, i.MX8X or i.MX8MM. For those who don't know, OpenCV is the most popular open-source computer vision library.

This article complies with the Typographic Conventions for Torizon Documentation.

Prerequisites

Hardware

  • An arm64v8 based SoM (Apalis iMX8, Colibri iMX8X, Verdin iMX8M Plus or Verdin iMX8M Mini)
  • A monitor or display connected to the SoM.

Software and Knowledge

info

Apalis iMX8X is phased out, and it is not available for purchase anymore. The latest supported BSP and TorizonCore version is 5.4.0.

About this Sample Project

This example uses the cv2 to access OpenCV libraries using Python3. It runs a slightly modified version of the sample extracted from the official OpenCV-Python tutorial.

In this project, we implemented the code in the main.py file. Keep this code in mind, we'll come back to it later. It loads the image from this file, which is locally stored and then exhibits it in a window in grayscale mode for 20 seconds:

main.py
import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('106926-verdin-imx8mm-front-view.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(20000) # Time in miliseconds
cv2.destroyAllWindows()

The OpenCV example loading an image in grayscale mode

You can modify the example for other color modes (like cv2.IMREAD_COLOR or cv2.IMREAD_UNCHANGED)

For the Impatient: Running the Sample Project in Torizon Without Building It

If you only want to see the sample project in action, in your board terminal, download the specific docker-compose file and run the containers:

# wget https://github.com/toradex/torizon-samples/raw/bookworm/opencv/docker-compose.yaml
# docker-compose -f docker-compose.yaml up

Modifying and Building the Project from Source

Getting the Source Code of the Torizon Samples

Now, going down into the details, we will explore the demonstration example available on the Toradex samples repository.

To obtain the files, clone the torizon-samples repository to your computer:

$ cd ~
$ git clone https://github.com/toradex/torizon-samples.git

Build the Sample Project

First, in your PC terminal, build the sample project:

info

Use your Dockerhub credentials

$ cd torizon-samples/opencv
$ docker build -t <your-dockerhub-username>/opencv-example .

After the build, push the image to your Dockerhub account:

$ docker push <your-dockerhub-username>/opencv-example

Modify the Docker-compose

After building the Dockerfile image above and pushing it to your Dockerhub, you need to edit the docker-compose file.

Edit the image field of the example with your image repository (replace the torizonextras username):

info

Use your Dockerhub credentials

docker-compose.yaml
  depends_on:
- weston
image: your-dockerhub-username/opencv-example
volumes:

After editing, save and send this file to your module using scp:

$ scp docker-compose.yaml torizon@<your-ip>:/home/torizon

Run the Sample Project

Now enter your module's terminal using SSH:

$ ssh torizon@<target-ip>
info

For more information about SSH, please refer to SSH on Linux.

Now you can launch the sample application by using the command:

# docker-compose -f docker-compose.yaml up

Implementation Details

The Docker Compose (yaml) file

This file configures the application's services. It informs the Docker runtime which containers the system will run, set privileges, among other options.

This example will run a Python3 script that uses the imshow method from OpenCV to display a picture. This method uses Wayland and Weston as a backend.

Therefore, this docker-compose file will start two containers:

  • One with the Weston image (Wayland compositor).
  • One with the application image (Wayland client). This container is the one that will execute our Python Script with the OpenCV library.

Both containers will communicate through shared folders by bind mounting.

Toradex supports Wayland protocol through its Debian Containers for Torizon.

The Dockerfile

In this section, you will go through some relevant snippets containing information about the Dockerfile, which describes the container itself.

Toradex Debian image - Wayland

Toradex provides a basic Wayland image in its Dockerhub page. To use with an arm64v8 computer-on-module (COM), add torizon/wayland-base-vivante to your image.

FROM torizon/wayland-base-vivante:2
tip

To find out the right tag for your Torizon OS version, read the article Torizon OS Containers Tags and Versioning.

OpenCV libraries

The following Dockerfile command line shows an example on how to setup the required package to install the OpenCV library:


###### INSTALL OPENCV

RUN apt-get update && apt-get install -y python3-opencv

The last line of the dockerfile execute the Python3 script as entrypoint:

ENTRYPOINT python3 opencv-example.py

Additional Considerations

  • You can use the cv2.VideoCapture function from cv2 to capture frames from video inputs, such as an external camera connected to the MIPI interface or even USB.
  • OpenCV provides modules for Machine Learning and Neural Networks. These modules target Arm Cortex-A cores and utilize Arm Neon to process models. However, until this moment, these modules are not accelerated by GPU. You can use other inference engines in conjunction with OpenCV to increase AI processing performance. See other AI solutions that Toradex offers through it network of partners.
  • if you need OpenCV for Armv7 (32-bit), please contact us.


Send Feedback!