Created
November 9, 2021 05:14
-
-
Save twinsant/8737fa611fe71b4ab436b43605b71776 to your computer and use it in GitHub Desktop.
Pythonista Text OCR example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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