
--------------------------------------------------------------------------------
-- Captcha Recognizer ONNX 模块
--------------------------------------------------------------------------------

---@type onnxruntime*
local _onnxruntime = require("onnxruntime")

---@type image_cv*
local _image_cv = require("image.cv")

---@alias captcha_recognizer_image_input string|ImageObject*

---@class captcha_recognizer_options*
---@field type? 'slider' 验证码类型
---@field model_path? string 模型路径
---@field model_dir? string 模型目录
---@field providers? string[] ONNX Runtime provider 列表
---@field fallback_to_cpu? boolean provider 失败时回退 CPU
---@field coreml_compute_units? 'all'|'cpu_only'|'cpu_and_gpu'|'cpu_and_neural_engine' CoreML 计算单元
---@field coreml_enable_on_subgraph? boolean 是否在子图启用 CoreML
---@field coreml_require_static_input_shapes? boolean 是否要求静态输入形状
---@field coreml_create_mlprogram? boolean 是否创建 MLProgram

---@class captcha_recognizer_predict_options*
---@field confidence? number 置信度阈值
---@field conf? number 置信度阈值
---@field iou? number NMS IoU 阈值
---@field imgsz? integer|{ [1]: integer, width?: integer, w?: integer } 输入尺寸
---@field input_size? integer 输入尺寸
---@field size? integer 输入尺寸

---@class captcha_recognizer_identify_options*
---@field confidence? number 置信度阈值
---@field conf? number 置信度阈值
---@field iou? number NMS IoU 阈值
---@field show? boolean 是否返回预览图
---@field y_iou? number Y 轴 IoU 阈值
---@field mask_compare_size? integer 掩膜比较尺寸

---@class captcha_recognizer_box*
---@field [1] number 左边界
---@field [2] number 上边界
---@field [3] number 右边界
---@field [4] number 下边界
---@field [5]? number 置信度
---@field [6]? integer 类别 ID

---@class captcha_recognizer_detection*
---@field box number[] 检测框
---@field score number 置信度
---@field class_id integer 类别 ID
---@field mask onnxruntime_mask_bitmap* 分割掩膜

---@class captcha_recognizer_prediction_batch*
---@field [1] captcha_recognizer_box*[] 检测框列表
---@field [2] onnxruntime_mask_bitmap*[] 掩膜列表
---@field boxes captcha_recognizer_box*[] 检测框列表
---@field masks onnxruntime_mask_bitmap*[] 掩膜列表

---@class captcha_recognizer_model_info*
---@field mode? 'slider' 模式
---@field model_path? string 模型路径
---@field used_coreml? boolean 是否使用 CoreML EP
---@field input_name? string 输入名
---@field input_shape? integer[] 输入形状
---@field providers? string[] provider 列表
---@field outputs? { name: string, shape: integer[] }[] 输出信息

---@class captcha_recognizer_instance*
---@field closed boolean 是否已关闭
---@field session onnxruntime_session* ONNX Runtime 会话
---@field output_names string[] 输出名
---@field model_info captcha_recognizer_model_info* 模型信息
---@field decoder_schema table 解码 schema
local captcha_recognizer_instance = {}

---@class captcha_recognizer*
---@field VERSION string 模块版本
---@field AUTHOR string 作者
---@field Slider fun(opts?: captcha_recognizer_options*): captcha_recognizer_instance* 创建滑块识别实例
---@type captcha_recognizer*
local M = {}

--- 创建验证码识别实例
---@param opts? captcha_recognizer_options* 选项
---@return captcha_recognizer_instance* instance 实例
function M.new(opts) end

--- 创建滑块识别实例
---@param opts? captcha_recognizer_options* 选项
---@return captcha_recognizer_instance* instance 实例
function M.Slider(opts) end

--- 获取模块版本
---@return string version 模块版本
function M.version() end

--- 获取可用 ONNX Runtime provider
---@return string[] providers provider 列表
function M.providers() end

--- 运行滑块检测模型
---@param input captcha_recognizer_image_input 图片路径、图片数据或图片对象
---@param conf? number|captcha_recognizer_predict_options* 置信度阈值或选项
---@param iou? number NMS IoU 阈值
---@param imgsz? integer|table 输入尺寸
---@return captcha_recognizer_prediction_batch*[] result 预测 batch
function captcha_recognizer_instance:predict(input, conf, iou, imgsz) end

--- 识别滑块缺口位置
---@param input captcha_recognizer_image_input 图片路径、图片数据或图片对象
---@param conf? number|captcha_recognizer_identify_options* 置信度阈值或选项
---@param iou? number NMS IoU 阈值
---@param show? boolean 是否返回预览图
---@return captcha_recognizer_box* box 检测框，失败时为空表
---@return number confidence 置信度
---@return ImageObject*? preview 预览图
function captcha_recognizer_instance:identify(input, conf, iou, show) end

--- 识别滑块横向偏移量
---@param input captcha_recognizer_image_input 图片路径、图片数据或图片对象
---@param conf? number|captcha_recognizer_identify_options* 置信度阈值或选项
---@param iou? number NMS IoU 阈值
---@param show? boolean 是否返回预览图
---@return number offset 横向偏移量
---@return number confidence 置信度
---@return ImageObject*? preview 预览图
function captcha_recognizer_instance:identify_offset(input, conf, iou, show) end

--- 获取模型信息
---@return captcha_recognizer_model_info* info 模型信息
function captcha_recognizer_instance:get_model_info() end

--- 关闭实例
function captcha_recognizer_instance:close() end

return M
