-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_to_text.py
76 lines (51 loc) · 2.03 KB
/
image_to_text.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from pytesseract import pytesseract
from text_functions import get_info
import logging
import concurrent.futures
import cv2
logging.basicConfig(level=logging.CRITICAL)
def starred_adaptive_threshold(args):
return cv2.adaptiveThreshold(*args)
def ocr(img):
return pytesseract.image_to_string(img)
def to_text(filename, show_image=False):
logging.debug(f"Processing Image :{filename}")
results = {}
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
logging.debug("Converted the Image to Grayscale")
logging.debug("Adding Bilarteral Filter on the image")
img = cv2.bilateralFilter(img, 5, 25, 25)
if show_image:
cv2.imshow("Bilateral Filter", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
logging.debug("Applying OCR on first image")
results |= get_info(" ".join(ocr(img).splitlines()), results)
logging.debug("Creating Addional Images for Pre-Processing..")
thresh_img_params = [
(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 4),
(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 255, 1)
]
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
logging.debug("Started Preprocessing and OCR on Additional Images")
for result in executor.map(starred_adaptive_threshold, thresh_img_params):
results |= get_info(" ".join(ocr(result).splitlines()), results)
if show_image:
cv2.imshow("Threshold Image", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return results
if __name__ == '__main__':
from sys import argv
from pathlib import Path
from random import choice
filename = "".join(argv[1:]).strip()
if not filename:
test_dir = Path('test')
filename = choice(
[i for i in test_dir.iterdir() if i.suffix != ".pdf"])
filename = str(filename)
print(filename)
show_image = False
print(to_text(filename, show_image=show_image))