|
| 1 | +/** |
| 2 | + * This is a sample Go program that demonstrates how to use the UniPDF library |
| 3 | + * to perform OCR on an image using an HTTP OCR service that returns HOCR formatted |
| 4 | + * output. The program parses the HOCR response and extracts word-level information |
| 5 | + * including bounding boxes and confidence scores. |
| 6 | + * |
| 7 | + * This example uses https://github.com/unidoc/ocrserver as the OCR service. |
| 8 | + * However, UniPDF's OCR API is designed to support other OCR services that accept |
| 9 | + * image uploads via HTTP and return text or HOCR formatted results. |
| 10 | + * |
| 11 | + * Run as: go run hocr_sample.go input.jpg |
| 12 | + */ |
| 13 | +package main |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "strconv" |
| 21 | + |
| 22 | + "github.com/stefanhengl/gohocr" |
| 23 | + "github.com/unidoc/unipdf/v4/common/license" |
| 24 | + "github.com/unidoc/unipdf/v4/ocr" |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + // Make sure to load your metered License API key prior to using the library. |
| 29 | + // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io |
| 30 | + err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) |
| 31 | + if err != nil { |
| 32 | + panic(err) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func main() { |
| 37 | + if len(os.Args) < 2 { |
| 38 | + fmt.Printf("Usage: go run hocr_sample.go input.jpg\n") |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + f, err := os.Open(os.Args[1]) |
| 43 | + if err != nil { |
| 44 | + fmt.Printf("Error opening file: %v\n", err) |
| 45 | + os.Exit(1) |
| 46 | + } |
| 47 | + defer f.Close() |
| 48 | + |
| 49 | + // Configure OCR service options. |
| 50 | + opts := ocr.OCROptions{ |
| 51 | + Url: "http://localhost:8080/file", |
| 52 | + Method: "POST", |
| 53 | + FileFieldName: "file", |
| 54 | + Headers: map[string]string{ |
| 55 | + "Accept": "application/json", |
| 56 | + }, |
| 57 | + FormFields: map[string]string{ |
| 58 | + "format": "hocr", |
| 59 | + }, |
| 60 | + TimeoutSeconds: 30, |
| 61 | + } |
| 62 | + |
| 63 | + // Create OCR client. |
| 64 | + client := ocr.NewHTTPOCRService(opts) |
| 65 | + |
| 66 | + result, err := client.ExtractText(context.Background(), f, "image.jpg") |
| 67 | + if err != nil { |
| 68 | + fmt.Printf("Error extracting text: %v\n", err) |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | + |
| 72 | + // Parse JSON response to extract the "result" field. |
| 73 | + var jsonObj map[string]interface{} |
| 74 | + if err := json.Unmarshal(result, &jsonObj); err != nil { |
| 75 | + fmt.Printf("Error parsing JSON response: %v\n", err) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + |
| 79 | + content, ok := jsonObj["result"].(string) |
| 80 | + if !ok { |
| 81 | + fmt.Printf("Error: result field is not a string\n") |
| 82 | + os.Exit(1) |
| 83 | + } |
| 84 | + fmt.Printf("Extracted text: %s\n", content) |
| 85 | + |
| 86 | + content, err = strconv.Unquote(content) |
| 87 | + if err != nil { |
| 88 | + fmt.Printf("Error unquoting content: %v\n", err) |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + |
| 92 | + contentBytes := []byte(content) |
| 93 | + |
| 94 | + data, err := gohocr.Parse(contentBytes) |
| 95 | + if err != nil { |
| 96 | + fmt.Printf("Error parsing HOCR data: %v\n", err) |
| 97 | + os.Exit(1) |
| 98 | + } |
| 99 | + |
| 100 | + for _, v := range data.Words { |
| 101 | + fmt.Printf("Word: %s, Title: %f\n", v.Content, v.Title) |
| 102 | + } |
| 103 | +} |
0 commit comments