lessthanoptimal / PyBoof

Python wrapper around the BoofCV Computer Vision Library
Apache License 2.0
67 stars 12 forks source link

Py4j Gateway issue when re-initializing pyboof #18

Open gaurav-t-sca opened 2 years ago

gaurav-t-sca commented 2 years ago

Steps to replicate on version 0.40.1:

import pyboof as pb
import numpy as np
pyboof_detector = pb.FactoryFiducial(np.uint8).qrcode()  # Works

pb.init_pyboof()
pyboof_detector = pb.FactoryFiducial(np.uint8).qrcode()  # Throws an error

The error thrown is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/pyboof/recognition.py", line 533, in __init__
    self.boof_image_type = dtype_to_Class_SingleBand(dtype)
  File "/usr/local/lib/python3.9/site-packages/pyboof/image.py", line 512, in dtype_to_Class_SingleBand
    return gateway.jvm.java.lang.Class.forName(class_path)
  File "/usr/local/lib/python3.9/site-packages/py4j/java_gateway.py", line 1709, in __getattr__
    answer = self._gateway_client.send_command(
  File "/usr/local/lib/python3.9/site-packages/py4j/java_gateway.py", line 1036, in send_command
    connection = self._get_connection()
  File "/usr/local/lib/python3.9/site-packages/py4j/java_gateway.py", line 980, in _get_connection
    raise Py4JNetworkError("Gateway is not connected.")
py4j.protocol.Py4JNetworkError: Gateway is not connected

In the same interpreter, if I run pb.check_jvm(False) afterwards, it returns True

Versions:

Any help would be greatly appreciated. Thanks in advance!

lessthanoptimal commented 2 years ago

This is caused by a quirk of python where global variables are copied into different .py files and not referenced.

https://stackoverflow.com/questions/60524565/global-variable-imported-from-a-module-does-not-update-why

working on a fix

lessthanoptimal commented 2 years ago

branch fix/globals as an update that resolves the issue. Will test it more carefully later.

lessthanoptimal commented 1 year ago

@gaurav-t-sca is this resolved?

liuyang-664 commented 1 year ago

I still have the same problem. With PyBoof 0.43.1, python 3.6.9, py4j 0.10.9.7.

liuyang-664 commented 1 year ago
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: pyboof/PyBoofEntryPoint has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:621)
Failed to successfully launch the JVM after 5 seconds.  Aborting
Traceback (most recent call last):
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/py4j/java_gateway.py", line 982, in _get_connection
    connection = self.deque.pop()
IndexError: pop from an empty deque

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/py4j/java_gateway.py", line 1132, in start
    self.socket.connect((self.address, self.port))
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ztest_zbar.py", line 9, in <module>
    import pyboof as pb
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/pyboof/__init__.py", line 250, in <module>
    python_port=int(os.environ.get('PYBOOF_PYTHON_PORT', 25334)))
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/pyboof/__init__.py", line 84, in init_pyboof
    __init_memmap(size_mb)
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/pyboof/__init__.py", line 156, in __init_memmap
    pbg.gateway.jvm.pyboof.PyBoofEntryPoint.initializeMmap(mmap_path, size_mb)
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/py4j/java_gateway.py", line 1715, in __getattr__
    "\n" + proto.END_COMMAND_PART)
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/py4j/java_gateway.py", line 1036, in send_command
    connection = self._get_connection()
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/py4j/java_gateway.py", line 984, in _get_connection
    connection = self._create_connection()
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/py4j/java_gateway.py", line 990, in _create_connection
    connection.start()
  File "/home/jaka/.virtualenvs/yolov4/lib/python3.6/site-packages/py4j/java_gateway.py", line 1144, in start
    raise Py4JNetworkError(msg, e)
py4j.protocol.Py4JNetworkError: An error occurred while trying to connect to the Java server (127.0.0.1:25333)