Skip to content

Commit c0ede4c

Browse files
committed
update
1 parent b286330 commit c0ede4c

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

README.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,89 @@ pip install winocr
88
```
99

1010
# Usage
11+
1112
## Pillow
13+
1214
```python
1315
import winocr
1416
from PIL import Image
1517

1618
img = Image.open('test.jpg')
1719
print((await winocr.recognize_pil(img, 'ja')).text)
1820
```
21+
![](https://camo.qiitausercontent.com/63785b963fa5d67e9f2be1ac9e4953579f4c2584/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f33373061353463382d623536612d353362382d366632352d3634333363313630383866612e706e67)
1922

2023
## OpenCV
24+
2125
```python
2226
import winocr
2327
import cv2
2428

2529
img = cv2.imread('test.jpg')
2630
print((await winocr.recognize_cv2(img, 'ja')).text)
2731
```
32+
![](https://camo.qiitausercontent.com/ad91376e61b0e32a243fdc92a4586e87c8f683ba/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f65383031336338362d613833322d393837632d353763382d3135383536356634313034322e706e67)
33+
34+
## Connect to local runtime on Colaboratory
35+
36+
![](https://i.imgur.com/gvj959U.png)
37+
38+
![](https://i.imgur.com/o9e0Fwk.png)
2839

2940
## Web API
41+
3042
Run server
3143
```powershell
3244
pip install winocr[api]
3345
winocr_serve
3446
```
3547

3648
### curl
49+
3750
```bash
3851
curl localhost:8000?lang=ja --data-binary @test.jpg
3952
```
53+
![](https://camo.qiitausercontent.com/9dcb18830efe482d9bbb1c8a90d80205f71112ed/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f66643138343566632d653634372d306437662d353336652d3634653363303263633861312e706e67)
4054

4155
### Python
56+
4257
```python
4358
import requests
4459

4560
bytes = open('test.jpg', 'rb').read()
4661
requests.post('http://localhost:8000/?lang=ja', bytes).json()['text']
4762
```
4863

64+
![](https://camo.qiitausercontent.com/04853be7fa2c389b3921ae4a098f1e41abab1a67/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f64373162333531362d326466652d616439312d626661352d6634376637623836633263632e706e67)
65+
66+
You can run OCR with the Colaboratory runtime with `./ngrok http 8000`
67+
68+
```python
69+
from PIL import Image
70+
from io import BytesIO
71+
72+
img = Image.open('test.jpg')
73+
# Preprocessing
74+
buf = BytesIO()
75+
img.save(buf, format='JPEG')
76+
requests.post('https://15a5fabf0d78.ngrok.io/?lang=ja', buf.getvalue()).json()['text']
77+
```
78+
![](https://camo.qiitausercontent.com/ee8498f92eef03632bb0d3abb26de1269970900c/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f33393064393539372d316435352d356431632d383532302d3135633461383131386464642e706e67)
79+
80+
```python
81+
import cv2
82+
import requests
83+
84+
img = cv2.imread('test.jpg')
85+
# Preprocessing
86+
requests.post('https://15a5fabf0d78.ngrok.io/?lang=ja', cv2.imencode('.jpg', img)[1].tobytes()).json()['text']
87+
```
88+
![](https://camo.qiitausercontent.com/e5f4e0bf0538b851ddd525879600a712a3ce978a/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f39613364613534612d363664362d396138322d373038662d6464343763323832313930302e706e67)
89+
4990
### JavaScript
91+
92+
If you only need to recognize Chrome and English, you can also consider the Text Detection API.
93+
5094
```javascript
5195
// File
5296
const file = document.querySelector('[type=file]').files[0]
@@ -56,3 +100,118 @@ await fetch('http://localhost:8000/', {method: 'POST', body: file}).then(r => r.
56100
const blob = await fetch('https://image.itmedia.co.jp/ait/articles/1706/15/news015_16.jpg').then(r=>r.blob())
57101
await fetch('http://localhost:8000/?lang=ja', {method: 'POST', body: blob}).then(r => r.json())
58102
```
103+
104+
It is also possible to run OCR Server on Windows Server.
105+
106+
# Information that can be obtained
107+
You can get **angle**, **text**, **line**, **word**, **BoundingBox**.
108+
109+
```python
110+
import pprint
111+
112+
result = await winocr.recognize_pil(img, 'ja')
113+
pprint.pprint({
114+
'text_angle': result.text_angle,
115+
'text': result.text,
116+
'lines': [{
117+
'text': line.text,
118+
'words': [{
119+
'bounding_rect': {'x': word.bounding_rect.x, 'y': word.bounding_rect.y, 'width': word.bounding_rect.width, 'height': word.bounding_rect.height},
120+
'text': word.text
121+
} for word in line.words]
122+
} for line in result.lines]
123+
})
124+
```
125+
![](https://camo.qiitausercontent.com/cea9240789734fc27486c62efec796b6397d7d35/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f63663534376231312d313038642d636335332d653863332d3136663337656432386163352e706e67)
126+
127+
# Language installation
128+
```powershell
129+
# Run as Administrator
130+
Add-WindowsCapability -Online -Name "Language.OCR~~~en-US~0.0.1.0"
131+
Add-WindowsCapability -Online -Name "Language.OCR~~~ja-JP~0.0.1.0"
132+
133+
# Search for installed languages
134+
Get-WindowsCapability -Online -Name "Language.OCR*"
135+
# State: Not Present language is not installed, so please install it if necessary.
136+
Name : Language.OCR~~~hu-HU~0.0.1.0
137+
State : NotPresent
138+
DisplayName : ハンガリー語の光学式文字認識
139+
Description : ハンガリー語の光学式文字認識
140+
DownloadSize : 194407
141+
InstallSize : 535714
142+
143+
Name : Language.OCR~~~it-IT~0.0.1.0
144+
State : NotPresent
145+
DisplayName : イタリア語の光学式文字認識
146+
Description : イタリア語の光学式文字認識
147+
DownloadSize : 159875
148+
InstallSize : 485922
149+
150+
Name : Language.OCR~~~ja-JP~0.0.1.0
151+
State : Installed
152+
DisplayName : 日本語の光学式文字認識
153+
Description : 日本語の光学式文字認識
154+
DownloadSize : 1524589
155+
InstallSize : 3398536
156+
157+
Name : Language.OCR~~~ko-KR~0.0.1.0
158+
State : NotPresent
159+
DisplayName : 韓国語の光学式文字認識
160+
Description : 韓国語の光学式文字認識
161+
DownloadSize : 3405683
162+
InstallSize : 7890408
163+
```
164+
165+
If you hate Python and just want to recognize it with PowerShell, click [here](https://gist.github.com/GitHub30/8bc1e784148e4f9801520c7e7ba191ea)
166+
167+
# Multi-Processing
168+
169+
By processing in parallel, it is 3 times faster. You can make it even faster by increasing the number of cores!
170+
171+
```python
172+
from PIL import Image
173+
174+
images = [Image.open('testocr.png') for i in range(1000)]
175+
```
176+
177+
### 1 core(elapsed 48s)
178+
179+
The CPU is not used up.
180+
![](https://camo.qiitausercontent.com/c9c991eb1473137866fb8693eb1d4bef7b6addfc/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f66613263326635322d396638362d643435322d643263372d6162336330653061663837642e706e67)
181+
182+
```python
183+
import winocr
184+
185+
[(await winocr.recognize_pil(img)).text for img in images]
186+
```
187+
![](https://camo.qiitausercontent.com/5bab8b980fee37d662f73893df2a4c0b4b494dd1/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f65656536636332302d376663352d376462372d383464622d6361656165326465666139392e706e67)
188+
189+
### 4 cores(elapsed 16s)
190+
191+
I'm using 100% CPU.
192+
![](https://camo.qiitausercontent.com/2722a602a190ac5e54df7164b9e3cf714cb48d42/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f37636565626631652d663232662d353065352d393635612d3430326666633738363435622e706e67)
193+
194+
Create a worker module.
195+
```python
196+
%%writefile worker.py
197+
import winocr
198+
import asyncio
199+
200+
async def ensure_coroutine(awaitable):
201+
return await awaitable
202+
203+
def recognize_pil_text(img):
204+
return asyncio.run(ensure_coroutine(winocr.recognize_pil(img))).text
205+
```
206+
207+
```python
208+
import worker
209+
import concurrent.futures
210+
211+
with concurrent.futures.ProcessPoolExecutor() as executor:
212+
# https://stackoverflow.com/questions/62488423
213+
results = executor.map(worker.recognize_pil_text, images)
214+
list(results)
215+
```
216+
217+
![](https://camo.qiitausercontent.com/e172513d58e10c9ad6ddd148eeb78e1bc112f624/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3230383336332f61373136373537312d616135642d636231352d616461312d6132643230306535386562382e706e67)

0 commit comments

Comments
 (0)