mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-12 04:48:02 -05:00
* feat(ocr): enhance OCR processing with structured results and hOCR support * Update ocr/google_docai_provider.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update ocr/google_docai_provider_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * refactor(tests): remove unused context import from google_docai_provider_test.go * refactor: Add defensive checks for language code in Google DocAI provider (#226) * Update ocr/google_docai_provider.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update ocr/google_docai_provider.go Co-authored-by: gardar <gardar@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: mkrinke <mad.krinke@googlemail.com> Co-authored-by: gardar <gardar@users.noreply.github.com>
57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// ProcessDocumentOCR processes a document through OCR and returns the combined text
|
|
func (app *App) ProcessDocumentOCR(ctx context.Context, documentID int) (string, error) {
|
|
docLogger := documentLogger(documentID)
|
|
docLogger.Info("Starting OCR processing")
|
|
|
|
imagePaths, err := app.Client.DownloadDocumentAsImages(ctx, documentID, limitOcrPages)
|
|
defer func() {
|
|
for _, imagePath := range imagePaths {
|
|
if err := os.Remove(imagePath); err != nil {
|
|
docLogger.WithError(err).WithField("image_path", imagePath).Warn("Failed to remove temporary image file")
|
|
}
|
|
}
|
|
}()
|
|
if err != nil {
|
|
return "", fmt.Errorf("error downloading document images for document %d: %w", documentID, err)
|
|
}
|
|
|
|
docLogger.WithField("page_count", len(imagePaths)).Debug("Downloaded document images")
|
|
|
|
var ocrTexts []string
|
|
for i, imagePath := range imagePaths {
|
|
pageLogger := docLogger.WithField("page", i+1)
|
|
pageLogger.Debug("Processing page")
|
|
|
|
imageContent, err := os.ReadFile(imagePath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error reading image file for document %d, page %d: %w", documentID, i+1, err)
|
|
}
|
|
|
|
result, err := app.ocrProvider.ProcessImage(ctx, imageContent)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error performing OCR for document %d, page %d: %w", documentID, i+1, err)
|
|
}
|
|
if result == nil {
|
|
pageLogger.Error("Got nil result from OCR provider")
|
|
return "", fmt.Errorf("error performing OCR for document %d, page %d: nil result", documentID, i+1)
|
|
}
|
|
|
|
pageLogger.WithField("has_hocr", result.HOCR != "").
|
|
WithField("metadata", result.Metadata).
|
|
Debug("OCR completed for page")
|
|
|
|
ocrTexts = append(ocrTexts, result.Text)
|
|
}
|
|
|
|
docLogger.Info("OCR processing completed successfully")
|
|
return strings.Join(ocrTexts, "\n\n"), nil
|
|
}
|