Ocr updates, fixed issue where cthulhu could say ocr failed when it didn't, and cthulhu only cares about if the word boundary changes when clicking
This commit is contained in:
1 parent
069adb62df
commit
5c919016bb
5 files changed
+68
-26
No files matched your search
@@ -202,6 +202,13 @@ func (session *ocrSession) validateActivation(ctx context.Context, request ocrAc
|
||||
if request.capture.Pixels == nil {
|
||||
return ocr.ErrSourceChanged
|
||||
}
|
||||
rectangle, ok := request.capture.Transform.ScreenRectangle(request.word.Bounds)
|
||||
if !ok {
|
||||
return ocr.ErrSourceChanged
|
||||
}
|
||||
// OCR bounds can be scaled. Map through screen coordinates back into the
|
||||
// original capture so unrelated animation cannot invalidate a stable word.
|
||||
bounds := rectangle.Sub(request.capture.ScreenBounds.Min).Add(request.capture.Pixels.Bounds().Min)
|
||||
if request.job.kind == ocr.ObjectSource {
|
||||
var zone *atspi.Rectangle
|
||||
if request.job.source.HasZone {
|
||||
@@ -215,17 +222,16 @@ func (session *ocrSession) validateActivation(ctx context.Context, request ocrAc
|
||||
fresh, err := session.capture.Capture(ctx, request.capture.Request)
|
||||
if err != nil || fresh.SourceWindow != request.capture.SourceWindow || fresh.RootBounds != request.capture.RootBounds ||
|
||||
fresh.SourceBounds != request.capture.SourceBounds || fresh.ScreenBounds != request.capture.ScreenBounds ||
|
||||
!equalOCRPixels(ctx, fresh.Pixels, request.capture.Pixels) {
|
||||
!equalOCRPixels(ctx, fresh.Pixels, request.capture.Pixels, bounds) {
|
||||
return ocr.ErrSourceChanged
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func equalOCRPixels(ctx context.Context, first, second image.Image) bool {
|
||||
if first == nil || second == nil || first.Bounds() != second.Bounds() {
|
||||
func equalOCRPixels(ctx context.Context, first, second image.Image, bounds image.Rectangle) bool {
|
||||
if first == nil || second == nil || first.Bounds() != second.Bounds() || bounds.Empty() || !bounds.In(first.Bounds()) {
|
||||
return false
|
||||
}
|
||||
bounds := first.Bounds()
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
if y%32 == 0 && ctx.Err() != nil {
|
||||
return false
|
||||
|
||||
@@ -122,16 +122,22 @@ func (confirm *ocrTestConfirmer) Confirm(_ context.Context, word string) (bool,
|
||||
return confirm.answer, nil
|
||||
}
|
||||
|
||||
func TestOCRActivationChecksFreshPixelsBeforeClick(t *testing.T) {
|
||||
func TestOCRActivationChecksTargetPixelsBeforeClick(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
command input.Command
|
||||
changed bool
|
||||
change image.Point
|
||||
blocked bool
|
||||
scale int
|
||||
}{
|
||||
{"object/unchanged", input.OCRObject, false},
|
||||
{"object/changed", input.OCRObject, true},
|
||||
{"inaccessible-window/unchanged", input.OCRWindow, false},
|
||||
{"inaccessible-window/changed", input.OCRWindow, true},
|
||||
{"object/unchanged", input.OCRObject, image.Pt(-1, -1), false, 1},
|
||||
{"object/outside-word", input.OCRObject, image.Pt(0, 0), false, 1},
|
||||
{"object/inside-word", input.OCRObject, image.Pt(10, 10), true, 1},
|
||||
{"inaccessible-window/unchanged", input.OCRWindow, image.Pt(-1, -1), false, 1},
|
||||
{"inaccessible-window/outside-word", input.OCRWindow, image.Pt(0, 0), false, 1},
|
||||
{"inaccessible-window/inside-word", input.OCRWindow, image.Pt(29, 19), true, 1},
|
||||
{"scaled/outside-word", input.OCRWindow, image.Pt(30, 20), false, 2},
|
||||
{"scaled/inside-word", input.OCRWindow, image.Pt(10, 10), true, 2},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
session, keys, capture, engine, output := ocrTestSession(t)
|
||||
@@ -142,13 +148,15 @@ func TestOCRActivationChecksFreshPixelsBeforeClick(t *testing.T) {
|
||||
clicker := &ocrTestClicker{points: make(chan image.Point, 1)}
|
||||
confirmer := &ocrTestConfirmer{answer: true, called: make(chan string, 1)}
|
||||
session.clicker, session.confirmer = clicker, confirmer
|
||||
paint := color.RGBA{R: 20, A: 255}
|
||||
changed := false
|
||||
capture.produce = func(ctx context.Context, request ocr.CaptureRequest) (ocr.Capture, error) {
|
||||
pixels := image.NewRGBA(image.Rect(0, 0, 100, 50))
|
||||
for index := range pixels.Pix {
|
||||
pixels.Pix[index] = 20
|
||||
}
|
||||
pixels.SetRGBA(0, 0, paint)
|
||||
if changed && test.change.In(pixels.Bounds()) {
|
||||
pixels.SetRGBA(test.change.X, test.change.Y, color.RGBA{R: 30, A: 255})
|
||||
}
|
||||
bounds := image.Rect(10, 20, 110, 70)
|
||||
return ocr.Capture{CaptureHeader: ocr.CaptureHeader{Request: request, RootBounds: image.Rect(0, 0, 200, 200), SourceBounds: bounds, ScreenBounds: bounds, SourceWindow: request.Foreground}, Pixels: pixels,
|
||||
Transform: ocr.Transform{ImageContent: image.Rect(0, 0, 100, 50), ScreenBounds: bounds, HasScreenMapping: true}}, ctx.Err()
|
||||
@@ -157,19 +165,17 @@ func TestOCRActivationChecksFreshPixelsBeforeClick(t *testing.T) {
|
||||
ocrTestReceive(t, keys.calls)
|
||||
ocrTestReceive(t, capture.calls)
|
||||
ocrTestReceive(t, engine.calls)
|
||||
engine.results <- ocr.Result{Text: "go", Width: 100, Height: 50, Words: []ocr.Word{{Text: "go", Bounds: image.Rect(10, 10, 30, 20), Span: ocr.Span{Start: 0, End: 2}}}}
|
||||
engine.results <- ocr.Result{Text: "go", Width: 100 * test.scale, Height: 50 * test.scale, Words: []ocr.Word{{Text: "go", Bounds: image.Rect(10*test.scale, 10*test.scale, 30*test.scale, 20*test.scale), Span: ocr.Span{Start: 0, End: 2}}}}
|
||||
ocrTestReceive(t, keys.calls)
|
||||
ocrTestReceive(t, output.speech)
|
||||
ocrTestReceive(t, output.frames)
|
||||
if test.changed {
|
||||
paint = color.RGBA{R: 30, A: 255}
|
||||
}
|
||||
changed = true
|
||||
session.Handle(input.CommandEvent{Command: input.OCRActivate, Timestamp: 101})
|
||||
if got := ocrTestReceive(t, confirmer.called); got != "go" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
ocrTestReceive(t, capture.calls)
|
||||
if test.changed {
|
||||
if test.blocked {
|
||||
if got := ocrTestReceive(t, output.speech); got.Text != "OCR source changed; refresh before activation" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
|
||||
@@ -105,13 +105,17 @@ func TestOCRAutomationTargetRejectsMissingResult(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestOCRAutomationTargetChecksPixelsAndMapping(t *testing.T) {
|
||||
for _, mode := range []string{"current", "stale", "release", "hover", "dispatch"} {
|
||||
for _, mode := range []string{"current", "stale", "outside", "release", "release-outside", "hover", "dispatch"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
session, keys, capture, engine, output := ocrTestSession(t)
|
||||
paint := byte(20)
|
||||
capture.produce = func(ctx context.Context, request ocr.CaptureRequest) (ocr.Capture, error) {
|
||||
pixels := image.NewRGBA(image.Rect(0, 0, 100, 50))
|
||||
pixels.Pix[0] = paint
|
||||
point := image.Pt(10, 10)
|
||||
if mode == "outside" || mode == "release-outside" {
|
||||
point = image.Pt(0, 0)
|
||||
}
|
||||
pixels.Pix[pixels.PixOffset(point.X, point.Y)] = paint
|
||||
bounds := image.Rect(10, 20, 110, 70)
|
||||
return ocr.Capture{CaptureHeader: ocr.CaptureHeader{Request: request, RootBounds: image.Rect(0, 0, 200, 200), SourceBounds: bounds, ScreenBounds: bounds, SourceWindow: request.Foreground}, Pixels: pixels, Transform: ocr.Transform{ImageContent: pixels.Bounds(), ScreenBounds: bounds, HasScreenMapping: true}}, ctx.Err()
|
||||
}
|
||||
@@ -123,12 +127,12 @@ func TestOCRAutomationTargetChecksPixelsAndMapping(t *testing.T) {
|
||||
ocrTestReceive(t, keys.calls)
|
||||
ocrTestReceive(t, output.speech)
|
||||
ocrTestReceive(t, output.frames)
|
||||
if mode == "stale" {
|
||||
if mode == "stale" || mode == "outside" {
|
||||
paint = 30
|
||||
}
|
||||
point, err := session.AutomationTarget(context.Background(), 42)
|
||||
|
||||
if mode == "release" || mode == "hover" || mode == "dispatch" {
|
||||
if mode == "release" || mode == "release-outside" || mode == "hover" || mode == "dispatch" {
|
||||
backend := &automationRecorder{window: 42}
|
||||
if mode == "dispatch" {
|
||||
session.tentacleBindings = func() []input.BindingOverride {
|
||||
@@ -140,7 +144,7 @@ func TestOCRAutomationTargetChecksPixelsAndMapping(t *testing.T) {
|
||||
t.Fatal("OCR swallowed the application command")
|
||||
}
|
||||
}
|
||||
if mode == "release" {
|
||||
if mode == "release" || mode == "release-outside" {
|
||||
backend.onRelease = func() { paint = 30 }
|
||||
} else if mode == "hover" {
|
||||
backend.onRoute = func() { paint = 30 }
|
||||
@@ -148,7 +152,7 @@ func TestOCRAutomationTargetChecksPixelsAndMapping(t *testing.T) {
|
||||
runtime := &tentacleRuntime{automation: backend, ocr: session, output: &tentacleOutput{gate: newFocusGate(nil)}}
|
||||
err := runtime.runAutomation(context.Background(), tentacleCommand{automationAllowed: true, application: "ck3", window: 42}, tentaclewire.Automation{Actions: []tentaclewire.AutomationAction{{Kind: "ocr-route"}, {Kind: "click", Button: 1}}})
|
||||
want := []string{"release"}
|
||||
if mode == "hover" || mode == "dispatch" {
|
||||
if mode == "release-outside" || mode == "hover" || mode == "dispatch" {
|
||||
want = append(want, "route", "click")
|
||||
}
|
||||
if (err != nil) != (mode == "release") || !reflect.DeepEqual(backend.calls, want) {
|
||||
|
||||
+5
-2
@@ -84,11 +84,14 @@ func ParseTSV(input io.Reader, width, height int) (Result, error) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(wordText) == "" {
|
||||
// Tesseract can include surrounding spaces in a word (for example,
|
||||
// " Play"). Normalize text before assembling spans; keep its bounds.
|
||||
wordText = strings.TrimSpace(wordText)
|
||||
if wordText == "" {
|
||||
continue
|
||||
}
|
||||
bounds := image.Rect(left, top, left+boxWidth, top+boxHeight)
|
||||
if !validWordBounds(bounds, width, height) || strings.TrimSpace(wordText) != wordText {
|
||||
if !validWordBounds(bounds, width, height) {
|
||||
return Result{}, invalidRow(rows)
|
||||
}
|
||||
separatorBytes := 0
|
||||
|
||||
@@ -54,6 +54,30 @@ func TestParseTSVEmptyAndQuotedWords(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTSVTrimsWordWhitespace(t *testing.T) {
|
||||
for _, word := range []string{" Play", "Play ", " Play ", "\u00a0Play\u00a0"} {
|
||||
t.Run(word, func(t *testing.T) {
|
||||
data := tsvHeader + "\n" +
|
||||
"5\t1\t1\t1\t1\t1\t0\t0\t10\t10\t90\té\n" +
|
||||
"5\t1\t1\t1\t1\t2\t10\t0\t10\t10\t95\t \u00a0 \n" +
|
||||
"5\t1\t1\t1\t1\t3\t26\t20\t46\t16\t0\t" + word + "\n" +
|
||||
"5\t1\t1\t1\t2\t1\t0\t40\t60\t16\t90\tTutorial\n"
|
||||
result, err := ParseTSV(strings.NewReader(data), 100, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Text != "é Play\nTutorial" || len(result.Words) != 3 || len(result.Lines) != 2 {
|
||||
t.Fatalf("wrong trimmed document: %+v", result)
|
||||
}
|
||||
if result.Words[1] != (Word{"Play", 0, image.Rect(26, 20, 72, 36), Span{2, 6}}) ||
|
||||
result.Words[2].Span != (Span{7, 15}) || result.Lines[0].Span != (Span{0, 6}) ||
|
||||
result.Lines[1].Span != (Span{7, 15}) {
|
||||
t.Fatalf("lost coordinates or Unicode offsets: %+v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTSVRejectsMalformedOutputAtomically(t *testing.T) {
|
||||
row := "5\t1\t1\t1\t1\t1\t0\t0\t10\t10\t90\tPrivate fixture text"
|
||||
for _, bad := range []string{
|
||||
@@ -71,7 +95,6 @@ func TestParseTSVRejectsMalformedOutputAtomically(t *testing.T) {
|
||||
row + "\x00",
|
||||
row + "\xff",
|
||||
row + "\textra field",
|
||||
row + " ",
|
||||
} {
|
||||
result, err := ParseTSV(strings.NewReader(tsvHeader+"\n"+row+"\n"+bad+"\n"), 100, 100)
|
||||
if !errors.Is(err, ErrInvalidOutput) || result.Text != "" || len(result.Words) != 0 {
|
||||
|
||||
Reference in new issue
Block a user