71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|
ocr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ocr/v20181119"
|
|
"tencent_ocr/pkg/errors"
|
|
)
|
|
|
|
type OCRService struct {
|
|
client *ocr.Client
|
|
tencentSecretID string
|
|
tencentSecretKey string
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func NewOCRService(tencentSecretID, tencentSecretKey string) (*OCRService, error) {
|
|
credential := common.NewCredential(tencentSecretID, tencentSecretKey)
|
|
cpf := profile.NewClientProfile()
|
|
cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com"
|
|
|
|
client, err := ocr.NewClient(credential, "", cpf)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to create Tencent Cloud OCR client")
|
|
}
|
|
|
|
return &OCRService{
|
|
client: client,
|
|
tencentSecretID: tencentSecretID,
|
|
tencentSecretKey: tencentSecretKey,
|
|
}, nil
|
|
}
|
|
|
|
func (s *OCRService) ProcessImage(ctx context.Context, imageBase64 string) (string, error) {
|
|
if imageBase64 == "" {
|
|
return "", errors.NewClientError("image data is required")
|
|
}
|
|
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
// Create OCR request
|
|
request := ocr.NewGeneralHandwritingOCRRequest()
|
|
request.ImageBase64 = common.StringPtr(imageBase64)
|
|
|
|
// Perform OCR
|
|
response, err := s.client.GeneralHandwritingOCRWithContext(ctx, request)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to perform OCR")
|
|
}
|
|
|
|
// Extract text from OCR response
|
|
var ocrText string
|
|
for _, textDetection := range response.Response.TextDetections {
|
|
ocrText += *textDetection.DetectedText + "\n"
|
|
}
|
|
|
|
return ocrText, nil
|
|
}
|
|
|
|
// Close implements graceful shutdown
|
|
func (s *OCRService) Close() error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
// Add any cleanup logic here if needed
|
|
return nil
|
|
}
|