Package: winocr
WinOCRInstallationpipinstallwinocrFull installpipinstallwinocr[all]UsagePillowThe language to be recognized can be specified by the lang parameter (second argument).importwinocrfromPILimportImageimg=Image.open('test.jpg')(awaitwinocr.recognize_pil(img,'ja')).textOpenCVimportwinocrimportcv2img=cv2.imread('test.jpg')(awaitwinocr.recognize_cv2(img,'ja')).textConnect to local runtime on ColaboratoryCreate a local connection by followingthese instructions.pipinstalljupyterlabjupyter_http_over_wsjupyterserverextensionenable--pyjupyter_http_over_wsjupyternotebook--NotebookApp.allow_origin='https://colab.research.google.com'--ip=0.0.0.0--port=8888--NotebookApp.port_retries=0Also available on Jupyter / Jupyter Lab.REPLimportcv2fromwinocrimportrecognize_cv2_syncimg=cv2.imread('testocr.png')recognize_cv2_sync(img)['text']'This is a lot of 12 point text to test the ocr code and see if it works on all types of file format. The quick brown dog jumped over the lazy fox. The quick brown dog jumped over the lazy fox. The quick brown dog jumped over the lazy fox. The quick brown dog jumped over the lazy fox.'fromPILimportImagefromwinocrimportrecognize_pil_syncimg=Image.open('testocr.png')recognize_pil_sync(img)['text']'This is a lot of 12 point text to test the ocr code and see if it works on all types of file format. The quick brown dog jumped over the lazy fox. The quick brown dog jumped over the lazy fox. The quick brown dog jumped over the lazy fox. The quick brown dog jumped over the lazy fox.'Multi-ProcessingfromPILimportImageimportconcurrent.futuresfromwinocrimportrecognize_pil_syncimages=[Image.open('testocr.png')foriinrange(1000)]withconcurrent.futures.ProcessPoolExecutor()asexecutor:results=list(executor.map(recognize_pil_sync,images))print(results)Web APIRun serverpipinstallwinocr[api]winocr_servecurlcurllocalhost:8000?lang=ja--data-binary@test.jpgPythonimportrequestsbytes=open('test.jpg','rb').read()requests.post('http://localhost:8000/?lang=ja',bytes).json()['text']You can run OCR with the Colaboratory runtime with./ngrok http 8000fromPILimportImagefromioimportBytesIOimg=Image.open('test.jpg')# Preprocessingbuf=BytesIO()img.save(buf,format='JPEG')requests.post('https://15a5fabf0d78.ngrok.io/?lang=ja',buf.getvalue()).json()['text']importcv2importrequestsimg=cv2.imread('test.jpg')# Preprocessingrequests.post('https://15a5fabf0d78.ngrok.io/?lang=ja',cv2.imencode('.jpg',img)[1].tobytes()).json()['text']JavaScriptIf you only need to recognize Chrome and English, you can also consider the Text Detection API.// Fileconstfile=document.querySelector('[type=file]').files[0]awaitfetch('http://localhost:8000/',{method:'POST',body:file}).then(r=>r.json())// Blobconstblob=awaitfetch('https://image.itmedia.co.jp/ait/articles/1706/15/news015_16.jpg').then(r=>r.blob())awaitfetch('http://localhost:8000/?lang=ja',{method:'POST',body:blob}).then(r=>r.json())It is also possible to run OCR Server on Windows Server.Information that can be obtainedYou can getangle,text,line,word,BoundingBox.importpprintresult=awaitwinocr.recognize_pil(img,'ja')pprint.pprint({'text_angle':result.text_angle,'text':result.text,'lines':[{'text':line.text,'words':[{'bounding_rect':{'x':word.bounding_rect.x,'y':word.bounding_rect.y,'width':word.bounding_rect.width,'height':word.bounding_rect.height},'text':word.text}forwordinline.words]}forlineinresult.lines]})Language installation# Run as AdministratorAdd-WindowsCapability-Online-Name"Language.OCR~~~en-US~0.0.1.0"Add-WindowsCapability-Online-Name"Language.OCR~~~ja-JP~0.0.1.0"# Search for installed languagesGet-WindowsCapability-Online-Name"Language.OCR*"# State: Not Present language is not installed, so please install it if necessary.Name:Language.OCR~~~hu-HU~0.0.1.0State:NotPresentDisplayName:ハンガリー語の光学式文字認識Description:ハンガリー語の光学式文字認識DownloadSize:194407InstallSize:535714Name:Language.OCR~~~it-IT~0.0.1.0State:NotPresentDisplayName:イタリア語の光学式文字認識Description:イタリア語の光学式文字認識DownloadSize:159875InstallSize:485922Name:Language.OCR~~~ja-JP~0.0.1.0State:InstalledDisplayName:日本語の光学式文字認識Description:日本語の光学式文字認識DownloadSize:1524589InstallSize:3398536Name:Language.OCR~~~ko-KR~0.0.1.0State:NotPresentDisplayName:韓国語の光学式文字認識Description:韓国語の光学式文字認識DownloadSize:3405683InstallSize:7890408If you hate Python and just want to recognize it with PowerShell, clickhereMulti-ProcessingBy processing in parallel, it is 3 times faster. You can make it even faster by increasing the number of cores!fromPILimportImageimages=[Image.open('testocr.png')foriinrange(1000)]1 core(elapsed 48s)The CPU is not used up.importwinocr[(awaitwinocr.recognize_pil(img)).textforimginimages]4 cores(elapsed 16s)I'm using 100% CPU.Create a worker module.%%writefileworker.pyimportwinocrimportasyncioasyncdefensure_coroutine(awaitable):returnawaitawaitabledefrecognize_pil_text(img):returnasyncio.run(ensure_coroutine(winocr.recognize_pil(img))).textimportworkerimportconcurrent.futureswithconcurrent.futures.ProcessPoolExecutor()asexecutor:# https://stackoverflow.com/questions/62488423results=executor.map(worker.recognize_pil_text,images)list(results)