Your Name

source file

Showing 1 changed file with 64 additions and 0 deletions
1 +from pytesseract import image_to_string
2 +import argparse
3 +import cv2
4 +
5 +
6 +def main():
7 + parser = argparse.ArgumentParser(description='OCR program')
8 + parser.add_argument('img_file', type=str, help="Input Image File Name")
9 + parser.add_argument('--op', type=int,
10 + help="Input option number to 0 ~12", default=0)
11 + parser.add_argument('--engine', type=str,
12 + help="Select engine to Google-Vision and Tessreact")
13 + args = parser.parse_args()
14 +
15 + file_path = 'C:\\Users\\argos\\Desktop\\' + args.img_file
16 + img = cv2.imread(file_path)
17 + select_freset(args.op, img)
18 +
19 +
20 +def tesseract_orc_to_file(img):
21 + text = image_to_string(img, lang='kor')
22 + print(text) # debug
23 + with open('result\\foo.txt', "w") as f:
24 + f.write(text)
25 +
26 +
27 +def select_freset(type, img):
28 + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
29 + if type == 1:
30 + #gray = cv2.GaussianBlur(gray, ksize=(3, 3), sigmaX=0)
31 + gray = cv2.bilateralFilter(gray, 9, 75, 75)
32 + _, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
33 + elif type == 2:
34 + gray = cv2.bilateralFilter(gray, 9, 75, 75)
35 + _, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY_INV)
36 + elif type == 3:
37 + gray = cv2.bilateralFilter(gray, 9, 75, 75)
38 + _, img = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY)
39 + elif type == 4:
40 + gray = cv2.bilateralFilter(gray, 9, 75, 75)
41 + _, img = cv2.threshold(gray, 30, 225, cv2.THRESH_BINARY_INV)
42 + elif type == 5:
43 + _, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
44 + elif type == 6:
45 + _, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY_INV)
46 + elif type == 7:
47 + _, img = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY)
48 + elif type == 8:
49 + _, img = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
50 + elif type == 9:
51 + _, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY)
52 + elif type == 10:
53 + _, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY_INV)
54 + elif type == 11:
55 + gray = cv2.bilateralFilter(gray, 9, 75, 75)
56 + _, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY)
57 + elif type == 12:
58 + gray = cv2.bilateralFilter(gray, 9, 75, 75)
59 + _, img = cv2.threshold(gray, 98, 255, cv2.THRESH_BINARY_INV)
60 + tesseract_orc_to_file(img)
61 +
62 +
63 +if __name__ == "__main__":
64 + main()