Skip to main content

Example: OpenCV

Product: CODESYS Development System

This example shows a Python implementation of opencv, a free library of programming functions. With this library, an image is evaluated and the information is transferred to CODESYS via shared memory.

Description

In this example, an image is evaluated using Python and opencv, the free library of programming functions. There are different colored circles in the image. The blue circles are filtered and the coordinates of the contours are written to a string. The string is combined with a string of the current timestamp. This string is transferred to a byte array and stored in the shared memory.

The shared memory is open for a specific time (in this case, 60 seconds) to allow CODESYS to read the data. In CODESYS, the shared memory buffer is read and the byte array is transferred back to a string.

After passing to the string, the coordinates of four contours and the timestamp are in the string. Another prepared option is the evaluation of a video stream. In this case, the contours are limited to five in order to avoid an overflow.

Additional information

Preparations:

  • In CODESYS, open the OpenCV_Example2.project project.

  • Install Python 3.8 or higher.

  • Install opencv: command line: pip install opencv-python

Folder location:

  • The Python project and the sample image should be in the same folder.

  • Name the shared memory in CODESYS the same as in Python (default: MySharedMemory).

  • Set the same size for the shared memory in CODESYS as well as in Python (default value: 100).

  • In the Python project, set the time for opening the shared memory (default: 60 seconds).

Procedure:

  • Start CODESYS and connect to the local CODESYS Control Win controller. Log in and start the controller.

  • In monitoring, the string variable PLC_PRG.strRead shows the value "".

  • Run the Python script -> In monitoring, the string shows four coordinates and a timestamp.

Other functionalities:

  • You can read about the main features of opencv on their website (https://opencv.org/ ).

  • For more functions, the buffer of the shared memory may have to be increased.

System requirements and restrictions

Programming system

CODESYS Development System (version 3.5.16.0 or higher)

Runtime system

CODESYS Control Win (version 3.5.16.0)

Add-on components

-

Note

_example_icon.png DOWNLOAD Project