forked from ZiadSheriif/Sa7a7LY
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ocr.py
33 lines (26 loc) · 935 Bytes
/
ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pytesseract
from PIL import Image
import os
from bidi.algorithm import get_display
import arabic_reshaper
def ocr(dir, langSelected, type=None):
# if it doesn't work :
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR.\tesseract.exe'
result = []
if type != None:
conf = '--psm 13 --oem 1 -c tessedit_char_whitelist=0123456789'
else:
conf = ""
for filename in os.scandir(dir):
ocrOutput = pytesseract.image_to_string(
Image.open(filename.path), lang=langSelected, config=conf)
if(langSelected == 'ara'):
arabicText = get_display(ocrOutput)
ocrOutput = arabic_reshaper.reshape(arabicText)
if(len(ocrOutput) != 0 and type != None):
result.append(ocrOutput[0])
elif (type != None):
result.append(0)
else:
result.append(ocrOutput)
return result