Skip to content

Instantly share code, notes, and snippets.

@scottpanton
Created December 27, 2020 03:44
Show Gist options
  • Save scottpanton/cc042142dec4cc8f08a5640c0aca800c to your computer and use it in GitHub Desktop.
Save scottpanton/cc042142dec4cc8f08a5640c0aca800c to your computer and use it in GitHub Desktop.
Use Google Vision API to OCR subtitles from https://sourceforge.net/p/videosubfinder
import io,os,glob
from google.cloud import vision
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'creds.json'
path = '/some/place/'
srcdir = 'TXTImages'
frames = glob.glob(path + srcdir + '/*.jpeg')
client = vision.ImageAnnotatorClient()
for frame in frames:
with io.open(frame, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.text_detection(image=image)
text = response.full_text_annotation.text
print(text)
outpath = frame.replace(srcdir,'TXTResults').replace('jpeg','txt')
with io.open(outpath, 'w') as f:
f.write(text)
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
@Miish3lov1
Copy link

Miish3lov1 commented Jan 20, 2021

Thank you so much for this useful code!
However I'm trying to figure out where did I go wrong, this is what I used:

import io,os,glob
from google.cloud import vision
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'creds.json'

path = "C:/TXTImages/"
srcdir = "C:/TXTImages/"
frames = glob.glob(path + srcdir + '/*.jpeg')

client = vision.ImageAnnotatorClient()

for frame in frames:
    with io.open(frame, 'rb') as image_file:
        content = image_file.read()
    image = vision.Image(content=content)
    response = client.text_detection(image=image)
    text = response.full_text_annotation.text
    print(text)
    outpath = frame.replace(srcdir,'TXTResults').replace('jpeg','txt')
    with io.open(outpath, 'w') as f:
        f.write(text)
    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

When I run the script nothing happens, it does not work and does not give me any error at the same time.

I made sure I installed glob.
I made sure I have creds.json in the same folders.
I have jpeg images stored in TXTImages.
I'm using python 3.6 on windows 10.
I suspect that I entered the wrong format for path and srcdir at the beginning of the code.

An input from you would be mostly appreciated!

===================
EDIT: Nevermind, since I'm a typical code illiterate I realized I didn't have TXTResults folder in my directory.

However I'd like to add something, as soon as the code tries to ocr a utf-8 characters it immediately stops and gives this error:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-12: character maps to

I solved this issue by simply adding encoding="utf-8" in (outpath, 'w') si it becomes (outpath, 'w', encoding="utf-8")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment