forked from just-chillin/ImageRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOCR.py
More file actions
86 lines (75 loc) · 2.69 KB
/
Copy pathOCR.py
File metadata and controls
86 lines (75 loc) · 2.69 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from google.cloud import vision
from PIL import Image
import os
import sys
import io
import subprocess
from threading import Lock
run_program_lock = Lock()
image_file_lock = Lock()
def filter_file(file: str):
"""
A function for adding various filters to the image in case.
WARNING: THIS FUNCTION IS NOT THREAD SAFE
:param file: The path to the image file
"""
image = Image.open(file)
# image = image.convert('L')
# image = image.filter(ImageFilter.EDGE_ENHANCE)
image.save(".tmp.png")
def run_interpreter(language, code):
"""
Runs code with interpreter and returns the output. Thread safe.
:param language: The name of the language chosen by the user
:param code: A string containing the code in memory
"""
from Main import config
program_fname = '.tmp.%s' % config[language]["extension"]
interpreter = config[language]["interpreter"]
run_program_lock.acquire(blocking=True)
with open(program_fname, 'w+') as program_file:
print(code, file=program_file)
try:
output = subprocess.check_output([interpreter, program_fname])
except subprocess.CalledProcessError as e:
output = "\0"
run_program_lock.release()
return output
def fix_common_text_issues(code_str: str) -> str:
"""
Fixes common, generic OCR issues.
:param code_str: The string containing the code in memory
:return: The fixed string
"""
return code_str.replace('–', '-')
def get_text_from_image(image):
"""
Sends a image file to google's servers to be OCR'ed. Thread safe.
:param file: The path to the file to interpret.
:return: The response from google's servers.
"""
client = vision.ImageAnnotatorClient()
image_file_lock.acquire(blocking=True)
image_path = '.tmp.png'
image.save(image_path)
filter_file(image_path)
with io.open(image_path, 'rb') as image_file:
image = image_file.read()
image_file = vision.types.Image(content=image)
response = client.document_text_detection(image=image_file)
image_file_lock.release()
return response
def run_image(file: str, language: str) -> str:
"""
Runs the image.
:param file: The path to the image file
:param language: The language to use settings for
:return: The output of the interpreter
"""
if not os.environ["GOOGLE_APPLICATION_CREDENTIALS"]:
print("Please make sure the environment variable GOOGLE_APPLICATIONS_CREDENTIALS is set to the path of your "
"credentials file", file=sys.stderr)
exit(1)
response = get_text_from_image(file)
code: str = fix_common_text_issues(response.full_text_annotation.text)
return run_interpreter(language, code)