Jump to content
Sign in to follow this  
shineworld

First Python + DelphiVCL Program

Recommended Posts

Good,
after a long time stressing this forum, especially the Python4Delphi channel, with lots of rookie requests,

I got to a good point with the development of my first Python program.

 

Until a few months ago I had always ignored Python and its possibilities as Delphi has always been a tool

with which I create all my works and I have never thought of anything else.

 

When Python4Delphi and DelphiVCL showed up I wondered if I could do something interesting with both

and I must admit that although Python was completely new land to me, the fact of sticking with Delphi

anyway took away any doubt. .. I had to try.

 

Basically, the program is pure Python (after being compiled with Cython), an embedded version, with the addition

of DelphiVCL (I've never used FMX so it's better to start from the VCL that I know very well) and some Python modules

made in Delphi where I put the more delicate parts and in use real threads and not "crippled" threads by the GIL.


I anticipate, it is nothing transcendental, but as a first Python project, I am satisfied with it.

Description of video
In this short video, we can see the execution of an external program written in Python for the holding of print markers

necessary to calculate the zero machining, the rotation of the piece on the work table, and all the scaling needed to

compensate for the error of model printing between CAD and plotter printer.

 

The Python program interacts directly with the CNC that moves the XYZ axes for the final cut through an API Client (cnc_api_client_core in PyPi)

to the CNC control software API Server, retrieving information and sending direct commands to the CNC System.

 

Image capture is done using a proprietary IP Camera equipped with LED lighting.
 

The Python program is executed through an embedded version of the language prepared with all the necessary tools and allows two UI,
vertical and horizontal, to adapt to all types of monitors.

NOTE:

The below CNC Control Software is 100% made with Delphi 🙂


Many Thanks to forum people for the support!

  • Like 4

Share this post


Link to post

Thanks for the insight view.

That looks great, but I'm not sure if I understand right how Delphi and Python interact here.
You say the RosettaCNC is written in Delphi, also handling the video-camera, while the Python code is doing the image processing to find the marker, is that what you showing us here ?

 

On the other hand you wrote that Python is controlling the CNC directly, but I assume you mean only the move from one zero-marker to the other, or do you mean also the full G-Code of the drawing ?

If the latter: What about the saying that Python is about 10+ times slower, I would expect that it should better not be used for realtime CNC control.

Ok, maybe the CNC controller understands G-Code or the like directly and you do not control the motor steps directly, so that also Python could handle it well.

 

In combination Delphi and Python makes a lot of sense, also I plan to start my first Python project when I have some spare time.

Share this post


Link to post

Actually, I was a little poor in the description because I thought I shouldn't bore you.

Let's see if I can group the ideas.

 

1] The IP Camera
The IP camera is made with an embedded board + a motorized camera sensor + Linux.

The IP camera, mounted on the Z axis of the CNC, uses a LAN connection to receive commands (eg: manual focus, brightness, resolution, etc)

and return a stream of encoded and compressed frames.

To do this, it uses the TCP / UDP protocols.
The management software was initially made with Python but then I will calmly rewrite it completely in C ++.

Theoretically, the code is already performing even in Python, as the bottleneck remains the acquisition of the frames from the sensor,

their encoding, and compression, which is already done using native code libraries.

In any case, a large part of the Python project was then compiled with Cython, creating python modules .so that a few percentage points of profit made me get.

 

2] Delphi and its expansion modules for Python.
Initially, I tried to manage TCP/UPD streams directly in Python via sockets and threads, but unfortunately, Python gives the worst,

as threading management is always subject to the rules of the GIL and therefore in fact creating more threads in python does not always

mean being able to optimize the use of cores.

I will not dwell on this question, which I too was not aware of, there are many discussions on the net.

I, therefore, thought of using Delphi to create an extension module for Python in which I entered all the communication work between PC and

Camera using Delphi's TThread (more practically internally I used INDY both for the TCP Client and for the UDP server)

In python, I no longer needed to create threads for managing TCP/UDP packets with the resolution of many problems that I had had in the first tests.

Still in the Python expansion module written in Delphi, I was able to manage other features not present in ready-made libraries for Python,

but which I already had working fine in Delphi, certainly gaining performances.

 

3] Python program
The Python program is actually made up of Python + DelphiVCL + Skia + OpenCV and other minor libraries, plus an image processing framework

that I built from scratch in pure python language.

To improve performance, the whole package, apart from the main file, was also compiled with Cython, obtaining a series of .pyd modules that make up the final product.

 

4] CNC
The CNC is actually made up of an embedded board with a REAL TIME industrial operating system that I wrote years ago and that we have been using for years

for CNC and PLC and all the CNC part is done directly on the board.

The CNC board, which controls the motors/inputs/outputs/EtherCAT /etc, communicates with the PC and with the CNC control software via LAN.

The control software is just a UI interface, has a G-code compiler, and takes care of transferring motion instructions or pre-processed blocks to the CNC board's execution buffer.
In the CNC control software, there is an API server (TCP/Server) which allows an external program/process, through an API Client, to access all the functions of the CNC,

including sending programs, MDI programs, JOG, etc.

So for Python, I have created a package that implements the client core API allowing a Python program to have full control of the CNC.

 

NOTE
The program can use Themes.
Without a theme, the assignment of a Bitmap to TImage objects (a Window control) can generate flickering (due to the WM_ERASExxx message).
Using Themes the flickering increase a lot.
So I've created a new DelphiVCL branch with Graphics32:TImgView32 which is a TGraphicControl-based object and solved the flickering phase managing the PAINT event.

Now I've been really boring :)
Sorry!

  • Thanks 1

Share this post


Link to post
16 hours ago, shineworld said:

Now I've been really boring 🙂

Not so much, reading good technical success stories is one of my hobbies :classic_biggrin:

  • Like 2

Share this post


Link to post

My First P4D was completed and released 🙂

Thanks to the developers of PyScripter and P4D for these amazing tools.
 

 

Edited by shineworld
  • Like 5

Share this post


Link to post

Thats a great application, I was looking for a similar option to find and exactly locate markers and their positions in an image,

but I'm afraid I cannot use P4D since this has to be CrossPlatform.

Maybe you have evaluated and tested some pure Pascal or Algorithmic solutions to find markers and you could share it with us ?

( and I don't want to use Cloud-KI solutions either ).

Share this post


Link to post

Looks to use OpenCV which is a computer vision library for the marker detection task. The bulk of other code is for hooking everything together and providing a UI. Delphi doesn't have the level of support for OpenCV and things like it that Python does. Bindings, examples, discussions and the number developers doing the same thing or similar are all larger for Python. OpenCV itself is multi-platform.

 

You can use OpenCV from Delphi but it is a LOT more work and probably an exercise in frustration to both get things working and develop a solution to a problem. Likely less effort overall to develop parts in Python to come  up with a solution so you can take advantage of it's ecosystem and then migrate that to Delphi if desired.

Share this post


Link to post

@Brian Evans

Yes, but I hope that shirneworld has found any other stable marker detection without even OpenCV, which is not that Cross-Plattform too.

OpenCV is of course perfect in this case.

I still hope to find some simple algorithm for marker detection, like e.g. QR-Code detection, to be able to locate them on pictures without much external stuff.

Yes, QR-Code detection algorithm was one of my favorites too, but maybe there could be even more optimized algorithms just fpr that purpose.

The markes itself were not critical, can be any shape.

 

 

Share this post


Link to post
7 hours ago, Rollo62 said:

but I'm afraid I cannot use P4D since this has to be CrossPlatform.

 

It is cross-platform.  It supports all targets Delphi supports.

  • Like 1

Share this post


Link to post

Yes, I've used OpenCV features to detect markers but my CNC Vision framework is born and designed to do more other.

I'm already working on a system which use TensorFlow for more complex image objects detection where Delphi will do things hard to do natively in python.

Share this post


Link to post
18 hours ago, pyscripter said:

It is cross-platform.  It supports all targets Delphi supports.

Thanks, yes I know.

But I'm afraid this would be simply getting too heavy for my app.

Moreover Python alone doesn't come with OpenCV and OpenCV is too heavy as well.

I hope the portions for marker detection could be stripped off separately, like using simple filters for that goal.

My consideration is that simple, specific marker-shapes might exist that have simple, specific detection algorithms as well, to allow easy and simple detetion and location.

 

Edited by Rollo62

Share this post


Link to post
On 10/29/2022 at 4:38 PM, Rollo62 said:

I still hope to find some simple algorithm for marker detection, like e.g. QR-Code detection, to be able to locate them on pictures without much external stuff.

Probably zxing has something you need. It's for QR's only

Share this post


Link to post

Sincerely, I've used an embedded version of Python with minimal packages installed, and the required files are few.

In Embarcadero GitHub repositories you can find a distro ready to be used.

No anaconda, conda, or virtual environment.

Just point to python dll with P4D and the joke is made.

 

You can also avoid calling a pure py script ad use Embarcadero P4D-Data-Sciences,

PythonEnvironments and Lightweight-Python-Wrappers and remain always on the Delphi project. 

 

For markers detection, you can always extract the math and logic of FindContours HougCircles. etc from OpenCV sources,

as suggested by the name they are open but you have to use something like to NumPy.

 

I don't like re-invent the wheel when it is already done so I've discarded the idea to re-write all math in pure pascal code,

although the related math (find counters, HougCircles, etc) is very simple, but very well implemented in OpenCV.    

 

What they have done in embarcadero with P4D and related tools is very important and allows them not to reinvent hot water by going direct to the use of tools born for scientific purposes.

 

 

Share this post


Link to post
On 10/31/2022 at 7:24 PM, shineworld said:

Sincerely, I've used an embedded version of Python with minimal packages installed, and the required files are few.

In Embarcadero GitHub repositories you can find a distro ready to be used.

No anaconda, conda, or virtual environment.

Just point to python dll with P4D and the joke is made.

 

You can also avoid calling a pure py script ad use Embarcadero P4D-Data-Sciences,

PythonEnvironments and Lightweight-Python-Wrappers and remain always on the Delphi project

 

For markers detection, you can always extract the math and logic of FindContours HougCircles. etc from OpenCV sources,

as suggested by the name they are open but you have to use something like to NumPy.

 

I don't like re-invent the wheel when it is already done so I've discarded the idea to re-write all math in pure pascal code,

although the related math (find counters, HougCircles, etc) is very simple, but very well implemented in OpenCV.    

 

What they have done in embarcadero with P4D and related tools is very important and allows them not to reinvent hot water by going direct to the use of tools born for scientific purposes.

 

 

 

dear shineworld, If you don't involve a lot of deep learning capabilities, i recommend the MITV Lab Packages, all are native compilation , does not need Python

 

https://mitov.com/products/visionlab#overview

Share this post


Link to post

Thanks for the suggestion I will go and look at it.

There is another aspect behind the scenes about the choice to use Python.
In the whole office the only one who uses and knows Delphi and object pascal is me, while my colleagues have been using Python for support projects for a long time already, so creating a framework that is based on Python + extra extensions made ad hoc with Delphi allows me to relieve myself from future work on the Vision compartment to devote myself to something else.

This is the reason why, when I can, I prefer to create tools that others use and not take a complete, specific project all the way through.

 

My mathematical background, compared to that of some of the team members, is basic, and they in python will surely be able to solve more elegantly and efficiently cases where my limitations will only get in the way.

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
Sign in to follow this  

×