Jump to content
JGMS

How to get CV2 working

Recommended Posts

P4D is great! I use it with pleasure, backboned by PyScripter, great as well!) to test my code beforehand.

The following Python code works perfect in PyScripter, but not in P4D in Delphi 11.2 or 11.3

import cv2
import imutils

image = cv2.imread("E:/APPS/ScannedFotos/DCIM/100MEDIA/PICT0022.JPG")
image_rotated = imutils.rotate(image, 2)
cv2.imwrite("E:/APPS/ScannedFotos/DCIM/100MEDIA/PICT0022.JPG", image_rotated, [int(cv2.IMWRITE_JPEG_QUALITY), 80])

In Delphi (VCL) I have have a form with a TPyEmbeddedResEnvironment310. All required Python libraries are present in de 3.10 folder within the Win64 subfolder of the Delphi code. (numpy, cv2, imutils, etc...)

The PythonEngine is loaded in the formcreation routine like so:

procedure TPyForm.FormCreate(Sender: TObject);
begin
  PythonEngine1              := TPythonEngine.Create(PyForm);
  PythonEngine1.RegVersion   := '3.10';
  PythonEngine1.DllName      := 'python310.dll';
  PythonEngine1.AutoLoad     := false;
  PythonEngine1.AutoFinalize := true;
  PythonEngine1.AutoUnload   := true;
  PythonEngine1.UseLastKnownVersion := false;
  PythonEngine1.RedirectIO   := false;
  PythonEngine1.loadDLL; 
  PyEmbeddedResEnvironment3101.pythonEngine := PythonEngine1;
  PyEmbeddedResEnvironment3101.Autoload     := True;
end;

The function that should work (like many others I have created so far):

Function TPyForm.Rotate_Plaatje_Met_CV2(Foto_bestand : String; Hoek : Double) : Boolean;
  VAR
    Mem :TStringList;
   begin
       Foto_bestand :=  B2F(Foto_bestand) ;
   TRY
       Mem := TStringList.Create;
       if FileExists(Foto_Bestand) then

       With Mem DO
       begin
          Add( 'import cv2 ' );
          Add( 'import imutils' );
          Add( 'image = cv2.imread("' + Foto_bestand + '")');
          Add( 'i = ' + Hoek.ToString  );
          Add( 'image_rotated = imutils.rotate(image, i) ' );
          Add(  'cv2.imwrite("'+ Foto_bestand + '", image_rotated, [int(cv2.IMWRITE_JPEG_QUALITY), 80]) ');
       end;
     TRY
       PythonEngine1.ExecString( ansiString( Mem.text ) );
       Result := True;
     Except
       Result := False;
     END;
     FINALLY
       Mem.Free;
     END;
   end;

I have no idea what is wrong. I tried lots of different ways with CV2 codes found on the internet, but all without success. I keep ketting the messages:

Project ....exe raised exception class $C000008E with message 'c000008e FLOAT_DIVIDE_BY_ZERO'.
   and when trying once more directly therafter:
Project ....exe raised exception class EPyAttributeError with message 'AttributeError: partially initialized module 'cv2' has no attribute 'INTER_AREA' (most likely due to a circular import)'.

I hope anybody can help me out of this circle. Because it just runs in PyScripter, it must be solvable, isn't it?

Many thanks ahead.

 

Jan

Edited by JGMS
To make it clear that the code is correct and that it concenrs VCL

Share this post


Link to post

Thank you so much!

I am so sorry that I did not recently look at this forum!

It works now! Unbelievable.

 

P4D is even greater now!

 

Regards, Jan

 

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×