babalae/better-genshin-impact · error · FileNotFoundException

Yap字典文件不存在

Error message

Yap字典文件不存在

What it means

PickTextInference (the Yap-based pickup text recognizer) maps ONNX output indices to characters using Assets\Model\Yap\index_2_word.json. The constructor throws FileNotFoundException when that dictionary file is absent, after which the Yap inference engine cannot be created.

Source

Thrown at BetterGenshinImpact/Core/Recognition/ONNX/SVTR/PickTextInference.cs:31

using Newtonsoft.Json;

namespace BetterGenshinImpact.Core.Recognition.ONNX.SVTR;

/// <summary>
///     来自于 Yap 的拾取文字识别
///     https://github.com/Alex-Beng/Yap
/// </summary>
public class PickTextInference : ITextInference
{
    private readonly InferenceSession _session;
    private readonly Dictionary<int, string> _wordDictionary;

    public PickTextInference()
    {
        _session = App.ServiceProvider.GetRequiredService<BgiOnnxFactory>().CreateInferenceSession(BgiOnnxModel.YapModelTraining,true);

        var wordJsonPath = Global.Absolute(@"Assets\Model\Yap\index_2_word.json");
        if (!File.Exists(wordJsonPath)) throw new FileNotFoundException("Yap字典文件不存在", wordJsonPath);

        var json = File.ReadAllText(wordJsonPath);
        _wordDictionary = JsonConvert.DeserializeObject<Dictionary<int, string>>(json) ??
                          throw new Exception("index_2_word.json deserialize failed");
    }

    public string Inference(Mat mat)
    {
        long startTime = Stopwatch.GetTimestamp();
        // 将输入数据调整为 (1, 1, 32, 384) 形状的张量
        var reshapedInputData  = OcrUtils.ToTensorYapDnn(mat, out var owner);

        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;

        using (owner)
        {
            // 创建输入 NamedOnnxValue, 运行模型推理
            results = _session.Run([NamedOnnxValue.CreateFromTensor("input", reshapedInputData)]);

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Restore index_2_word.json under Assets\Model\Yap\.
  2. Confirm Global.Absolute(@"Assets\Model\Yap\index_2_word.json") resolves to the real file location.
  3. Ensure the Yap folder and the YapModelTraining .onnx (registered in BgiOnnxModel) are both deployed together.
  4. Guard construction with a File.Exists check and report the missing asset path.

Example fix

// before
var inf = new PickTextInference(); // throws if json missing

// after
var dictPath = Global.Absolute(@"Assets\Model\Yap\index_2_word.json");
if (!File.Exists(dictPath)) throw new InvalidOperationException($"Yap dictionary missing: {dictPath}");
var inf = new PickTextInference();
Defensive patterns

Strategy: validation

Validate before calling

var dictPath = Global.Absolute(@"Assets\Model\Yap\index_2_word.json");
if (!File.Exists(dictPath))
    throw new InvalidOperationException($"Yap dictionary missing: {dictPath}");
var inference = new PickTextInference();

Try / catch

try
{
    _yap = new PickTextInference();
}
catch (FileNotFoundException ex) when (ex.Message.Contains("Yap字典文件不存在"))
{
    Logger.LogError(ex, "Yap dictionary missing; pickup text OCR disabled.");
    _yap = null;
}

Prevention

When it happens

Trigger: Constructing new PickTextInference() when Assets\Model\Yap\index_2_word.json does not exist at Global.Absolute's resolved location.

Common situations: Yap model assets not shipped with the build; directory casing mismatch on a case-sensitive filesystem; assets moved without updating the hardcoded path; clean checkout missing the binary asset.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/e22c92a735f44f57. Report an issue: GitHub.