From a010f26d670de26619ecea3cfb68918947d028f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Thu, 4 Nov 2021 13:05:27 -0300 Subject: [PATCH] fix ocr --- api/ocr/cli.go | 162 ++++++++++++++++++++++++++++++------------------- 1 file changed, 99 insertions(+), 63 deletions(-) diff --git a/api/ocr/cli.go b/api/ocr/cli.go index 62778bb..8644b25 100644 --- a/api/ocr/cli.go +++ b/api/ocr/cli.go @@ -3,8 +3,11 @@ package ocr import ( "encoding/xml" "go.digitalcircle.com.br/open/replaycli-go/api" + "log" ) +const ScreenShot = "ss" + type AltoString struct { Text string `xml:",chardata"` ID string `xml:"ID,attr"` @@ -14,7 +17,25 @@ type AltoString struct { HEIGHT int `xml:"HEIGHT,attr"` WC float64 `xml:"WC,attr"` CONTENT string `xml:"CONTENT,attr"` + RHPOS int `xml:"RHPOS,attr"` + RVPOS int `xml:"RVPOS,attr"` + RWIDTH int `xml:"RWIDTH,attr"` + RHEIGHT int `xml:"RHEIGHT,attr"` } + +func (a *AltoString) AbsCoords(q *Opts) { + nx := int(float64(a.HPOS)/q.Proportion) + q.X + ny := int(float64(a.VPOS)/q.Proportion) + q.Y + nw := int(float64(a.WIDTH) / q.Proportion) + nh := int(float64(a.HEIGHT) / q.Proportion) + log.Printf("Mapping: x: %v=>%v; y:%v=>%v; w:%v=>%v; h:%v=>%v;(Prop: %v,qx: %v, qy: %v)", a.HPOS, nx, a.VPOS, ny, a.WIDTH, nw, a.HEIGHT, nh, q.Proportion, q.X, q.Y) + a.RHPOS = nx + a.RVPOS = ny + a.RWIDTH = nw + a.RHEIGHT = nh + return +} + type AltoSP struct { Text string `xml:",chardata"` WIDTH int `xml:"WIDTH,attr"` @@ -22,48 +43,48 @@ type AltoSP struct { HPOS int `xml:"HPOS,attr"` } type AltoTextLine struct { - Text string `xml:",chardata"` - ID string `xml:"ID,attr"` - HPOS string `xml:"HPOS,attr"` - VPOS string `xml:"VPOS,attr"` - WIDTH string `xml:"WIDTH,attr"` - HEIGHT string `xml:"HEIGHT,attr"` - String []AltoString `xml:"String"` - SP []AltoSP `xml:"SP"` + Text string `xml:",chardata"` + ID string `xml:"ID,attr"` + HPOS string `xml:"HPOS,attr"` + VPOS string `xml:"VPOS,attr"` + WIDTH string `xml:"WIDTH,attr"` + HEIGHT string `xml:"HEIGHT,attr"` + String []*AltoString `xml:"String"` + SP []*AltoSP `xml:"SP"` } type AltoTextBlock struct { - Text string `xml:",chardata"` - ID string `xml:"ID,attr"` - HPOS int `xml:"HPOS,attr"` - VPOS int `xml:"VPOS,attr"` - WIDTH int `xml:"WIDTH,attr"` - HEIGHT int `xml:"HEIGHT,attr"` - TextLine []AltoTextLine `xml:"TextLine"` + Text string `xml:",chardata"` + ID string `xml:"ID,attr"` + HPOS int `xml:"HPOS,attr"` + VPOS int `xml:"VPOS,attr"` + WIDTH int `xml:"WIDTH,attr"` + HEIGHT int `xml:"HEIGHT,attr"` + TextLine []*AltoTextLine `xml:"TextLine"` } type AltoComposedBlock struct { - Text string `xml:",chardata"` - ID string `xml:"ID,attr"` - HPOS int `xml:"HPOS,attr"` - VPOS int `xml:"VPOS,attr"` - WIDTH int `xml:"WIDTH,attr"` - HEIGHT int `xml:"HEIGHT,attr"` - TextBlock []AltoTextBlock `xml:"TextBlock"` + Text string `xml:",chardata"` + ID string `xml:"ID,attr"` + HPOS int `xml:"HPOS,attr"` + VPOS int `xml:"VPOS,attr"` + WIDTH int `xml:"WIDTH,attr"` + HEIGHT int `xml:"HEIGHT,attr"` + TextBlock []*AltoTextBlock `xml:"TextBlock"` } type AltoPrintSpace struct { - Text string `xml:",chardata"` - HPOS int `xml:"HPOS,attr"` - VPOS int `xml:"VPOS,attr"` - WIDTH int `xml:"WIDTH,attr"` - HEIGHT int `xml:"HEIGHT,attr"` - ComposedBlock []AltoComposedBlock `xml:"ComposedBlock"` + Text string `xml:",chardata"` + HPOS int `xml:"HPOS,attr"` + VPOS int `xml:"VPOS,attr"` + WIDTH int `xml:"WIDTH,attr"` + HEIGHT int `xml:"HEIGHT,attr"` + ComposedBlock []*AltoComposedBlock `xml:"ComposedBlock"` } type AltoPage struct { - Text string `xml:",chardata"` - WIDTH int `xml:"WIDTH,attr"` - HEIGHT int `xml:"HEIGHT,attr"` - PHYSICALIMGNR string `xml:"PHYSICAL_IMG_NR,attr"` - ID string `xml:"ID,attr"` - PrintSpace AltoPrintSpace `xml:"PrintSpace"` + Text string `xml:",chardata"` + WIDTH int `xml:"WIDTH,attr"` + HEIGHT int `xml:"HEIGHT,attr"` + PHYSICALIMGNR string `xml:"PHYSICAL_IMG_NR,attr"` + ID string `xml:"ID,attr"` + PrintSpace *AltoPrintSpace `xml:"PrintSpace"` } type AltoDescription struct { Text string `xml:",chardata"` @@ -72,7 +93,7 @@ type AltoDescription struct { Text string `xml:",chardata"` FileName string `xml:"fileName"` } `xml:"sourceImageInformation"` - OCRProcessing struct { + OCRProcessing *struct { Text string `xml:",chardata"` ID string `xml:"ID,attr"` OcrProcessingStep struct { @@ -85,22 +106,22 @@ type AltoDescription struct { } `xml:"OCRProcessing"` } type AltoLayout struct { - Text string `xml:",chardata"` - Page AltoPage `xml:"Page"` + Text string `xml:",chardata"` + Page *AltoPage `xml:"Page"` } type Alto struct { - XMLName xml.Name `xml:"alto"` - Text string `xml:",chardata"` - Xmlns string `xml:"xmlns,attr"` - Xlink string `xml:"xlink,attr"` - Xsi string `xml:"xsi,attr"` - SchemaLocation string `xml:"schemaLocation,attr"` - Description AltoDescription `xml:"Description"` - Layout AltoLayout `xml:"Layout"` + XMLName xml.Name `xml:"alto"` + Text string `xml:",chardata"` + Xmlns string `xml:"xmlns,attr"` + Xlink string `xml:"xlink,attr"` + Xsi string `xml:"xsi,attr"` + SchemaLocation string `xml:"schemaLocation,attr"` + Description *AltoDescription `xml:"Description"` + Layout *AltoLayout `xml:"Layout"` } -func (a Alto) Strings() []AltoString { - ret := make([]AltoString, 0) +func (a *Alto) Strings() []*AltoString { + ret := make([]*AltoString, 0) for _, v := range a.Layout.Page.PrintSpace.ComposedBlock { for _, v1 := range v.TextBlock { for _, v2 := range v1.TextLine { @@ -112,24 +133,39 @@ func (a Alto) Strings() []AltoString { } return ret } +func (a *Alto) CalcAbsolute(q *Opts) { + for _, v := range a.Layout.Page.PrintSpace.ComposedBlock { + for _, v1 := range v.TextBlock { + for _, v2 := range v1.TextLine { + for _, v3 := range v2.String { + v3.AbsCoords(q) + } + } + } + } + +} type Opts struct { - Ver string `json:"ver,omitempty"` - X int `json:"x"` - Y int `json:"y"` - H int `json:"h"` - W int `json:"w"` - Blur float64 `json:"blur"` - Sharpen float64 `json:"sharpen"` - Resizew int `json:"resizew"` - Resizeh int `json:"resizeh"` - Lang string `json:"lang"` - Tempfile string `json:"tempfile"` - Dispid int `json:"d"` - Gray bool `json:"gray"` - Src string `json:"src"` - Bytes []byte `json:"bytes"` - AddRects bool `json:"add_rects"` + Ver string `json:"ver,omitempty"` + X int `json:"x"` + Y int `json:"y"` + H int `json:"h"` + W int `json:"w"` + Blur float64 `json:"blur"` + Sharpen float64 `json:"sharpen"` + Resizew int `json:"resizew"` + Resizeh int `json:"resizeh"` + Lang string `json:"lang"` + Tempfile string `json:"tempfile"` + Dispid int `json:"d"` + Gray bool `json:"gray"` + Src string `json:"src"` + Bytes []byte `json:"bytes"` + AddRects bool `json:"add_rects"` + Proportion float64 `json:"proportion"` + Encoding string `json:"encoding"` + Absolute bool `json:"absolute"` } type Cli struct {