CODESYS Raspberry PI - OpenCV on current Raspbian

This is an attempt to get the CODESYS OpenCV Raspberry Package running on the current (or newer) Images of Raspbian.
As CODESYS opencv package for Raspberry PI was designed for older Raspbian versions, there are dependencies in the opencv Package, that cannot easily resolved on the newest Raspbian versions.

I tried something and found out that it might work like this:

Download and transfer the included debian package (*.deb) to your raspberry (as described in the package). But dont install it.
This will cause some dependency errors, as the "old" opencv packages in version 2.4.9 are not available anymore in current rasbian.

  • now connect to your raspi via SSH (e.g. putty or whatever program you like)

  • now modify the dependencies of this package:
    you can do that easily by yourself, or with the attached script "fix_cds_opencv_deps.sh":
    what it basically does: use the .deb file, unpack it, change the dependencies to opencv, and then repack it again to a "no_deps" .deb

this is my script "fix_cds_opencv_deps.sh":

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash

# this script can be used to fix and remove the opencv lib dependencies of 
# the codesys opencv package for raspberry pi
#

# first argument can be the full path to the opencv debian package
# if no argument is given, we try to find it in the home directory of the user

opencv_pkg=$1

if [ -z "$opencv_pkg" ];then
    echo "try to use local file";
    opencv_pkg=$(find ~ -name "codesys_raspi_opencv_V3.5.*" | tail -n 1)

fi

if [ ! -f "$opencv_pkg" ];then
    echo "package file not found. please pass the codesys opencv deb file as argument";
    exit -1;

fi 

newpkg=${opencv_pkg/codesys/codesys_nodeps}

echo "package: $opencv_pkg"

# now we unpack this deb package locally
echo "unpack into local folder"
dpkg-deb -R $opencv_pkg "codesys-raspi-opencv-pkg"

# now we can modify the dependency section of the deb package control file
echo "modify dependencies"
sed -i "s/^Depends:.*/Depends:/" "codesys-raspi-opencv-pkg/DEBIAN/control"

# now we pack it again into a deb file
echo "pack into new package: $newpkg"
dpkg-deb -b "codesys-raspi-opencv-pkg" "$newpkg"

# now we remove the temp folder
rm -rf "codesys-raspi-opencv-pkg"

echo "Showing info from new packaged without dependencies:"
dpkg-deb -I "$newpkg"

echo "done!"
  • dont forget to make that file executable with the command
chmod +x <filename>
  • then you can execute it with:
./fix_cds_opencv_deps.sh"
  • If that worked, we can focus on the hard work: compiling opencv by yourself:

  • we need some prerequisites to be able to compile.
    The easy ones are:

sudo apt-get install build-essential git cmake pkg-config 
  • The difficult ones are:
sudo apt-get install libjpeg8-dev 
sudo apt-get install libtiff4-dev 
sudo apt-get install libjasper-dev 
sudo apt-get install libpng12-dev 
sudo apt-get install libavcodec-dev 
sudo apt-get install libavformat-dev 
sudo apt-get install libswscale-dev 
sudo apt-get install libv4l-dev 

The versions or numbers of the detailled packages might be slightly different to the mentioned ones. just try with autocompletion which ones work.
They heavily depend on the real rasbian version that you are using.

  • Now we proceed:
    This is my script: "raspi_install_opencv.sh":
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash

# script to download and compile 
# opencv from source in version 2.4.9
# that fits to the codesys package opencv
#
# only execute this directly on the raspberry (as we dont cross compile)
# and at the end we install the compiled libraries on this target
# 

#
# there are some build prerequisites
# sudo apt-get install build-essential git cmake pkg-config 
# these depend on the detailled config of opencv that you want to build:
# sudo apt-get install libjpeg8-dev 
# sudo apt-get install libtiff4-dev 
# sudo apt-get install libjasper-dev 
# sudo apt-get install libpng12-dev 
# sudo apt-get install libavcodec-dev 
# sudo apt-get install libavformat-dev 
# sudo apt-get install libswscale-dev 
# sudo apt-get install libv4l-dev 
# 
# the versions or numbers of the detailled packages might be slightly different to the mentioned ones. just try with autocompletion which ones work
#

# just a test if we are running on a raspi. if that does not work properly and you are sure that you are running
# on a raspi then just comment the next line:
if [ -z "$(grep 'BCM' /proc/cpuinfo)" ]; then 
    echo "execute this script only on a raspberry"; 
    exit -1
fi

echo "creating local working dir: opencv"
mkdir opencv
cd opencv;

if [ ! -f "2.4.9.1.zip" ]; then
    echo "downloading the sources from github"
    wget https://github.com/opencv/opencv/archive/2.4.9.1.zip
fi

echo "unzip the zip"
unzip 2.4.9.1.zip

echo "go into directory"
cd 2.4.9.1

echo "prepare compilation"
mkdir build
cd build

echo "call cmake"
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_EXTRA_MODULES_PATH=. ..
echo "cmake done"

echo "now compile"
make all -j4
echo "compilation done"

echo "install now"
sudo make install 

echo "congrats, you made it until here"
  • dont forget to make that file executable with the command
chmod +x <filename>
  • then execute it:
./raspi_install_opencv.sh

the compilation will take some time. in the end it will install all libraries compiled on the raspi.

Now you can install the codesys opencv package (the fixed one), and it should work:

as admin user (root) or with sudo:

dpkg -i codesys_nodeps_raspi_opencv_V3.5.11.0.deb

This should work and afterwards you should be able to run opencv example projects on your current Raspberry.

I admit that this a pretty fast and roughly hacked tutorial, but in general it should work.
Just let me know where you had problems, so i can make this hack here better and a more robust hack.

Good luck and have fun with it.

Posted by erichspitzweg 2018-05-06
  • gesba - 2020-04-12

    Hello
    I am new here and I have transferred the package dpkg-deb -R $ opencv_pkg "codesys-raspi-opencv-pkg"
    .deb with filezilla but the unpack gives me an error that it is not a debian file.
    How do you transfer your codesys_raspi_opencv_V3.5.11.0.deb from codesys To raspberry if it doesn't let you install it because it gives you dependencies error?

    Thanks in advance.

     
  • gesba - 2020-04-12

    Got it !!!!
    I did it by removing the codesys package install again transfer the .deb again and it gave no problems.
    Most likely codesys_raspi_opencv_V3.5.11.0.deb was corrupt.
    Thank you very much for the great contributions of your publications.

     
  • ravsingh - 2020-09-07

    Hi,
    Just having some troubles trying to execute the first script, it says syntax error, end of file unexpected ("expecting then")

    any pointers?
    TIA

     
  • ravsingh - 2020-09-07

    pi@raspberrypi:/opt/codesys $ ls
    bin codesys_raspi_opencv_V3.5.11.0.deb fix_cds_opencv_deps.sh lib scripts
    pi@raspberrypi:/opt/codesys $ sudo nano fix_cds_opencv_deps.sh
    pi@raspberrypi:/opt/codesys $ sudo sh fix_cds_opencv_deps.sh fix_cds_opencv_deps.sh
    fix_cds_opencv_deps.sh: 32: fix_cds_opencv_deps.sh: Syntax error: end of file unexpected (expecting "then")
    pi@raspberrypi:/opt/codesys $

     

Log in to post a comment.