all repos — videocr @ a5e6845a1bc3d3497bda2e87b87bbeaf2541b3f2

Extract hardcoded subtitles from videos using machine learning

move tessdata dir to ~/tessdata
Yi Ge me@yige.ch
Mon, 29 Apr 2019 03:04:06 +0200
commit

a5e6845a1bc3d3497bda2e87b87bbeaf2541b3f2

parent

fba35f010840753d71b2bafc9447f5607031eb7f

3 files changed, 10 insertions(+), 7 deletions(-)

jump to
M videocr/api.pyvideocr/api.py

@@ -1,6 +1,4 @@

from urllib.request import urlopen -import pathlib -import os import shutil from . import constants

@@ -9,14 +7,14 @@

def get_subtitles(video_path: str, lang='eng', time_start='0:00', time_end='', use_fullframe=False) -> str: - # download tesseract data file to ./tessdata/ if necessary - fpath = pathlib.Path('tessdata/{}.traineddata'.format(lang)) + # download tesseract data file to ~/tessdata if necessary + fpath = constants.TESSDATA_DIR / '{}.traineddata'.format(lang) if not fpath.is_file(): if lang == 'eng': url = constants.ENG_URL else: url = constants.TESSDATA_URL.format(lang) - os.makedirs('tessdata', exist_ok=True) + constants.TESSDATA_DIR.mkdir(parents=True, exist_ok=True) with urlopen(url) as res, open(fpath, 'w+b') as f: shutil.copyfileobj(res, f)
M videocr/constants.pyvideocr/constants.py

@@ -1,3 +1,7 @@

+import pathlib + +TESSDATA_DIR = pathlib.Path.home() / 'tessdata' + ENG_URL = 'https://github.com/tesseract-ocr/tessdata_fast/raw/master/eng.traineddata' TESSDATA_URL = 'https://github.com/tesseract-ocr/tessdata_best/raw/master/script/{}.traineddata'
M videocr/video.pyvideocr/video.py

@@ -4,6 +4,7 @@ import datetime

import pytesseract import cv2 +from . import constants from .models import PredictedFrame, PredictedSubtitle

@@ -70,8 +71,8 @@

def _single_frame_ocr(self, img) -> str: if not self.use_fullframe: # only use bottom half of the frame by default - img = img[img.shape[0] // 2:, :] - config = r'--tessdata-dir "tessdata"' + img = img[self.height // 2:, :] + config = '--tessdata-dir "{}"'.format(constants.TESSDATA_DIR) return pytesseract.image_to_data(img, lang=self.lang, config=config) def get_subtitles(self) -> str: