paddle ocr suport
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user