all repos — videocr @ bd6f15978b905d9e6c72fe6f8c2b43e80e0d44ab

Extract hardcoded subtitles from videos using machine learning

videocr/models.py (view raw)

 1from __future__ import annotations
 2from typing import List
 3from dataclasses import dataclass
 4from fuzzywuzzy import fuzz
 5
 6
 7CONF_THRESHOLD = 60
 8# word predictions with lower confidence will be filtered out
 9
10
11@dataclass
12class PredictedWord:
13    __slots__ = 'confidence', 'text'
14    confidence: int
15    text: str
16
17
18class PredictedFrame:
19    index: int  # 0-based index of the frame
20    words: List[PredictedWord]
21    confidence: int  # total confidence of all words
22    text: str
23
24    def __init__(self, index, pred_data: str):
25        self.index = index
26        self.words = []
27
28        block = 0  # keep track of line breaks
29
30        for l in pred_data.splitlines()[1:]:
31            word_data = l.split()
32            if len(word_data) < 12:
33                # no word is predicted
34                continue
35            _, _, block_num, *_, conf, text = word_data
36            block_num, conf = int(block_num), int(conf)
37
38            # handle line breaks
39            if block < block_num:
40                block = block_num
41                if self.words and self.words[-1].text != '\n':
42                    self.words.append(PredictedWord(0, '\n'))
43
44            if conf >= CONF_THRESHOLD:
45                self.words.append(PredictedWord(conf, text))
46
47        self.confidence = sum(word.confidence for word in self.words)
48
49        self.text = ' '.join(word.text for word in self.words)
50        # remove chars that are obviously ocr errors
51        table = str.maketrans('|', 'I', '<>{}[];`@#$%^*_=~\\')
52        self.text = self.text.translate(table).replace(' \n ', '\n').strip()
53
54    def is_similar_to(self, other: PredictedFrame, threshold=70) -> bool:
55        return fuzz.ratio(self.text, other.text) >= threshold
56
57
58class PredictedSubtitle:
59    frames: List[PredictedFrame]
60    text: str
61
62    def __init__(self, frames: List[PredictedFrame]):
63        self.frames = [f for f in frames if f.confidence > 0]
64
65        if self.frames:
66            self.text = max(self.frames, key=lambda f: f.confidence).text
67        else:
68            self.text = ''
69
70    @property
71    def index_start(self) -> int:
72        if self.frames:
73            return self.frames[0].index
74        return 0
75
76    @property
77    def index_end(self) -> int:
78        if self.frames:
79            return self.frames[-1].index
80        return 0
81
82    def is_similar_to(self, other: PredictedSubtitle, threshold=90) -> bool:
83        return fuzz.partial_ratio(self.text, other.text) >= threshold
84
85    def __repr__(self):
86        return '{} - {}. {}'.format(self.index_start, self.index_end, self.text)