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
+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