https://github.com/github30/winocr
https://github.com/github30/winocr
ocr python python3 text-recognition windows windows-10 windows-11 winrt winsdk
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/github30/winocr
- Owner: GitHub30
- License: mit
- Created: 2021-06-08T11:16:45.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-25T04:04:54.000Z (9 months ago)
- Last Synced: 2025-05-12T14:29:16.970Z (2 months ago)
- Topics: ocr, python, python3, text-recognition, windows, windows-10, windows-11, winrt, winsdk
- Language: Python
- Homepage: https://qiita.com/relu/items/94e35a85c9c64f86f738
- Size: 34.2 KB
- Stars: 20
- Watchers: 1
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WinOCR
[](https://badge.fury.io/py/winocr)
[](https://badge.fury.io/py/winocr)# Installation
```powershell
pip install winocr
```Full install
```powershell
pip install winocr[all]
```# Usage
## Pillow
The language to be recognized can be specified by the lang parameter (second argument).
```python
import winocr
from PIL import Imageimg = Image.open('test.jpg')
(await winocr.recognize_pil(img, 'ja')).text
```
## OpenCV
```python
import winocr
import cv2img = cv2.imread('test.jpg')
(await winocr.recognize_cv2(img, 'ja')).text
```
## Connect to local runtime on Colaboratory
Create a local connection by following [these instructions](https://research.google.com/colaboratory/local-runtimes.html).
```powershell
pip install jupyterlab jupyter_http_over_ws
jupyter serverextension enable --py jupyter_http_over_ws
jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com' --ip=0.0.0.0 --port=8888 --NotebookApp.port_retries=0
```

Also available on Jupyter / Jupyter Lab.
## REPL
```python
import cv2
from winocr import recognize_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.'
``````python
from PIL import Image
from winocr import recognize_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-Processing
```python
from PIL import Image
import concurrent.futures
from winocr import recognize_pil_syncimages = [Image.open('testocr.png') for i in range(1000)]
with concurrent.futures.ProcessPoolExecutor() as executor:
results = list(executor.map(recognize_pil_sync, images))
print(results)
```## Web API
Run server
```powershell
pip install winocr[api]
winocr_serve
```### curl
```bash
curl localhost:8000?lang=ja --data-binary @test.jpg
```
### Python
```python
import requestsbytes = 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 8000`
```python
from PIL import Image
from io import BytesIOimg = Image.open('test.jpg')
# Preprocessing
buf = BytesIO()
img.save(buf, format='JPEG')
requests.post('https://15a5fabf0d78.ngrok.io/?lang=ja', buf.getvalue()).json()['text']
```
```python
import cv2
import requestsimg = cv2.imread('test.jpg')
# Preprocessing
requests.post('https://15a5fabf0d78.ngrok.io/?lang=ja', cv2.imencode('.jpg', img)[1].tobytes()).json()['text']
```
### JavaScript
If you only need to recognize Chrome and English, you can also consider the Text Detection API.
```javascript
// File
const file = document.querySelector('[type=file]').files[0]
await fetch('http://localhost:8000/', {method: 'POST', body: file}).then(r => r.json())// Blob
const blob = await fetch('https://image.itmedia.co.jp/ait/articles/1706/15/news015_16.jpg').then(r=>r.blob())
await fetch('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 obtained
You can get **angle**, **text**, **line**, **word**, **BoundingBox**.```python
import pprintresult = await winocr.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
} for word in line.words]
} for line in result.lines]
})
```
# Language installation
```powershell
# Run as Administrator
Add-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 languages
Get-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.0
State : NotPresent
DisplayName : ハンガリー語の光学式文字認識
Description : ハンガリー語の光学式文字認識
DownloadSize : 194407
InstallSize : 535714Name : Language.OCR~~~it-IT~0.0.1.0
State : NotPresent
DisplayName : イタリア語の光学式文字認識
Description : イタリア語の光学式文字認識
DownloadSize : 159875
InstallSize : 485922Name : Language.OCR~~~ja-JP~0.0.1.0
State : Installed
DisplayName : 日本語の光学式文字認識
Description : 日本語の光学式文字認識
DownloadSize : 1524589
InstallSize : 3398536Name : Language.OCR~~~ko-KR~0.0.1.0
State : NotPresent
DisplayName : 韓国語の光学式文字認識
Description : 韓国語の光学式文字認識
DownloadSize : 3405683
InstallSize : 7890408
```If you hate Python and just want to recognize it with PowerShell, click [here](https://gist.github.com/GitHub30/8bc1e784148e4f9801520c7e7ba191ea)
# Multi-Processing
By processing in parallel, it is 3 times faster. You can make it even faster by increasing the number of cores!
```python
from PIL import Imageimages = [Image.open('testocr.png') for i in range(1000)]
```### 1 core(elapsed 48s)
The CPU is not used up.
```python
import winocr[(await winocr.recognize_pil(img)).text for img in images]
```
### 4 cores(elapsed 16s)
I'm using 100% CPU.

Create a worker module.
```python
%%writefile worker.py
import winocr
import asyncioasync def ensure_coroutine(awaitable):
return await awaitabledef recognize_pil_text(img):
return asyncio.run(ensure_coroutine(winocr.recognize_pil(img))).text
``````python
import worker
import concurrent.futureswith concurrent.futures.ProcessPoolExecutor() as executor:
# https://stackoverflow.com/questions/62488423
results = executor.map(worker.recognize_pil_text, images)
list(results)
```