diff options
author | Nick White <git@njw.name> | 2019-03-26 15:21:43 +0000 |
---|---|---|
committer | Nick White <git@njw.name> | 2019-03-26 15:21:43 +0000 |
commit | 7d4cc9f7dd297c08e02447dda1f8aad9db0b0768 (patch) | |
tree | ba8ff4f4165d07557de96fcbda6da079f587a3e6 /lib/hocr/lines.go | |
parent | cd1fb1c9f6e1384ac0add8904425e6f92b17a704 (diff) |
Better error handling with hocr lines
Diffstat (limited to 'lib/hocr/lines.go')
-rw-r--r-- | lib/hocr/lines.go | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/lib/hocr/lines.go b/lib/hocr/lines.go index c60a619..20c6550 100644 --- a/lib/hocr/lines.go +++ b/lib/hocr/lines.go @@ -7,6 +7,7 @@ import ( "image" "image/png" "io/ioutil" + "log" "os" "path/filepath" "strings" @@ -72,9 +73,11 @@ func parseLineDetails(h Hocr, i image.Image, name string) (line.Details, error) ln.Avgconf = (totalconf / float64(num)) / 100 ln.Text = getLineText(l) ln.OcrName = name - var imgd line.ImgDirect - imgd.Img = i.(*image.Gray).SubImage(image.Rect(coords[0], coords[1], coords[2], coords[3])) - ln.Img = imgd + if i != nil { + var imgd line.ImgDirect + imgd.Img = i.(*image.Gray).SubImage(image.Rect(coords[0], coords[1], coords[2], coords[3])) + ln.Img = imgd + } lines = append(lines, ln) } return lines, nil @@ -93,15 +96,17 @@ func GetLineDetails(hocrfn string) (line.Details, error) { return newlines, err } + var img image.Image pngfn := strings.Replace(hocrfn, ".hocr", ".png", 1) pngf, err := os.Open(pngfn) if err != nil { - return newlines, err - } - defer pngf.Close() - img, err := png.Decode(pngf) - if err != nil { - return newlines, err + log.Println("Warning: can't open image %s\n", pngfn) + } else { + defer pngf.Close() + img, err = png.Decode(pngf) + if err != nil { + log.Println("Warning: can't load image %s\n", pngfn) + } } n := strings.Replace(filepath.Base(hocrfn), ".hocr", "", 1) |