Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Recognize text from picture
-
@JonB, thanks, very nice. I have noted and wondered about how difficult number 1 is to recognize... Not very exotic, is it? But in my experiments it looked like the simple heuristic of ”if the result is something else than 1-9, assume it is a 1” would work pretty well for Sudoku.
-
@JonB, can you open up this one a little bit?
approx_num_ch=(r.boundingBox().size.width*9)
-
The *9 is because if the initial rectangle detection and crop works, then each square is approx 1/9 width. So the approx number of squares a rectangle covers tells us how many characters it should have... I was getting many cases where 1 got read as Te, or some other two character value, even though the width was less than one box... so I wanted to have special handling for narrow boxes, as that is probably a 1, while wide boxes could have multiple characters because the bounding box legitimately spans adjacent boxes.
-
@JonB, there’s something in the math here I do not quite get. I would expect something like:
num_char = r.bbox/(full_bbox/9) = r.bbox * 9 / full_bbox
Thus looks like you are missing the division?
-
The results of vision are always provided as normalized coordinates — meaning the full box is always 1.
For drawing, you have to then multiply by image width/height.Since the perspective correction both fixes perspective and crops — 1/9 is the size, roughly, of a single cell.
-
@JonB, now I understand, thank you.
-
I have a quick question in regards to the original ocr post how do I print the text as one single csv list, I tried but I have been getting a list of lists instead of one single list
This is a snippet of the code example I think needs to be altered
success = handler.performRequests_error_([req], None) if success: for result in req.results(): print(result.text()) else: print('Problem recognizing anything')
-
results=[str(result.text()) for result in req.results()] print(results)
Or maybe
print(','.join(results))
-
There is a lot of good code here... It would be really awesome if there was a
GitHub repo
to stitch it all together into an app. -
-
Hi,
I'm aware that this thread is about a year old. But maybe someone can nevertheless alighten me. I'm trying to do a similar thing in JavaScript for Automation (JXA), and I see this line in your example:for result in req.results(): print(result.text())
translated to JavaScript, that's
results.forEach(r => { console.log(r.text); })
and that works like a charm. I'm just wondering why, since according to Apple's documentation, the
results
object doesn't even have atext
property, onlystring
(cf. https://developer.apple.com/documentation/vision/vnrecognizedtext?language=objc)I was first wondering if
text
is perhaps a nice Python thing, but since the same works in JavaScript, I'm sure that I'm missing something obvious in Apple's documentation. Does anyone know what (and where I should be looking)?Thanks a lot in advance
Christian -
Does
string
not work?Often there are undocumented or decrecates features available in objc objects. Often we just poke around using autocomplete (which ultimately uses some of the introspection objc features of the objc runtime (which let you get a list of methods or instance vars, etc)
-
Does string not work?
It does, but only in a very convoluted way, like so:
results.forEach(r => { console.log(r.topCandidates(1).js[0].string.js) })
The
js
in the middle is required to convert the ObjC array returned bytopCandidates
to a JavaScript array (and again to convert the NSString returned bystring
to a JS string). But usingstring
directly atr
does not work.we just poke around using autocomplete
I gues that happens in XCode (the poking around)?
-
No, the exploration happens in pythonista, in the console. Once you have an object, dir(variable) lists the methods and such, or frankly just typing a letter and autocomplete suggestions does it's thing.
If you're not using a bridging library like
https://github.com/TooTallNate/NodObjC
I'd suggest that you do, since it might take care of a lot of the annoying bits like converting every type to js equivalents, and let's you access some of the dynamic introspection stuff that makes objc pretty neat.Under the hood, there are objc runtime functions that let you get lists of method names. For instance,see
https://github.com/jsbain/objc_hacks/blob/master/print_objc.py
For how you can do it in python. Or, look at the NodObjC code for class.js and core.js -- it looks like it does something similar, using the objc copy_methodsList, etc, and adds those as J's callable functions to the prototype. Then your favorite J's debugger ought to show you what is there... -
In this case, looking at the headers for VNTextObservation shows the
text
attribute. -
Thanks a lot for that. Apple's documentation doesn't mention any of these properties :-(
-
This post is deleted! -
How do I take the recognized stuff and make a mathematical problem to solve out of it?
-
Hello,
thanks for your subject. I made an app 4 yeard ago with Pythonista and i found nothing to recognize from camera the 15 number of a bingo card like this one.
My app still working with writing manually the number.i will try now to update it with text recognition and see if it can work with your code.
On this example what i need as a result is : (18,37,55,62,89,9,13,36,48,77,5,26,58,66,80)
I think i can have some issues with the 'noise' around the number i need (text outside the tab or the repetition in small character). Is it possible to make it first see the tab and only look inside each row and column and not outside ?Hope we can do something robust :)
-
@Attirail that will be a little tricky because of the small and large numbers in each box. Detecting the outer box probably won't be too bad, then if you have known grid dimensions I'd think you could then find the grid without much trouble. But then you'd have to essentially only grab a specific region of each grid.