Showing
1 changed file
with
17 additions
and
34 deletions
1 | from pytesseract import image_to_string | 1 | from pytesseract import image_to_string |
2 | import argparse | 2 | import argparse |
3 | import cv2 | 3 | import cv2 |
4 | +from pathlib import Path | ||
5 | +import os.path | ||
6 | +import yaml | ||
4 | 7 | ||
8 | +doc = yaml.load(open('freset.yaml', 'r')) | ||
9 | +bil_op = doc["bilateralFilterData"] | ||
10 | +th_op = doc["thresholdData"] | ||
5 | 11 | ||
6 | def main(): | 12 | def main(): |
7 | parser = argparse.ArgumentParser(description='OCR program') | 13 | parser = argparse.ArgumentParser(description='OCR program') |
... | @@ -12,8 +18,9 @@ def main(): | ... | @@ -12,8 +18,9 @@ def main(): |
12 | help="Select engine to Google-Vision and Tessreact") | 18 | help="Select engine to Google-Vision and Tessreact") |
13 | args = parser.parse_args() | 19 | args = parser.parse_args() |
14 | 20 | ||
15 | - file_path = 'C:\\Users\\argos\\Desktop\\' + args.img_file | 21 | + direct = Path(os.path.expanduser('~')) |
16 | - img = cv2.imread(file_path) | 22 | + file_path = direct / 'Desktop' / args.img_file |
23 | + img = cv2.imread(str(file_path)) | ||
17 | select_freset(args.op, img) | 24 | select_freset(args.op, img) |
18 | 25 | ||
19 | 26 | ||
... | @@ -23,40 +30,16 @@ def tesseract_orc_to_file(img): | ... | @@ -23,40 +30,16 @@ def tesseract_orc_to_file(img): |
23 | with open('result\\foo.txt', "w") as f: | 30 | with open('result\\foo.txt', "w") as f: |
24 | f.write(text) | 31 | f.write(text) |
25 | 32 | ||
26 | - | ||
27 | def select_freset(type, img): | 33 | def select_freset(type, img): |
28 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | 34 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
29 | - if type == 1: | 35 | + if type < 7: |
30 | - #gray = cv2.GaussianBlur(gray, ksize=(3, 3), sigmaX=0) | 36 | + gray = cv2.bilateralFilter(gray, int(bil_op[0]), int(bil_op[1]), int(bil_op[2])) |
31 | - gray = cv2.bilateralFilter(gray, 9, 75, 75) | 37 | + _, img = cv2.threshold(gray, int(th_op['threshVal'][type % 3]), |
32 | - _, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY) | 38 | + int(th_op['threshMax'][0]), cv2.THRESH_BINARY_INV if type%2 else cv2.THRESH_BINARY) |
33 | - elif type == 2: | 39 | + elif type < 13: |
34 | - gray = cv2.bilateralFilter(gray, 9, 75, 75) | 40 | + _, img = cv2.threshold(gray, int(th_op['threshVal'][type % 3]), |
35 | - _, img = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY_INV) | 41 | + int(th_op['threshMax'][0]), |
36 | - elif type == 3: | 42 | + cv2.THRESH_BINARY_INV if type % 2 else cv2.THRESH_BINARY) |
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) | 43 | tesseract_orc_to_file(img) |
61 | 44 | ||
62 | 45 | ... | ... |
-
Please register or login to post a comment