-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_text.py
63 lines (44 loc) · 1.46 KB
/
get_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
from PIL import Image
import cv2
import imutils
import pytesseract
import numpy as np
import argparse
import os
import sys
import subprocess
import file_names
import crop_to_text
#def downscale_image(args):
#
# DPI = 300
# height, width, _ = image.shape
#
# temp_path = os.getcwd() + '/temp.jpeg'
# cmd = ["convert", "-units", "PixelsPerInch", args["image"], "-density", str(DPI), temp_path]
# subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait()
#
# downscaled_image = cv2.imread(temp_path)
# os.remove(temp_path)
#
# return downscaled_image
def return_text(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.fastNlMeansDenoising(image, None, 10, 3, 7)
#cv2.imshow("Gray", image)
image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 41, 10)
filename = file_names.denoised_threshed_input_image
image = crop_to_text.fit_to_text(image)
cv2.imwrite(filename, image)
text = pytesseract.image_to_string(Image.open(filename))
return text
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to image to get text from")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
#cv2.imshow("Input image", image)
#deskewed_cropped_image = fix_skewness(cropped_image)
#image = downscale_image(args)
print(return_text(image))