2

Here is how I capture image with my camera

import logging
import gphoto2 as gp
def main():
    def callback(level, domain, string, data=None):
        print('Callback: level =', level, ', domain =', domain, ', string =', string)
        if data:
            print('Callback data:', data)

    logging.basicConfig(
        format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING)
    callback_obj = gp.check_result(gp.use_python_logging())

    camera = gp.Camera()
    camera.init()
    try:
        camera_file_path = gp.check_result(camera.capture(gp.GP_CAPTURE_IMAGE))
    except gp.GPhoto2Error as ex:
        print("callback: ", ex, ex.code, ex.message, ex.string)
    camera.exit()

if __name__ == "__main__" : exit(main())

If the camera is not able to focus it generates following error

...
WARNING: gphoto2: (ptp_usb_getresp [usb.c:466]) PTP_OC 0x90c8 receiving resp failed: Out of Focus (0xa002)
WARNING: gphoto2: (camera_nikon_capture [library.c:3153]) 'ret' failed: 'Out of Focus' (0xa002)
WARNING: gphoto2: (gp_context_error) Out of Focus
WARNING: gphoto2: (gp_camera_capture [gphoto2-camera.c:1340]) 'camera->functions->capture (camera, type, path, context)' failed: -1
('callback: ', GPhoto2Error('[-1] Unspecified error',), -1, '[-1] Unspecified error', 'Unspecified error')

The exception error code is -1 but how can I capture Out of Focus warning?

UPDATE

Filtered out unnecessary errors from logs

import logging
import gphoto2 as gp
from datetime import datetime
def main():
    def callback(level, domain, string, data=None):
        err_codes = ("(0x2005)", "(0x2019)")
        if not string.decode().endswith(err_codes):
            print("[{0}] {1}: {2}".format(datetime.utcnow(), domain.decode(), string.decode()))
        if data:
            print('Callback data:', data)

    callback_obj = gp.check_result(gp.gp_log_add_func(gp.GP_LOG_ERROR, callback))
    camera = gp.Camera()
    try:
        camera.init()
    except gp.GPhoto2Error as err:
        exit(err.code)

    try:
        camera_file_path = camera.capture(gp.GP_CAPTURE_IMAGE)
    except gp.GPhoto2Error as err:
        exit(err.code)
    camera.exit()

if __name__ == "__main__" : exit(main())
phoxd
  • 1,546
  • 3
  • 12
  • 26

2 Answers2

1

You've defined a callback function, in which you could parse the error string to detect out of focus, but you haven't installed your callback so libgphoto2 isn't using it. Use gp_log_add_func to install your callback.

Also, you're passing the return value of camera.capture to gp.check_result. This is incorrect as camera.capture already checks the result and raises an exception if there's an error.

  • Thanks for noticing that I didn't actually callback. But why do we assign callback object? Is there any use? I edited my code; is filtering callback message by error code a good way to reduce error logs? – phoxd Feb 19 '20 at 23:37
  • A callback function is the only way to receive log messages from libgphoto2. You wanted to know if your camera had reported "out of focus", so you need a callback that parses the messages to detect this. – Jim Easterbrook Feb 20 '20 at 08:41
  • i see, i need to assign callback object when scoped inside a function – phoxd Feb 20 '20 at 17:42
0

If you can figure out the name of the logger, you can add your own handler to it and do your own log processing.

Calling "getLogger" with your camera library's name would get you the logger.

Calling AddHandler on that logger with a custom handler would allow you to do your own log processing for logs coming from that logger.

Please see Python's Logging Cookbook for more info

Hope this helps

Sam
  • 794
  • 1
  • 7
  • 26