Last active
November 26, 2019 10:53
-
-
Save devarshi16/f8812b05c5928fb20840804987ba7de0 to your computer and use it in GitHub Desktop.
Annotate text images using GOCR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.cloud import vision | |
import io | |
import os | |
import os | |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/devarshi/gcloud.json" | |
def detect_text(path): | |
"""Detects text in the file.""" | |
client = vision.ImageAnnotatorClient() | |
with io.open(path, 'rb') as image_file: | |
content = image_file.read() | |
image = vision.types.Image(content=content) | |
response = client.text_detection(image=image) | |
texts = response.text_annotations | |
print(path,': Texts:') | |
if len(texts) != 0: | |
all_texts = [] | |
for text in texts: | |
print('\n"{}"'.format(text.description)) | |
all_texts.append(text.description.strip()) | |
return all_texts[:int(len(all_texts)/2)] # For some unknown reason GOCR returns info about same text twice | |
else: | |
return None | |
with open('annotations.txt','w') as f: | |
for img in os.listdir('cropped'): | |
img_path = os.path.join('cropped',img) | |
print(img_path) | |
text = detect_text(img_path) | |
if text!= None: | |
f.write(img+' '+' '.join(text)+'\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment