Using OCR text() on a match from Screen.capture()

Asked by Nano Stray

I would like to take a screenshot of a moving object to get its (x,y) coordinates and then read the words inside that object

So far, using the Finder Class has helped me to reliably capture the screen and then analyze the image

capturedImage = capture(SOME_REGION)
f = Finder(capturedImage)
f.findAll(SOME_IMAGE)
while f.hasNext():
    temp = f.next()
    print "the x coordinate of the match is ", temp.x

but now how do I read the words near the matching objects with the text() function from the screenshot that I had originally captured?

I am aware that Region(x,y,w,h).text() would work but that is not going to be for the screenshot that is currently being analyzed

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

This worked for me with 2.0.5:

img = "img.png"
reg = Region(0,0,300, 300)
capturedImage = SCREEN.capture(reg) #comment1
f = Finder(capturedImage)
f.findAll(img)
while f.hasNext():
    temp = f.next()
    print "match:", temp
print Image(capturedImage).getSub(temp).text() #comment2

comment1:
this returns the ScreenImage inMemory (does not save to file)
so it can be easily used later to search in

comment 2:
temp is a match in an image, so the match coord are relative to the top left corner of the image - hence (0,0)
so getSub cuts out the pixel area represented by match temp.
The text features work the same as for Region with class Image

Revision history for this message
Nano Stray (nanostray) said :
#2

Perfect!! It worked!!

Thank you very much Dr.RaiMan

------------------------------------------------------

For other readers out there you may substitute temp for another Region if the text you're trying to analyze is not exactly inside the match found

  print Image(capturedImage).getSub(temp).text() #comment2

to
  newRegion = Region(temp.x + customX, temp.y + customY, customW, customH)
  print Image(capturedImage).getSub().text(newRegion) #comment2

Revision history for this message
Nano Stray (nanostray) said :
#3