Files
BordroRobot/app/lib/ocr/visionapi.go
2024-03-30 15:35:53 +03:00

68 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ocr
import (
"context"
"fmt"
"os"
"google.golang.org/api/option"
vision "cloud.google.com/go/vision/apiv1"
)
var credsPath = "C:/Users/huseyin/Desktop/project/notitek/BordroRobot/app/lib/ocr/readpdf-75de5191c083.json"
func detectText(filePath string) ([]string, error) {
ctx := context.Background()
client, err := vision.NewImageAnnotatorClient(ctx, option.WithCredentialsFile(credsPath))
if err != nil {
return nil, fmt.Errorf("ImageAnnotatorClient oluşturulamadı: %v", err)
}
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("Dosya açılamadı: %v", err)
}
defer f.Close()
image, err := vision.NewImageFromReader(f)
if err != nil {
return nil, fmt.Errorf("Resim oluşturulamadı: %v", err)
}
annotations, err := client.DetectTexts(ctx, image, nil, 10)
if err != nil {
return nil, fmt.Errorf("Metin tespit edilemedi: %v", err)
}
res := make([]string, 0)
if len(annotations) == 0 {
return nil, fmt.Errorf("Metin tespit edilemedi: %v", err)
} else {
fmt.Println("Metin:")
for _, annotation := range annotations {
// fmt.Println(annotation)
res = append(res, annotation.Description)
}
}
return res, nil
}
func VisionApi(path string) ([]string, error) {
filePath := path // Örnek bir dosya yolu
resp, err := detectText(filePath)
if err != nil {
fmt.Printf("Metin tespit edilemedi: %v\n", err)
return nil, err
}
for _, txt := range resp {
fmt.Println(txt)
}
return resp, nil
}