gidesa/ocvWrapper46

Question: How to load a pCvMat from a TStream

Closed this issue · 10 comments

Thank you for this great implementation! I am new to OpenCV so my apologies up front if my question is obvious.

I am trying to load a JPG image from a TStream. Your code samples all show how to load an image from a file, but I cannot figure out how to load an image from a stream (eg a TFileStream or TMemoryStream).

I believe that I should use the openCV function imageMat(imageData) for that.

Is this method available in your wrapper? Or if not, is there a recommended way or a sample code to load an image from a stream?
(note: I am using FPC 3.2.2/Lazarus 2.2)

Thank you

gidesa commented

Hello, thank you for appreciating my work.
You could load a Opencv image from a TBitmap object, using the Bitmap2MatImage function in OpenCvWrapper.pas.
This, because the internal memory of a Opencv image is very similar to a bitmap image.
If you use the TOcvImage class from unOpencvDelphi46.pas (an abstraction over a Opencv Mat image), there are methods to convert to/from TBitmap: toBmp/fromBmp, simply they encapsulate the Bitmap2MatImage/Matimage2Bitmap functions.
So using the bitmap included inside a Jpeg object you could load a Opencv Mat image.

Thank you so much, I got it working now.

My goal is to implement a face recognition feature. I assume that I would have to train the library first, and then call some recognition feature that checks how a face matches the trained faces.

The images (that I load from the stream) are cropped face extracts from a face detection routine. I now need to train and recognize these extracts. Do you happen to have a sample available for that? tia

gidesa commented

You can examine the examples in Lazarus components directory (or same in Delphi components). Here you find a class to implement a face detection and recognition software, plus components wrapping the classes for a simpler use in Lazarus or Delphi IDE.

I overlooked that there was another sample in the components folder. That helped tremendously! Thank you so much for the pointer. This will definitely give me a jump start.

I have created a script to compile your wrapper for macOS, lightly based on the Linux script as provided in your repository. Feel free to add the attached content to your repository's home/macOS folder and unzip the file there.

In order for it to work, the {$IFDEF LINUX} conditionals in Delphi\OPENCVWrapper.pas should be changed into {$IFDEF UNIX} so that the memory copy works on all UNIX variants (like Linux and macOS)

And above {$IFDEF DEBUG} add the following to link the dylib.

{$IFDEF FPC}
  {$IFDEF DARWIN}
    {$linklib libocvCPPWrapper46.dylib}
  {$ENDIF}
{$ENDIF}

macOS.zip

Here's a screenshot of your object detection example running on macOS with a M1 (Silicon/Arm) processor

Screenshot 2023-08-26 at 23 21 20
gidesa commented

Thank you for your porting to MacOs. A very nice extension.
In next days I will add your contribution to repository.
A question: in the zip file there is a _MACOSX directory, it contains some binary files. It seems to me a temp/appo directory. Must it to be copied in repository?

ah yes, you can ignore that folder. It's a macOS thing to add such cache folder...and on macOS you don't see it. I should have double checked the ZIP.

gidesa commented

Ok, released your very useful scripts for MacOS. Thank you!

That's great! Thank you