【问题标题】:gphoto2 how to handle Out of Focus warninggphoto2如何处理失焦警告
【发布时间】:2020-06-02 21:29:31
【问题描述】:

这是我用相机拍摄图像的方法

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())

如果相机无法对焦,则会产生以下错误

...
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')

异常错误代码为-1,但如何捕获Out of Focus 警告?

更新

从日志中过滤掉不必要的错误

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())

【问题讨论】:

    标签: python python-logging libgphoto2


    【解决方案1】:

    您已经定义了一个回调函数,您可以在其中解析错误字符串以检测失焦,但您尚未安装回调,因此 libgphoto2 没有使用它。使用gp_log_add_func 安装您的回调。

    另外,您将camera.capture 的返回值传递给gp.check_result。这是不正确的,因为camera.capture 已经检查了结果,如果出现错误,则会引发异常。

    【讨论】:

    • 感谢您注意到我实际上并没有回电。但是我们为什么要分配回调对象呢?有什么用吗?我编辑了我的代码;通过错误代码过滤回调消息是减少错误日志的好方法吗?
    • 回调函数是从 libgphoto2 接收日志消息的唯一方法。您想知道您的相机是否报告了“失焦”,因此您需要一个解析消息的回调来检测这一点。
    • 我明白了,我需要在函数内分配回调对象
    【解决方案2】:

    如果你能弄清楚记录器的名称,你可以将你自己的处理程序添加到它并进行你自己的日志处理。

    使用您的相机库名称调用“getLogger”将使您获得记录器。

    使用自定义处理程序在该记录器上调用 AddHandler 将允许您对来自该记录器的日志进行自己的日志处理。

    请看Python's Logging Cookbook for more info

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 2013-03-30
      • 2016-04-15
      • 2012-09-09
      • 1970-01-01
      • 2017-08-23
      • 2015-01-30
      • 2011-01-23
      相关资源
      最近更新 更多