paddle ocr suport

This commit is contained in:
m
2026-07-12 12:19:19 +02:00
parent f303ac8b5c
commit 571a571db6
8 changed files with 197 additions and 4 deletions
+67
View File
@@ -8,6 +8,8 @@ import (
"io"
"net/http"
"time"
"github.com/ledongthuc/pdf"
)
type Client struct {
@@ -25,6 +27,18 @@ func NewClient(endpoint string) *Client {
}
func (c *Client) Recognize(imageData []byte) ([]TextBlock, error) {
docType := DetectDocumentType(imageData)
switch docType {
case PDFScanned:
fmt.Println("PDFScanned -> PaddleOCR")
case Image:
fmt.Println("Image -> PaddleOCR")
default:
return []TextBlock{}, fmt.Errorf("DOCTYPE not supported => %s", docType)
}
b64 := base64.StdEncoding.EncodeToString(imageData)
reqBody, err := json.Marshal(OCRRequest{Image: b64})
@@ -63,6 +77,59 @@ func (c *Client) Recognize(imageData []byte) ([]TextBlock, error) {
return flattenResults(ocrResp.Result), nil
}
type DocType string
const (
Image DocType = "image"
PDFText DocType = "pdf_text"
PDFScanned DocType = "pdf_scanned"
Unknown DocType = "unknown"
)
func isPDFText(data []byte) bool {
reader := bytes.NewReader(data)
r, err := pdf.NewReader(reader, int64(len(data)))
if err != nil {
return false
}
for i := 1; i <= r.NumPage(); i++ {
page := r.Page(i)
if page.V.IsNull() {
continue
}
text, _ := page.GetPlainText(nil)
if len(text) > 20 {
return true
}
}
return false
}
func DetectDocumentType(data []byte) DocType {
mime := http.DetectContentType(data)
switch {
case mime == "application/pdf":
if isPDFText(data) {
return PDFText
}
return PDFScanned
case bytes.HasPrefix(data, []byte{0xFF, 0xD8}): // JPEG
return Image
case bytes.HasPrefix(data, []byte{0x89, 0x50, 0x4E, 0x47}): // PNG
return Image
default:
return Unknown
}
}
func (c *Client) HealthCheck() error {
resp, err := c.httpClient.Get(c.endpoint + "/health")
if err != nil {
+1 -1
View File
@@ -22,7 +22,7 @@ func GenerateFileDownloadUrl(fileID string) string {
sig := sign(fileID, expires, secret)
url := fmt.Sprintf(
"http://192.168.1.142:8080/api/v1/files/%s?expires=%d&sig=%s",
"http://192.168.1.17:8080/api/v1/files/%s?expires=%d&sig=%s",
fileID,
expires,
sig,