-
Notifications
You must be signed in to change notification settings - Fork 15
/
VideoTextRemover.py
82 lines (66 loc) · 2.89 KB
/
VideoTextRemover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#Imports
import cv2
import os
import numpy as np
import re
import time
#This is the folder the video will be in, and a subfolder for the temporary processing files.
dir = 'C:/Videos/' #Place video directory here
video_name = 'input.mp4' #Place video name here
#Sorts alphanumerically with frame formatting
def sortedproper( l ):
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
#Used for finding execution time
start_time = time.time()
#Video capture
vid = cv2.VideoCapture(os.path.join(dir, video_name))
frame_counter = 0
#Checks if Temp folder exists, if not, make one
if not os.path.exists(os.path.join(dir,'Temp')):
os.mkdir(os.path.join(dir,'Temp'))
print("Directory " , os.path.join(dir,'Temp') , " Created ")
else:
print("Directory " , os.path.join(dir,'Temp') , " already exists")
os.chdir(os.path.join(dir,'Temp'))
#Main process
#Runs through every frame to detect subtitles
#Saves each frame into Temp folder
while (frame_counter < vid.get(cv2.CAP_PROP_FRAME_COUNT)):
ret, img = vid.read()
name = "frame%d.jpg" % (frame_counter)
if not ret:
break
if not os.path.exists(os.path.join(dir,"Temp/frame%d.jpg" % (frame_counter))):
mask = np.zeros(img.shape, np.uint8)
recogImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
recogImg = cv2.threshold(recogImg, 240, 255, cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
recogImg = cv2.morphologyEx(recogImg, cv2.MORPH_CLOSE, kernel)
recogImg = cv2.dilate(recogImg, kernel, iterations=3)
contours, hierarchy = cv2.findContours(recogImg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
c = max(contours, key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(c)
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
mask[y:y+h, x:x+w] = recogImg[y:y+h, x:x+w]
mask = cv2.erode(mask, kernel, iterations=1)
mask = cv2.GaussianBlur(mask, (3,3), 0)
if len(mask.shape) > 2:
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
cleanedImg = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
cv2.imwrite(name, cleanedImg)
frame_counter += 1
#Move back to original video directory and begin saving frames into one video
os.chdir(dir)
video = cv2.VideoWriter('output.avi', 0, int(vid.get(cv2.CAP_PROP_FPS)), (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))))
images = [img for img in os.listdir(os.path.join(dir,'Temp')) if img.endswith(".jpg")]
images = sortedproper(images)
for image in images:
print(image)
video.write(cv2.imread(image))
#Cleanup and print execution time
video.release()
vid.release()
print("--- %s seconds ---" % (time.time() - start_time))