Recent

Author Topic: In Raspi,Is the library openCV that call by Python and Lazarus different?  (Read 2896 times)

sxbug

  • New Member
  • *
  • Posts: 11
In raspberry Pi 4, I simply use 'PIP install opencv-python' to install opencv, and then I can use OpenCV to realize image display, face recognition and other functions. The openCV  version is 4.1.1.26. It is easy way.

I used 'sudo apt get install libopencv-dev' to install some openCV .so files, and I use raspiopencv.pas to make a simple lazarus project, which can be compiled in Lazarus, but not run. When I ran it ,msg displayed ' not found raise. C '  etc. I took a look under the directory. It seems that OpenCV .so files' VERSION is  3.2  installed.

I have these questions: 1. Can't copy several DLL files just like in windows use openCV library under raspberry  ? What's the easiest way to use opencv under Lazarus? 2. If I used cmake to reinstall openCV3.2 ,May cause opencv under Python unusable? 3. Is openCV in Python related to openCV in Lazarus?

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
I thought python uses its own versioned opencv: python-opencv.
If pip has installed the standard opencv libraries under /usr/lib or /usr/local /lib you do not have to do anything, just make sure you have  correct headers for pascal.
The opencv api tends to change a lot between major versions.
If that is not the case then indeed, copy them to a user location and put the path in your fpc.cfg ( or use -Fl from the command line)
If you want the latest opencv download this bash file here: https://github.com/milq/milq/blob/master/scripts/bash/install-opencv.sh (official script)
and run
Code: Bash  [Select][+][-]
  1.  sudo bash install_opencv.sh

This will not affect python.!

Note I an not aware that opencv can already use the VideoCore VI GPU so it can be slow.

There are pre-build packages through this link https://www.pyimagesearch.com/2019/09/16/install-opencv-4-on-raspberry-pi-4-and-raspbian-buster/

[edit] it seems that python is an opencv dependency, but my suggested install methods do not affect the working of python
« Last Edit: February 10, 2020, 08:43:11 am by Thaddy »
Specialize a type, not a var.

sxbug

  • New Member
  • *
  • Posts: 11
At first thank you very much

I have installed some of the opencv libraries with the command: sudo apt-get libopencv-dev.
Then I can find them in / usr / lib / arm-linux-gnueabihf Directory,such as : libopencv_ Video. a     libopencv_ Video. so libopencv_Video.so. 3.2     libopencv_Video. so. 3.2.0

Libopencv_ Videostab     Libopencv_ Viz      Libopencv_ Ximgproc       Libopencv_ Xobjdetect      Libopencv_Xphoto      Libopencv_Core   etc (about 165  files)

Then I used this header file(RaspiOpencv.pas) to prog:
RaspiOpencv
*********************************************************************
 *  Modified to use Raspberry Pi with Ver3.3. for evaluation purpose.*
 *  Functions were changed to suit RaspberryPi and FPC Lazarus.      *
 *  Add function cvLoadImageM etc.                                   *
 *  Replaced Bitmap2IplImage and iplImage2Bitmap.                    *
 *                                                                   *
 *  K.Otani       Nov-2017                                           *
 *********************************************************************
 *                                                                   *
 *  The contents of this file are used with permission, subject to   *
 *  the Mozilla Public License Version 1.1 (the "License"); you may  *
 *  not use this file except in compliance with the License. You may *
 *  obtain a copy of the License at                                  *
 *  http://www.mozilla.org/MPL/MPL-1.1.html                          *
 *                                                                   *
 *  Software distributed under the License is distributed on an      *
 *  "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or   *
 *  implied. See the License for the specific language governing     *
 *  rights and limitations under the License.                        *
 *                                                                   *
 *********************************************************************)
unit RaspiOpenCV;

{$mode objfpc}{$H+}
interface
uses
     Classes, ctypes , Graphics , SysUtils, Math , GraphType ;
const
{$define Ver3}
// Please modify .dll/.so version name
{$ifdef MSWINDOWS}
 video_lib       = 'opencv_world331.dll';
 videoio_lib     = 'opencv_world331.dll';
 cv_lib          = 'opencv_world331.dll';
 cvcore_lib      = 'opencv_world331.dll';
 highgui_lib     = 'opencv_world331.dll';
 cvimcodecs_lib  = 'opencv_world331.dll';
 cvimgproc_lib   = 'opencv_world331.dll';
 calib3D_lib     = 'opencv_world331.dll';
 cvobjdetect_lib = 'opencv_world331.dll';
 {$endif}

{$ifdef CPUARM} //  ARM only for Raspberry Pi
{$ifdef Ver2}
//   /usr/lib/arm-linux-gnueabihf
      cv_lib      = 'libopencv_core.so.2.4';
      video_lib   = 'libopencv_video.so.2.4';
      calib_lib   = 'libopencv_calib3d.so.2.4';
      highgui_lib = 'libopencv_highgui.so.2.4';
      cxCore_lib  = 'libopencv_core.so.2.4';
    cvimgproc_lib = 'libopencv_imgproc.so.2.4';
  cvobjdetect_lib = 'libopencv_objdetect.so.2.4';

{$else}
{$ifdef Ver3}

//   /usr/local/lib
  cv_lib          = 'libopencv_core.so.3.2';
  video_lib       = 'libopencv_video.so.3.2';
  videoio_lib     = 'libopencv_videoio.so.3.2';  //add
  cvcore_lib      = 'libopencv_core.so.3.2';
  highgui_lib     = 'libopencv_highgui.so.3.2';
  cvimcodecs_lib  = 'libopencv_imgcodecs.so.3.2'; //add
  cvimgproc_lib   = 'libopencv_imgproc.so.3.2';   //add
  calib3D_lib     = 'libopencv_calib3d.so.3.2';   //add
  cvobjdetect_lib = 'libopencv_objdetect.so.3.2'; //add
{$endif}
{$endif}
{$endif}
  (*Matrix type (CvMat)*)
  CV_CN_MAX  = 64;
  CV_CN_SHIFT = 3;
  CV_DEPTH_MAX = (1 shl CV_CN_SHIFT);
  CV_8U  = 0;
  CV_8S  = 1;
  CV_16U = 2;
  CV_16S = 3;
  CV_32S = 4;
  CV_32F = 5;
  CV_64F = 6;
  CV_USRTYPE1 = 7;
  CV_AUTO_STEP = $7fffffff;
... ...

The compilation can pass, but there is an error in running. The prompt message is:
Project Project1 raised exception class 'External:SIGTRAP'. In file '../sysdeps/unix/sysv/linux/raise.c' at line 50

File not found
The file '/build/glibc-FUvrFr/glibc-2.28/sysdeps/unix/sysv/linux/raise.c" was not found.
Do you want to locate it yourself?

I did not find this file(raise.c) in the download files (library and source code). How can I do it?
Thank you again

sxbug

  • New Member
  • *
  • Posts: 11
Sorry for my poor English.
I've implemented  'sudo bash install_opencv.sh' ,and installed into          /usr/lib/lazarus/2.0.0/components/opencv

Files such as libopencv are generated in the directory:
opencv/ 3rdparty
              apps
             cmake
             data
             doc
             include
             modules
             platforms
             samples
             build
                 |
                  -------3rdparty
                            apps
                            bin
                             carotene
                            CMakeFiles
                            configured
                            data
                            doc
                            include
                            java_test
                            modules
                            opencv2
                            python_loader
                            test-reports
                            tmp
                            unix-install
                            lib/  libopencv_calib3d.so       libopencv_calib3d.so.4.2        libopencv_calib3d.so.4.2.0

I modified the header file:   raspiopencv.pas
**************************
const
{$define Ver3}

// Please modify .dll/.so version name

{$ifdef MSWINDOWS}
 video_lib       = 'opencv_world331.dll';
 videoio_lib     = 'opencv_world331.dll';
 cv_lib          = 'opencv_world331.dll';
 cvcore_lib      = 'opencv_world331.dll';
 highgui_lib     = 'opencv_world331.dll';
 cvimcodecs_lib  = 'opencv_world331.dll';
 cvimgproc_lib   = 'opencv_world331.dll';
 calib3D_lib     = 'opencv_world331.dll';
 cvobjdetect_lib = 'opencv_world331.dll';
 {$endif}

{$ifdef CPUARM} //  ARM only for Raspberry Pi
{$ifdef Ver2}
//   /usr/lib/arm-linux-gnueabihf
      cv_lib      = 'libopencv_core.so.2.4';
      video_lib   = 'libopencv_video.so.2.4';
      calib_lib   = 'libopencv_calib3d.so.2.4';
      highgui_lib = 'libopencv_highgui.so.2.4';
      cxCore_lib  = 'libopencv_core.so.2.4';
    cvimgproc_lib = 'libopencv_imgproc.so.2.4';
  cvobjdetect_lib = 'libopencv_objdetect.so.2.4';

{$else}
{$ifdef Ver3}

//   /usr/local/lib
  cv_lib          = 'libopencv_core.so.4.2';
  video_lib       = 'libopencv_video.so.4.2';
  videoio_lib     = 'libopencv_videoio.so.4.2';  //add
  cvcore_lib      = 'libopencv_core.so.4.2';
  highgui_lib     = 'libopencv_highgui.so.4.2';
  cvimcodecs_lib  = 'libopencv_imgcodecs.so.4.2'; //add
  cvimgproc_lib   = 'libopencv_imgproc.so.4.2';   //add
  calib3D_lib     = 'libopencv_calib3d.so.4.2';   //add
  cvobjdetect_lib = 'libopencv_objdetect.so.4.2'; //add
{$endif}
{$endif}
{$endif}
.......
***********************************************************
But there was still an error:
When I execute project in IDE:
The compilation can pass, but there is an error in running. The prompt message is:
Project Project1 raised exception class 'External:SIGTRAP'. In file '../sysdeps/unix/sysv/linux/raise.c' at line 50

File not found
The file '/build/glibc-FUvrFr/glibc-2.28/sysdeps/unix/sysv/linux/raise.c" was not found.
Do you want to locate it yourself?

When I execute in terminal:
pi@raspberrypi:/usr/lib/lazarus/2.0.0/components/opencv/lazarus $ ./project1

(project1:3265): Gtk-ERROR **: 11:43:35.665: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported

These files  such as :libopencv_ Video. a     libopencv_ Video. so libopencv_Video.so. 3.2     libopencv_Video. so. 3.2.0  are still  /usr/ lib/arm-linux-gnueabihf Directory.

Do I  manually delete all libopencv3.2 files under /usr/ lib/arm-linux-gnueabihf , and then copy these opencv4.2 files to /usr/ lib/arm-linux-gnueabihf?
Which directory is the opencv libs normal installation in? Is there a problem with the manual copy/move?
Can I delete any files under TMP?

Thanks

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
You should not add the versioning! after .so. Never. The OS resolves that for you. If versioning is really needed, it will be before .so, not after.
« Last Edit: February 12, 2020, 08:16:53 am by Thaddy »
Specialize a type, not a var.

sxbug

  • New Member
  • *
  • Posts: 11
I moved the opencv files(had finished install) to a new directory -- / home / pi / opencv / opencv-4.2 ,  and deleted all libopencv3.2 files in the / usr/ lib/arm-linux-gnueabihf directory, and added opencv4.2.so file such as libopencv_core, libopencv_video libopencv_video, etc. to / usr/ lib/arm-linux-gnueabihf directory,  and I found opencv 4.2 files had been in usr / local / lib dirrectory.
But now Lazarus can't compile this project, showing : project1.lpr (20, 0) error: error while linking
It seems that some opencv library files are not linked correctly. What should I do?

Thanks!!!!!

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Any call to opencv should be declared as cdecl. Maybe you forgot?
Specialize a type, not a var.

sxbug

  • New Member
  • *
  • Posts: 11
I use this header file(raspiopencv.pas) . Please help me to read attach file. I check that 'Cdel' is used. Have the functions in openCV4.2's so file changed?

Please enlighten me.

Thank you

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
You might want to take a look at this thread:
https://forum.lazarus.freepascal.org/index.php/topic,38121.0.html
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

sxbug

  • New Member
  • *
  • Posts: 11
raspberrypi 4.19.102-v71
fpc 3.0.4
lazarus:2.0.0
opencv(download):4.2
libopencv-dev:3.2

I can't find any useful information for using opencv under Lazarus of raspberry pi. I'm not familiar with the raspberry pi environment, so I don't know how to set it up correct. I think it should be caused by version mismatch. During compilation, we can see those functions ( such as cvload cvhaarobjectdetect and other) are undefined, and then displayed the error msg: error while linking.

If I must uninstall OpenCV4.2 ,How can I do?
How can I upgrade libopencv-dev to Ver4.2?

Please enlighten me. Thanks!!!!!

sxbug

  • New Member
  • *
  • Posts: 11
Re: In Raspi,Is the library openCV that call by Python and Lazarus different?
« Reply #10 on: February 14, 2020, 02:56:07 am »
Without deleting opencv4.2, I used Thaddy 's opencv_install.sh  script to install opencv3.2.0 again, and  error displayed:
......
/home/pi/OpenCV/modules/videoio/src/cap_ffmpeg_impl.hpp:2039:32: note: suggested alternative: ‘AVFMT_NOFILE’
     if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
                                ^~~~~~~~~~~~~~~~
                                AVFMT_NOFILE
/home/pi/OpenCV/modules/videoio/src/cap_ffmpeg_impl.hpp: In static member function ‘static AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext*, AVCodecID, int, int, int, double, AVPixelFormat)’:
/home/pi/OpenCV/modules/videoio/src/cap_ffmpeg_impl.hpp:2338:25: error: ‘CODEC_FLAG_GLOBAL_HEADER’ was not declared in this scope
             c->flags |= CODEC_FLAG_GLOBAL_HEADER;
                         ^~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/OpenCV/modules/videoio/src/cap_ffmpeg_impl.hpp:2338:25: note: suggested alternative: ‘AV_CODEC_FLAG_GLOBAL_HEADER’
             c->flags |= CODEC_FLAG_GLOBAL_HEADER;
                         ^~~~~~~~~~~~~~~~~~~~~~~~
                         AV_CODEC_FLAG_GLOBAL_HEADER
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:154:modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o]       error 1
make[1]: *** [CMakeFiles/Makefile2:2431:modules/videoio/CMakeFiles/opencv_videoio.dir/all]     error 2
make: *** [Makefile:163:all] error 2
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_calib3d.so.4.2 not symbol link
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_videoio.so.4.2 not symbol link
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_imgcodecs.so.4.2 not symbol link
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.4.2 not symbol link
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_video.so.4.2 not symbol link
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_imgproc.so.4.2 not symbol link
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_core.so.4.2 not symbol link
ldconfig: /usr/lib/arm-linux-gnueabihf/libopencv_objdetect.so.4.2 not symbol link
pi@raspberrypi:~ $

How can I uninstall openCV4.2? Only manual delete opencv4.2 files?
Will this be the main problem?
Thanks!!!!

sxbug

  • New Member
  • *
  • Posts: 11
Re: In Raspi,Is the library openCV that call by Python and Lazarus different?
« Reply #11 on: February 14, 2020, 10:39:58 am »
It's MAYBE clear:
1 You can only install the corresponding version (opencv3.2), and you must download and cmake it again.
2 parts of functions still cannot be used, display:
      raspberrypi:/usr/lib/lazarus/2.0.0/components/opencv/lazarus $ sudo ./project1
     OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file            /home/pi/OpenCV/modules/core/src/persistence.cpp, line 6628
     terminate called after throwing an instance of 'cv::Exception'
    what(): /home/pi/OpenCV/modules/core/src/persistence.cpp:6628: error: (-2) The node does not represent a user object           (unknown type?) in function cvRead
3 If install the wrong version opencv, you can manually delete the  so files under: / usr / local / lib ,  / usr / lib / arm-linux-gnueabihf.

Thank you for your help.

 

TinyPortal © 2005-2018