Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

1.0 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"time"

	"github.com/siyuan-note/siyuan/kernel/ocr"
)

func main() {
	var request ocr.WorkerRequest
	err := json.NewDecoder(io.LimitReader(os.Stdin, 16384)).Decode(&request)
	if err == nil && request.Config.Worker != "" {
		err = fmt.Errorf("recursive OCR worker is forbidden")
	}
	if err == nil {
		provider := &ocr.PaddleProvider{Config: func() ocr.PaddleConfig { return request.Config }}
		defer provider.Close()
		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
		defer cancel()
		var rows []map[string]string
		if request.Image == "" {
			err = provider.Validate(ctx)
			rows = []map[string]string{}
		} else {
			rows, err = provider.Recognize(ctx, request.Image)
		}
		if err == nil {
			err = json.NewEncoder(os.Stdout).Encode(rows)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}