下面是我用相机捕捉图像的方法
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())发布于 2020-02-19 12:21:45
您已经定义了一个回调函数,在这个函数中可以解析错误字符串以检测出焦点,但是还没有安装回调,因此libgphoto2没有使用它。使用gp_log_add_func安装回调。
此外,您还将camera.capture的返回值传递给gp.check_result。这是不正确的,因为camera.capture已经检查了结果,并在出现错误时引发异常。
发布于 2020-02-19 03:14:46
如果可以确定记录器的名称,则可以向其添加自己的处理程序并执行自己的日志处理。
用相机库的名字叫"getLogger“会让你成为记录器。
使用自定义处理程序在该记录器上调用AddHandler将允许您对来自该记录器的日志进行自己的日志处理。
希望这能有所帮助
https://stackoverflow.com/questions/60288032
复制相似问题