feat(ocr): 新增本地 OCR sidecar

This commit is contained in:
yml
2026-11-17 17:26:59 +08:00
parent a6eb6c95b9
commit 90addb80d5
166 changed files with 72858 additions and 34 deletions
+2
View File
@@ -21,6 +21,7 @@ type Config struct {
FieldEncryptionLegacyKey string
ExternalUploadSecret string
ExternalUploadAllowedIPs []string
OCRServiceURL string
BootstrapAdminUsername string
BootstrapAdminPassword string
BootstrapAdminNickname string
@@ -105,6 +106,7 @@ func Load() Config {
FieldEncryptionLegacyKey: fieldEncryptionLegacyKey(getEnv("APP_ENV", "development")),
ExternalUploadSecret: getEnv("EXTERNAL_UPLOAD_SECRET", ""),
ExternalUploadAllowedIPs: getEnvList("EXTERNAL_UPLOAD_ALLOWED_IPS"),
OCRServiceURL: strings.TrimRight(strings.TrimSpace(getEnv("OCR_SERVICE_URL", "")), "/"),
BootstrapAdminUsername: getEnv("ADMIN_BOOTSTRAP_USERNAME", ""),
BootstrapAdminPassword: getEnv("ADMIN_BOOTSTRAP_PASSWORD", ""),
BootstrapAdminNickname: getEnv("ADMIN_BOOTSTRAP_NICKNAME", "超级管理员"),
+7
View File
@@ -111,6 +111,13 @@ func TestLoadDatabaseConfigInvalidValuesUseDefaults(t *testing.T) {
}
}
func TestLoadOCRServiceURL(t *testing.T) {
t.Setenv("OCR_SERVICE_URL", " http://ocr:8787/ ")
if got := Load().OCRServiceURL; got != "http://ocr:8787" {
t.Fatalf("OCRServiceURL = %q, want http://ocr:8787", got)
}
}
// TestFieldEncryptionLegacyKeyNonProductionDefaultsToHistoric 验证非生产环境未设置 legacy 时
// 回退到历史硬编码密钥(开发态零配置兼容旧密文)。
func TestFieldEncryptionLegacyKeyNonProductionDefaultsToHistoric(t *testing.T) {
@@ -103,7 +103,7 @@ func (h *Handler) GetQrCodeStatsHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": stats})
}
// RecognizeQrCodeGroupNameHandler 同步调用 PaddleOCR API 识别二维码图片中的企业微信群名(保留向后兼容)
// RecognizeQrCodeGroupNameHandler 同步识别二维码图片中的企业微信群名(保留向后兼容)
func (h *Handler) RecognizeQrCodeGroupNameHandler(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
@@ -120,7 +120,7 @@ func (h *Handler) RecognizeQrCodeGroupNameHandler(c *gin.Context) {
)
if err != nil {
if errors.Is(err, ErrQrCodeOCRNotConfigured) {
c.JSON(http.StatusBadRequest, gin.H{"error": "请先在系统配置中填写 PaddleOCR API Token"})
c.JSON(http.StatusBadRequest, gin.H{"error": "二维码 OCR 未配置"})
return
}
if errors.Is(err, ErrQrCodeOCRInvalidFile) {
@@ -129,7 +129,7 @@ func (h *Handler) RecognizeQrCodeGroupNameHandler(c *gin.Context) {
}
if errors.Is(err, ErrQrCodeOCRUnavailable) {
response.RecordError(c, err)
c.JSON(http.StatusBadGateway, gin.H{"error": "PaddleOCR 服务暂时不可用"})
c.JSON(http.StatusBadGateway, gin.H{"error": "OCR 服务暂时不可用"})
return
}
response.RecordError(c, err)
@@ -140,7 +140,7 @@ func (h *Handler) RecognizeQrCodeGroupNameHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": result})
}
// SubmitOCRJobHandler 提交 OCR 任务到 PaddleOCR,立即返回任务 ID
// SubmitOCRJobHandler 提交 OCR 任务,立即返回任务 ID
func (h *Handler) SubmitOCRJobHandler(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
@@ -157,7 +157,7 @@ func (h *Handler) SubmitOCRJobHandler(c *gin.Context) {
)
if err != nil {
if errors.Is(err, ErrQrCodeOCRNotConfigured) {
c.JSON(http.StatusBadRequest, gin.H{"error": "请先在系统配置中填写 PaddleOCR API Token"})
c.JSON(http.StatusBadRequest, gin.H{"error": "二维码 OCR 未配置"})
return
}
if errors.Is(err, ErrQrCodeOCRInvalidFile) {
@@ -166,7 +166,7 @@ func (h *Handler) SubmitOCRJobHandler(c *gin.Context) {
}
if errors.Is(err, ErrQrCodeOCRUnavailable) {
response.RecordError(c, err)
c.JSON(http.StatusBadGateway, gin.H{"error": "PaddleOCR 服务暂时不可用"})
c.JSON(http.StatusBadGateway, gin.H{"error": "OCR 服务暂时不可用"})
return
}
if errors.Is(err, ErrQrCodeOCRRedisDisabled) {
+236 -18
View File
@@ -57,7 +57,7 @@ const (
)
var (
ErrQrCodeOCRNotConfigured = errors.New("二维码 OCR Token 未配置")
ErrQrCodeOCRNotConfigured = errors.New("二维码 OCR 未配置")
ErrQrCodeOCRInvalidFile = errors.New("无效的二维码图片")
ErrQrCodeOCRUnavailable = errors.New("二维码 OCR 服务不可用")
ErrQrCodeOCRJobNotFound = errors.New("OCR 任务不存在或已过期")
@@ -177,6 +177,25 @@ type paddleOCRJobStatusResponse struct {
Error string `json:"error"`
}
type localOCRLine struct {
Text string `json:"text"`
Score float64 `json:"score"`
X1 float64 `json:"x1"`
Y1 float64 `json:"y1"`
X2 float64 `json:"x2"`
Y2 float64 `json:"y2"`
X3 float64 `json:"x3"`
Y3 float64 `json:"y3"`
X4 float64 `json:"x4"`
Y4 float64 `json:"y4"`
}
type localOCRResponse struct {
OK bool `json:"ok"`
Result []localOCRLine `json:"result"`
Error string `json:"error"`
}
func readAndValidateOCRFile(reader io.Reader, contentType string) ([]byte, string, error) {
data, err := io.ReadAll(io.LimitReader(reader, maxQrCodeOCRFileSize+1))
if err != nil || len(data) == 0 || len(data) > maxQrCodeOCRFileSize {
@@ -254,8 +273,16 @@ func (r *Repository) getOCRJobRecord(ctx context.Context, jobID string) (*OCRJob
return &record, nil
}
// RecognizeQrCodeGroupName 同步调用 PaddleOCR 识别二维码图片中的群名(保留向后兼容)。
// RecognizeQrCodeGroupName 同步识别二维码图片中的群名(保留向后兼容)。
func (r *Repository) RecognizeQrCodeGroupName(ctx context.Context, filename, contentType string, reader io.Reader) (*QrCodeOCRResult, error) {
data, contentType, err := readAndValidateOCRFile(reader, contentType)
if err != nil {
return nil, err
}
if r.hasOCRService() {
return r.recognizeLocalQrCodeGroupName(ctx, data)
}
config, err := r.GetQrCodeOCRConfig(ctx)
if err != nil {
return nil, err
@@ -263,10 +290,6 @@ func (r *Repository) RecognizeQrCodeGroupName(ctx context.Context, filename, con
if strings.TrimSpace(config.Token) == "" {
return nil, ErrQrCodeOCRNotConfigured
}
data, contentType, err := readAndValidateOCRFile(reader, contentType)
if err != nil {
return nil, err
}
paddleJobID, err := r.submitPaddleOCRJob(ctx, config, filename, contentType, data)
if err != nil {
@@ -292,11 +315,32 @@ func (r *Repository) RecognizeQrCodeGroupName(ctx context.Context, filename, con
}, nil
}
// SubmitOCRJob 提交 OCR 任务到 PaddleOCR,立即返回任务 ID 供前端轮询。
// SubmitOCRJob 提交 OCR 任务,立即返回任务 ID 供前端轮询。
func (r *Repository) SubmitOCRJob(ctx context.Context, filename, contentType string, reader io.Reader) (string, error) {
if r.redis == nil {
return "", ErrQrCodeOCRRedisDisabled
}
data, contentType, err := readAndValidateOCRFile(reader, contentType)
if err != nil {
return "", err
}
jobID := uuid.NewString()
if r.hasOCRService() {
result, err := r.recognizeLocalQrCodeGroupName(ctx, data)
if err != nil {
return "", err
}
record := &OCRJobRecord{
Status: ocrJobStatusDone,
Result: result,
}
if err := r.saveOCRJobRecord(ctx, jobID, record); err != nil {
return "", err
}
return jobID, nil
}
config, err := r.GetQrCodeOCRConfig(ctx)
if err != nil {
return "", err
@@ -304,17 +348,12 @@ func (r *Repository) SubmitOCRJob(ctx context.Context, filename, contentType str
if strings.TrimSpace(config.Token) == "" {
return "", ErrQrCodeOCRNotConfigured
}
data, contentType, err := readAndValidateOCRFile(reader, contentType)
if err != nil {
return "", err
}
paddleJobID, err := r.submitPaddleOCRJob(ctx, config, filename, contentType, data)
if err != nil {
return "", err
}
jobID := uuid.NewString()
record := &OCRJobRecord{
PaddleJobID: paddleJobID,
Status: ocrJobStatusSubmitted,
@@ -325,6 +364,83 @@ func (r *Repository) SubmitOCRJob(ctx context.Context, filename, contentType str
return jobID, nil
}
func (r *Repository) hasOCRService() bool {
return r != nil && strings.TrimSpace(r.ocrServiceURL) != ""
}
func (r *Repository) recognizeLocalQrCodeGroupName(ctx context.Context, jpegData []byte) (*QrCodeOCRResult, error) {
ppmData, err := encodeOCRServicePPM(jpegData)
if err != nil {
return nil, ErrQrCodeOCRInvalidFile
}
endpoint := strings.TrimRight(r.ocrServiceURL, "/") + "/api/ocr"
respBody, err := r.doPaddleRequest(ctx, "local", func(ctx context.Context) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(ppmData))
if err == nil {
req.Header.Set("Content-Type", "image/x-portable-pixmap")
req.Header.Set("Accept", "application/json")
}
return req, err
})
if err != nil {
return nil, err
}
var payload localOCRResponse
if err := json.Unmarshal(respBody, &payload); err != nil {
upstreamErr := newOCRUpstreamError("local_decode", http.StatusOK, nil, respBody, err)
r.logOCRUpstreamFailure(ctx, upstreamErr, 1, true, 0)
return nil, upstreamErr
}
if !payload.OK {
message := strings.TrimSpace(payload.Error)
if message == "" {
message = "local OCR service returned ok=false"
}
upstreamErr := newOCRUpstreamError("local_result", http.StatusOK, nil, respBody, errors.New(message))
r.logOCRUpstreamFailure(ctx, upstreamErr, 1, true, 0)
return nil, upstreamErr
}
rawText, candidates := parseLocalOCRGroupNames(payload.Result)
groupName := ""
if len(candidates) > 0 {
groupName = candidates[0]
}
return &QrCodeOCRResult{
GroupName: groupName,
Candidates: candidates,
RawText: rawText,
}, nil
}
func encodeOCRServicePPM(jpegData []byte) ([]byte, error) {
source, _, err := image.Decode(bytes.NewReader(jpegData))
if err != nil {
return nil, err
}
bounds := source.Bounds()
width := bounds.Dx()
height := bounds.Dy()
if width <= 0 || height <= 0 {
return nil, ErrQrCodeOCRInvalidFile
}
var buffer bytes.Buffer
buffer.Grow(width*height*3 + 32)
if _, err := fmt.Fprintf(&buffer, "P6\n%d %d\n255\n", width, height); err != nil {
return nil, err
}
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
pixel := color.RGBAModel.Convert(source.At(x, y)).(color.RGBA)
buffer.WriteByte(pixel.R)
buffer.WriteByte(pixel.G)
buffer.WriteByte(pixel.B)
}
}
return buffer.Bytes(), nil
}
// PollOCRJobResult 轮询 OCR 任务状态,每次调用向 PaddleOCR 查询一次并更新缓存。
func (r *Repository) PollOCRJobResult(ctx context.Context, jobID string) (*OCRJobRecord, error) {
record, err := r.getOCRJobRecord(ctx, jobID)
@@ -654,9 +770,9 @@ func (r *Repository) logOCRUpstreamFailure(ctx context.Context, upstreamErr *ocr
fields = append(fields, zap.Error(upstreamErr.Cause))
}
if final {
r.ocrLogger(ctx).Error("PaddleOCR 请求失败", fields...)
r.ocrLogger(ctx).Error("OCR 请求失败", fields...)
} else {
r.ocrLogger(ctx).Warn("PaddleOCR 请求失败,准备重试", fields...)
r.ocrLogger(ctx).Warn("OCR 请求失败,准备重试", fields...)
}
}
@@ -817,8 +933,8 @@ func collectPaddleOCRText(value any, texts *[]string) {
}
}
func parseQrCodeGroupNameCandidates(rawText string) []string {
excluded := []*regexp.Regexp{
var (
qrCodeGroupNameBoilerplatePatterns = []*regexp.Regexp{
regexp.MustCompile(`使用.*微信`),
regexp.MustCompile(`企业微信`),
regexp.MustCompile(`扫码`),
@@ -828,8 +944,110 @@ func parseQrCodeGroupNameCandidates(rawText string) []string {
regexp.MustCompile(`天内`),
regexp.MustCompile(`月\d+日`),
regexp.MustCompile(`前`),
regexp.MustCompile(`^\d+$`),
}
legacyQRCodeGroupNameExcludedPatterns = append(
append([]*regexp.Regexp(nil), qrCodeGroupNameBoilerplatePatterns...),
regexp.MustCompile(`^\d+$`),
)
)
func isQrCodeGroupNameBoilerplate(text string) bool {
for _, pattern := range qrCodeGroupNameBoilerplatePatterns {
if pattern.MatchString(text) {
return true
}
}
return false
}
type localOCRGroupNameCandidate struct {
text string
score float64
distance float64
order int
}
func parseLocalOCRGroupNames(lines []localOCRLine) (string, []string) {
texts := make([]string, 0, len(lines))
instructionY := math.MaxFloat64
for _, line := range lines {
text := normalizeQrCodeOCRLine(line.Text)
if text == "" {
continue
}
texts = append(texts, text)
if isQrCodeGroupNameBoilerplate(text) &&
(strings.Contains(text, "扫码") || strings.Contains(text, "加入") || strings.Contains(text, "使用")) {
if y := localOCRLineCenterY(line); y < instructionY {
instructionY = y
}
}
}
candidates := make([]localOCRGroupNameCandidate, 0, len(lines))
for order, line := range lines {
text := normalizeQrCodeOCRLine(line.Text)
length := utf8.RuneCountInString(text)
if length == 0 || length > 64 || line.Score < 0.45 || isQrCodeGroupNameBoilerplate(text) {
continue
}
y := localOCRLineCenterY(line)
distance := 0.0
if instructionY < math.MaxFloat64 {
if y >= instructionY-2 {
continue
}
distance = instructionY - y
}
candidates = append(candidates, localOCRGroupNameCandidate{
text: text,
score: line.Score,
distance: distance,
order: order,
})
}
sort.SliceStable(candidates, func(i, j int) bool {
if instructionY < math.MaxFloat64 && math.Abs(candidates[i].distance-candidates[j].distance) > 0.5 {
return candidates[i].distance < candidates[j].distance
}
if candidates[i].score != candidates[j].score {
return candidates[i].score > candidates[j].score
}
return candidates[i].order < candidates[j].order
})
rawText := strings.Join(texts, "\n")
names := make([]string, 0, len(candidates)+len(lines))
seen := make(map[string]struct{}, len(candidates))
add := func(text string) {
if text == "" {
return
}
if _, exists := seen[text]; exists {
return
}
seen[text] = struct{}{}
names = append(names, text)
}
for _, candidate := range candidates {
add(candidate.text)
}
for _, candidate := range parseQrCodeGroupNameCandidates(rawText) {
add(candidate)
}
return rawText, names
}
func localOCRLineCenterY(line localOCRLine) float64 {
values := []float64{line.Y1, line.Y2, line.Y3, line.Y4}
total := 0.0
for _, value := range values {
total += value
}
return total / float64(len(values))
}
func parseQrCodeGroupNameCandidates(rawText string) []string {
scores := map[string]int{}
for _, line := range strings.Split(rawText, "\n") {
line = normalizeQrCodeOCRLine(line)
@@ -840,7 +1058,7 @@ func parseQrCodeGroupNameCandidates(rawText string) []string {
continue
}
skip := false
for _, pattern := range excluded {
for _, pattern := range legacyQRCodeGroupNameExcludedPatterns {
if pattern.MatchString(line) {
skip = true
break
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
@@ -238,3 +239,77 @@ func TestCollectPaddleOCRJSONLTextSupportsPPOCRv6(t *testing.T) {
t.Fatalf("candidates = %v, want 火爆出租-778群 as first candidate", candidates)
}
}
func TestParseLocalOCRGroupNamesSupportsShortNumericAndChineseNames(t *testing.T) {
cases := []struct {
name string
title string
}{
{name: "纯数字", title: "111"},
{name: "中英数字", title: "小语对接h870"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
lines := []localOCRLine{
{Text: "C", Score: 0.76, Y1: 73, Y2: 73, Y3: 87, Y4: 87},
{Text: tc.title, Score: 0.95, Y1: 117, Y2: 117, Y3: 134, Y4: 134},
{Text: "使用微信或企业微信扫码加入", Score: 0.96, Y1: 163, Y2: 163, Y3: 177, Y4: 177},
{Text: "该二维码7天内(11月24日前)有效", Score: 0.99, Y1: 487, Y2: 487, Y3: 498, Y4: 498},
}
rawText, candidates := parseLocalOCRGroupNames(lines)
if len(candidates) == 0 || candidates[0] != tc.title {
t.Fatalf("candidates = %v, want %q; raw=%q", candidates, tc.title, rawText)
}
})
}
}
func TestRecognizeQrCodeGroupNameUsesLocalOCRService(t *testing.T) {
var gotContentType string
var gotPPMHeader string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotContentType = r.Header.Get("Content-Type")
body := new(bytes.Buffer)
_, _ = body.ReadFrom(r.Body)
if len(body.Bytes()) >= 2 {
gotPPMHeader = string(body.Bytes()[:2])
}
_ = json.NewEncoder(w).Encode(localOCRResponse{
OK: true,
Result: []localOCRLine{
{Text: "111", Score: 0.999996, Y1: 117, Y2: 117, Y3: 134, Y4: 134},
{Text: "使用微信或企业微信扫码加入", Score: 0.95, Y1: 163, Y2: 163, Y3: 177, Y4: 177},
},
})
}))
defer server.Close()
repo := NewRepository(nil, nil, nil,
WithOCRServiceURL(server.URL),
withOCRHTTPClient(server.Client()),
withOCRGate(newOCRRequestGate(1, 0)),
withOCRSleep(func(context.Context, time.Duration) error { return nil }),
)
imageData := testOCRPNG(t)
result, err := repo.RecognizeQrCodeGroupName(t.Context(), "group.png", "image/png", bytes.NewReader(imageData))
if err != nil {
t.Fatal(err)
}
if result.GroupName != "111" || !strings.Contains(result.RawText, "使用微信") {
t.Fatalf("result = %+v", result)
}
if gotContentType != "image/x-portable-pixmap" || gotPPMHeader != "P6" {
t.Fatalf("content-type=%q ppm-header=%q", gotContentType, gotPPMHeader)
}
}
func testOCRPNG(t *testing.T) []byte {
t.Helper()
source := image.NewRGBA(image.Rect(0, 0, 64, 96))
draw.Draw(source, source.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
var buffer bytes.Buffer
if err := png.Encode(&buffer, source); err != nil {
t.Fatal(err)
}
return buffer.Bytes()
}
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"strings"
"time"
"github.com/redis/go-redis/v9"
@@ -22,6 +23,7 @@ type Repository struct {
ocrHTTPClient *http.Client
ocrGate *ocrRequestGate
ocrSleep func(context.Context, time.Duration) error
ocrServiceURL string
}
const (
@@ -62,6 +64,12 @@ func withOCRSleep(sleep func(context.Context, time.Duration) error) RepositoryOp
}
}
func WithOCRServiceURL(rawURL string) RepositoryOption {
return func(repo *Repository) {
repo.ocrServiceURL = strings.TrimRight(strings.TrimSpace(rawURL), "/")
}
}
func NewRepository(db *gorm.DB, hub *chathub.Hub, redis *redis.Client, options ...RepositoryOption) *Repository {
repo := &Repository{
db: db,
+7 -1
View File
@@ -177,7 +177,13 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
}
var chatRepo *chat.Repository
if deps.DB != nil {
chatRepo = chat.NewRepository(deps.DB, chatHub, deps.Redis, chat.WithLogger(logger))
chatRepo = chat.NewRepository(
deps.DB,
chatHub,
deps.Redis,
chat.WithLogger(logger),
chat.WithOCRServiceURL(cfg.OCRServiceURL),
)
}
// 创建 chat 适配器用于 listing