tomasmcz / imago

Optical position recognition for the game of Go
Other
82 stars 27 forks source link

support for python 3? #11

Open Zacharias030 opened 4 years ago

Zacharias030 commented 4 years ago

Since python2 is end-of-life'd, are you going to support python 3?

tomasmcz commented 4 years ago

Hi! Thanks for your interest in this project! Unfortunately, I am not currently developing it, so I'm not planning to support Python 3 right now. Although I suspect that after using 2to3 there will not be a lot of work left to make the conversion work.

ChristianBarette commented 3 years ago

I have started to translate in Python3 thanks to 2to3 and Pylint but I am stucked by "import pcf" not found : src/filters.py:import pcf src/filters.py: pcf.edge(image.size, image.tobytes())) Could you tell me what is the file ~/src/pcf.so ? Why it is not pcf.py ?

ChristianBarette commented 3 years ago

As I have found that pcf.so is a compilation result of pcf.c file, I have change the make file with python3.8 .

before install: src/pcf.so src/pcf.so: pcf.c gcc -Ofast -fPIC -shared -I/usr/include/python2.7/ pcf.c -lpython2.7 -o src/pcf.so clean: rm -f src/pcf.so src/*.pyc

after install: src/pcf.so src/pcf.so: pcf.c gcc -Ofast -fPIC -shared -I/usr/include/python3.8/ pcf.c -lpython3.8 -o src/pcf.so clean: rm -f src/pcf.so src/*.pyc

When I run make I get this message make gcc -Ofast -fPIC -shared -I/usr/include/python3.8/ pcf.c -lpython3.8 -o src/pcf.so pcf.c: In function ‘initpcf’: pcf.c:181:10: warning: implicit declaration of function ‘Py_InitModule’ [-Wimplicit-function-declaration] 181 | (void) Py_InitModule("pcf", myModule_methods);

Then when I run imago, I get this error " module 'pcf' has no attribute 'edge' " python3.8 imago -S image.jpg Traceback (most recent call last): File "imago", line 7, in imago.main() File "/home/cbaba/webdev/imago-master-py3/src/imago.py", line 122, in main lines, l1, l2, bounds, hough = linef.find_lines(image, do_something, logger) File "/home/cbaba/webdev/imago-master-py3/src/linef.py", line 80, in find_lines im_h = prepare(image, show_image, logger) File "/home/cbaba/webdev/imago-master-py3/src/linef.py", line 23, in prepare im_edges = filters.edge_detection(im_l) File "/home/cbaba/webdev/imago-master-py3/src/filters.py", line 45, in edge_detection pcf.edge(image.size, image.tobytes())) AttributeError: module 'pcf' has no attribute 'edge'

Have you heard about discrepancies in calling C between python2.7 and python3 ?

tomasmcz commented 3 years ago

Hi, thanks for your interest in this project!

I started the conversion from Python2 to Python3 some time ago, but haven't finished it. As for the pcf.c file, see this commit in the python3 branch of this repository: ac57970348e16cdaacfea1a70700a1adf4b18c41 It was working with Python3.6.

I see that I have converted some Python files as well. I'm not sure what's left to fix. Unfortunately, I don't have time to test it right now. If you find out, plese let me know.

ChristianBarette commented 3 years ago

Hi,

I plan to use a subset of Imago (ie the command : python3.8 imago -S image.jpg), so my conversion will not be complete but relative.

Today the compilation is OK but I am stuck between Python and C Langage : Python don't recognize Py_edge in the library.

I am a retired engineer, I started in 1980 and I leaved the development job for management job in 1993. So I am 30 years late in IT. Today I have time for golf and fun IT.

My knowledge in C Langage is completely vanished but I remember it's a powerful Langage and I liked it. My knowledge in Python didn't exist few days ago.

I have created a PWA application in HTML, CSS and Javascript and it's OK on Android smartphone. The missing part is calling Imago from client side.

As Imago requires Python2.7 and I am thinking a PWA is possible in Python, I try to converse Imago then I will converse my application in Python (or calling Python from Javascript ....).

So thanks you for your answer I will try your new files and return to you.

Keep in touch.

PS: I visited Prague, it's a very nice city.

ChristianBarette commented 3 years ago

Hi, I understand why the passage of parameters from Python to C (calling pcf.edge in edge_detection, in filters.py) fires the following error :

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 1054: invalid start byte

Python transfers image raw data (tobytes), instead of pointer on this data... Of course, raw data is not utf-8 data....

To suppress this error, I have encoding tobytes in ASCII before transferring like this :

def edge_detection(image): """Edge detection (on BW images).""" new_image = image.filter(ImageFilter.GaussianBlur())

GaussianBlur is undocumented class, it might not work in future versions

# of PIL

# begin cba
# encode tobytes in ASCII with xbm mode    
imgcba = Image.new('L', image.size)
imgcba = image
octets = bytes(imgcba.tobytes("xbm"))
# end cba

new_image = Image.frombytes('L', image.size,
             # pcf.edge(image.size, image.tobytes()))
             pcf.edge(image.size, octets))  # cba

return new_image

Of course pcf.c must be changed to take into account the modification of interface and to add encoding/decoding ASCII/raw data.

So two solutions : modify pcf or converting pcf in Python.

Questions :

1> to be able to have imago in a single langage ie Python, do you know if all mathematical functions (math.h) are available in Python ?

2> If I wanted to modify pcf.c, could you give me guidelines or advices for parsing the parameters and using of PyArg_ParseTuple ?

ChristianBarette commented 3 years ago

Third solution

to wrap imago in a virtual machine with python2.7 (or 3.6) with virtualenv. I have tried it, it is OK.

tomasmcz commented 3 years ago

The reason why some of the functions were written in C is that I thought they would be quite slow in Python. I have not actually measured the exact difference in speed, but I'm not sure if just rewriting it all to Python would be a good idea. (I was actually thinking about going the other way and rewriting the RANSAC algorithm to C as well, because my Python implementation is quite slow, but that one is a bit more complex than for example the Hough transform.)

You could try to write these functions with the help of some Python library that is better suited for array computations than pure Python, e.g. NumPy. Or you can try to find libraries that already implement these functions efficiently. I don't think that there was a library for Hough transform at the time that I wrote Imago, but I didn't look for it very hard, because I wanted to try to program it myself. I think I took a course in low level algorithm optimization in C that year, that might have also contributed to my decision :-D

I haven't done any project where I would interface C and Python recently, so I don't know how to do that properly with the new Python. To be honest, even when I wrote the original version, I just copied something from the Python documentation and tweeked it until it worked. Since I only needed to interface these few funcions in the pcf.c and was not building a public library, I just didn't bother to try to understand in any depth. So I can't really help very much with that.

If I have some free time, I'd like to finish porting Imago to Python3, but I can't really promise if and when that would happen.