main.py
2.3 KB
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
from pytesseract import image_to_string
import argparse
import cv2
def main():
parser = argparse.ArgumentParser(description='OCR program')
parser.add_argument('img_file', type=str, help="Input Image File Name")
parser.add_argument('--op', type=int,
help="Input option number to 0 ~12", default=0)
parser.add_argument('--engine', type=str,
help="Select engine to Google-Vision and Tessreact")
args = parser.parse_args()
file_path = 'C:\\Users\\argos\\Desktop\\' + args.img_file
img = cv2.imread(file_path)
select_freset(args.op, img)
def tesseract_orc_to_file(img):
text = image_to_string(img, lang='kor')
print(text) # debug
with open('result\\foo.txt', "w") as f:
f.write(text)
def select_freset(type, img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if type == 1:
#gray = cv2.GaussianBlur(gray, ksize=(3, 3), sigmaX=0)
gray = cv2.bilateralFilter(gray, 9, 75, 75)
_, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
elif type == 2:
gray = cv2.bilateralFilter(gray, 9, 75, 75)
_, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY_INV)
elif type == 3:
gray = cv2.bilateralFilter(gray, 9, 75, 75)
_, img = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY)
elif type == 4:
gray = cv2.bilateralFilter(gray, 9, 75, 75)
_, img = cv2.threshold(gray, 30, 225, cv2.THRESH_BINARY_INV)
elif type == 5:
_, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
elif type == 6:
_, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY_INV)
elif type == 7:
_, img = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY)
elif type == 8:
_, img = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
elif type == 9:
_, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY)
elif type == 10:
_, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY_INV)
elif type == 11:
gray = cv2.bilateralFilter(gray, 9, 75, 75)
_, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY)
elif type == 12:
gray = cv2.bilateralFilter(gray, 9, 75, 75)
_, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY_INV)
tesseract_orc_to_file(img)
if __name__ == "__main__":
main()