fix: parse charset as JSON array instead of line-by-line
This commit is contained in:
+13
-14
@@ -1,9 +1,9 @@
|
|||||||
package captcha
|
package captcha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"math"
|
"math"
|
||||||
@@ -254,26 +254,25 @@ func ctcDecode(output []float32, charset []string) string {
|
|||||||
|
|
||||||
func (h *Handler) loadCharset() ([]string, error) {
|
func (h *Handler) loadCharset() ([]string, error) {
|
||||||
charsetPath := filepath.Join(h.modelPath, "CharSets.txt")
|
charsetPath := filepath.Join(h.modelPath, "CharSets.txt")
|
||||||
file, err := os.Open(charsetPath)
|
|
||||||
|
// 读取整个文件
|
||||||
|
data, err := os.ReadFile(charsetPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
charset := make([]string, 0, 6000)
|
// 解析 JSON 数组格式
|
||||||
scanner := bufio.NewScanner(file)
|
var charset []string
|
||||||
for scanner.Scan() {
|
if err := json.Unmarshal(data, &charset); err != nil {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
return nil, fmt.Errorf("解析字符集失败: %v", err)
|
||||||
if line != "" {
|
|
||||||
charset = append(charset, line)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]string, len(charset)+1)
|
// 确保第一个元素是空字符串(CTC blank)
|
||||||
result[0] = ""
|
if len(charset) == 0 || charset[0] != "" {
|
||||||
copy(result[1:], charset)
|
charset = append([]string{""}, charset...)
|
||||||
|
}
|
||||||
|
|
||||||
return result, scanner.Err()
|
return charset, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===================== Math 数学计算 =====================
|
// ===================== Math 数学计算 =====================
|
||||||
|
|||||||
Reference in New Issue
Block a user