Skip to content

Instantly share code, notes, and snippets.

@twinsant
Created November 9, 2021 05:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twinsant/8737fa611fe71b4ab436b43605b71776 to your computer and use it in GitHub Desktop.
Save twinsant/8737fa611fe71b4ab436b43605b71776 to your computer and use it in GitHub Desktop.
Pythonista Text OCR example.
import photos
import objc_util
VNImageRequestHandler = objc_util.ObjCClass('VNImageRequestHandler')
VNRecognizeTextRequest = objc_util.ObjCClass('VNRecognizeTextRequest')
def text_ocr(asset):
img_data = objc_util.ns(asset.get_image_data().getvalue())
with objc_util.autoreleasepool():
req = VNRecognizeTextRequest.alloc().init().autorelease()
# print(req.supportedRecognitionLanguagesAndReturnError_(None))
req.setRecognitionLanguages_(['zh-Hans'])
req.setRecognitionLevel_(0)
handler = VNImageRequestHandler.alloc().initWithData_options_(img_data, None).autorelease()
success = handler.performRequests_error_([req], None)
if success:
for result in req.results():
print(result.text())
def main():
all_assets = photos.get_assets()
asset = photos.pick_asset(assets=all_assets)
if asset is None:
return
text_ocr(asset)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment