jim-easterbrook / python-gphoto2

Python interface to libgphoto2
GNU Lesser General Public License v3.0
357 stars 59 forks source link

gp_camera_exit throws 'Unspecified error' after upgrading libgphoto2-dev #128

Closed greiginsydney closed 2 years ago

greiginsydney commented 2 years ago

(I'm not sure if this should go here or @ libgphoto2).

I recently used this repo to update libgphoto2 as I required support for the Canon 250D.

apt-get install libgphoto2-dev installs v2.5.22 which pre-dates support for the 250D. Reference

Your system

Raspberry Pi 3 Model B Rev 1.2 running Raspbian GNU/Linux 10 (buster).

Here's the output of gphoto2_version:

pi@Bench3BPlus:/usr/local/share/python-gphoto2/examples $ python3 gphoto2_version.py
python: 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0]
libgphoto2: ['2.5.27.1', 'standard camlib set SKIPPING SOME (ax203 canon digigr8 dimagev directory jl2005a jl2005c kodak_dc240 mars pentax ptp2 ricoh_g3 sierra sonix sq905 st2205 topfield tp6801 SKIPPING docupen lumix)', 'gcc (C compiler used)', 'ltdl (for portable loading of camlibs)', 'EXIF (for special handling of EXIF files)']
python-gphoto2: 2.2.4
pi@Bench3BPlus:/usr/local/share/python-gphoto2/examples $

Your problem

The issue I'm now encountering is that gp_camera_exit throws 'Unspecified error' 100% of the time, where it previously used to work fine.

Here's an minimal example script:

import gphoto2 as gp

camera = gp.Camera()
context = gp.gp_context_new()
camera.init(context)
config = camera.get_config(context)
abilities = gp.check_result(gp.gp_camera_get_abilities(camera))
try:
    gp.check_result(gp.gp_camera_exit(camera))
except Exception as e:
    print('gp_camera_exit THREW: ' + str(e))

... and the output:

pi@Bench3BPlus:~ $ python3 test.py
gp_camera_exit THREW: [-1] Unspecified error
pi@Bench3BPlus:~ $

Thanks.

- G.

jim-easterbrook commented 2 years ago

Relying on other people's updater scripts can mean you don't know what state your libgphoto installation is in. The gphoto2_version.py output suggests it's using libgphoto2 v2.5.27.1, so it's probably OK. It might be worth using ldd to check the python-gphoto2 binaries are linked to the correct libgphoto2 library.

Your mix of "object oriented" and "C" style is rather confusing. You also don't need context, unless you plan to use its callbacks (which don't seem useful to me). Try this:

import gphoto2 as gp

camera = gp.Camera()
camera.init()
config = camera.get_config()
abilities = camera.get_abilities()
camera.exit()

You don't actually need to call camera.exit() unless your program is going to carry on running and some other program needs to access the camera. I don't know if it would help to delete config before calling camera.exit().

jim-easterbrook commented 2 years ago

Forget what I said about deleting config, that's something I've suggested before and it shouldn't be relevant. (See last posts in this thread: https://github.com/gphoto/libgphoto2/issues/352)

gp_camera_exit() is definitely problematic with Canon cameras. I get PTP Device Busy warnings with my 100D rather than your [-1] Unspecified error but I suspect they are different manifestations of the same problem, which seems to be related to having called get_config.

jim-easterbrook commented 2 years ago

Can you enable gphoto2 logging - it might give us some clues.

    callback_obj = gp.check_result(gp.use_python_logging())
greiginsydney commented 2 years ago

Thanks Jim I'll follow those up.

I did a fresh rebuild yesterday afternoon using these steps (but I've kept the old memory card to go back to):

sudo apt-get install libgphoto2-dev -y
sudo pip3 install -v gphoto2

As an aside, gphoto has installed itself in /usr/local/lib/python3.7/dist-packages/gphoto2/ ... where it used to be in /usr/local/share/python-gphoto2/ I wasn't expecting that!

From only having done that, when I run gphoto2_version.py I now get this result, with libgphoto2 still back in the dark ages as expected, but python-gphoto2 has leapt to 2.3.0:

pi@BenchPi3BPlus:/usr/local/lib/python3.7/dist-packages/gphoto2/examples $ python3 gphoto2_version.py
python: 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0]
libgphoto2: ['2.5.22', 'all camlibs', 'gcc (C compiler used)', 'ltdl (for portable loading of camlibs)', 'EXIF (for special handling of EXIF files)']
libgphoto2_port: ['0.12.0', 'iolibs: disk ptpip serial usb1 usbdiskdirect usbscsi', 'gcc (C compiler used)', 'ltdl (for portable loading of iolibs)', 'USB (libusb1, for USB cameras)', 'serial (for serial cameras)', 'no resmgr (serial port access and locking)', 'no ttylock (serial port locking)', 'no lockdev (serial port locking)']
python-gphoto2: 2.3.0
pi@BenchPi3BPlus:/usr/local/lib/python3.7/dist-packages/gphoto2/examples $

... but 2.2.4 is your latest released build? Has 2.3.0 snuck out prematurely?

Too many variables have changed. This is getting messy. What say you?

- G.

greiginsydney commented 2 years ago

You don't actually need to call camera.exit() unless your program is going to carry on running and some other program needs to access the camera. I don't know if it would help to delete config before calling camera.exit().

My app is a web front-end, and so each time the script interacts with the camera I believe I need to clean up after myself (closing the connection) as there are multiple workers and multiple humans potentially all trying to talk to the one camera.

 

gp_camera_exit() is definitely problematic with Canon cameras.

Do you mean that camera.exit() is a more reliable / preferred way to clean up?

jim-easterbrook commented 2 years ago

In reverse order... camera.exit() and gp_camera_exit() are fundamentally the same - when I use either name I mean both functions.

Yes, you obviously do need camera.exit() in your case.

I released python-gphoto2 v2.3.0 a couple of days ago. It shouldn't make any difference to your problem as most of the changes are related to how I build it. (Now there are "binary wheels" for some Linux computers which mean you don't need to install libexiv2.)

The python-gphoto2 examples (and a few other files) used to be in /usr/local/share/python-gphoto2/, but the actual Python package was always in /usr/local/lib/python3.7/dist-packages/gphoto2/ or similar. Now I'm using setuptools instead of distutils (to allow use of wheels) I'm not able to install things in /usr/local/share.

When you upgrade libgphoto2, make sure pkg-config --cflags --libs libgphoto2 points to the new installation before you reinstall python-gphoto2. If the new libgphoto2 is in the same place as it was before then make sure there aren't old iolibs and camlibs hanging around. (They're in subdirectories of the directory containing the libgpphoto2 library.)

If you increase logging verbosity by adding this line to your test script

logging.basicConfig(level=logging.DEBUG)

you might get some information showing what libgphoto2 was doing just before the unspecified error occurs.

jim-easterbrook commented 2 years ago

PS python-gphoto2 has recently been added to the packages available on piwheels.org, so after you've updated libgphoto2 you'll need to use the --no-binary option to make pip ignore any wheels it finds. I'm about to update the README to make this clearer.

greiginsydney commented 2 years ago

Thanks. I'm still chugging through my issue & trying to get on top of the linkings and interaction.

In the updated docos you wrote:

...but make sure that the pkg-config --cflags --libs libgphoto2 command shows where you've installed your build.

What I don't understand are the implications / significance of the output. For example, here's mine at the moment:

pi@BenchPi3BPlus:~ $ pkg-config --cflags --libs libgphoto2
-I/usr/include/gphoto2 -I/usr/include/libexif -I/usr/include/gphoto2 -lgphoto2 -lm -lgphoto2_port -lm
pi@BenchPi3BPlus:~ $

BTW, it's probably just minor housekeeping, but the Releases page on GitHub hasn't been updated to reflect 2.3.0 as the latest.

jim-easterbrook commented 2 years ago

The pkg-config output shows the header (.h) files are in /usr/include/gphoto2, which is a standard installation, so the library files (.so) will be in /usr/lib or similar. When you install libgphoto2 manually (i.e. not from the OS package manager) it usually ends up under /usr/local/.

Good point about the GitHub releases page - I forgot to do that bit.

msmeissn commented 2 years ago

was a bug in 2.5.27.1, should be fixed now

greiginsydney commented 2 years ago

It turns out there's a build of libgphoto2-dev 2.5.27 for Debian/Raspbian 'Bullseye', so I'll wait for that to be released for the Pi (which can't be far off).

- G.