Compare commits
9 Commits
9b97c99657
...
master
Author | SHA1 | Date |
---|---|---|
Pradana AUMARS | 92d131ecd6 | |
Yun | b5e6f5a57f | |
Pradana AUMARS | 25765b8b6f | |
Pradana AUMARS | 09f5098e19 | |
Pradana AUMARS | b005e36fcd | |
Yun | aec2b9c95a | |
Yun | 7f6881749f | |
Pradana AUMARS | edc1bc28a2 | |
Pradana AUMARS | 5534ae317f |
|
@ -4,6 +4,7 @@ import sys
|
|||
import multiprocessing
|
||||
import pytesseract
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from . import constants
|
||||
from . import utils
|
||||
|
@ -18,6 +19,8 @@ class Video:
|
|||
num_frames: int
|
||||
fps: float
|
||||
height: int
|
||||
width: int
|
||||
resize_dim: List[int]
|
||||
pred_frames: List[PredictedFrame]
|
||||
pred_subs: List[PredictedSubtitle]
|
||||
|
||||
|
@ -27,6 +30,9 @@ class Video:
|
|||
self.num_frames = int(v.get(cv2.CAP_PROP_FRAME_COUNT))
|
||||
self.fps = v.get(cv2.CAP_PROP_FPS)
|
||||
self.height = int(v.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||
self.width = int(v.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||
scale_percent = 47 # apparently 32 pixels is the optimal character height for tesseract.
|
||||
self.resize_dim=(int(self.width * scale_percent/100), int(self.height * scale_percent/100))
|
||||
|
||||
def run_ocr(self, lang: str, time_start: str, time_end: str,
|
||||
conf_threshold: int, use_fullframe: bool) -> None:
|
||||
|
@ -56,7 +62,15 @@ class Video:
|
|||
if not self.use_fullframe:
|
||||
# only use bottom half of the frame by default
|
||||
img = img[self.height // 2:, :]
|
||||
config = '--tessdata-dir "{}"'.format(constants.TESSDATA_DIR)
|
||||
img = cv2.dilate(img, np.ones((2, 2), np.uint8))
|
||||
_, img = cv2.threshold(img, 215, 255, cv2.THRESH_BINARY)
|
||||
color_mask = cv2.inRange(img, (255, 255, 255), (255, 255, 255))
|
||||
img = cv2.bitwise_and(img, img, mask=color_mask)
|
||||
img = cv2.erode(img, np.ones((2, 2), np.uint8))
|
||||
img = cv2.bitwise_not(img)
|
||||
img = cv2.resize(img, self.resize_dim, interpolation=cv2.INTER_AREA)
|
||||
img = cv2.copyMakeBorder(img, 20, 20, 0, 0, cv2.BORDER_CONSTANT, None, (255,255,255))
|
||||
config = '--tessdata-dir "{}" --psm 7 -c preserve_interword_spaces=1'.format(constants.TESSDATA_DIR)
|
||||
try:
|
||||
return pytesseract.image_to_data(img, lang=self.lang, config=config)
|
||||
except Exception as e:
|
||||
|
|
Loading…
Reference in New Issue