Last active
August 13, 2023 21:18
-
-
Save daisycamber/a1b400fd8ab73852b608f6165e889c44 to your computer and use it in GitHub Desktop.
Censor a video with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2, pytesseract | |
import numpy as np | |
from moviepy.editor import * | |
FREQUENCY = 200 # The frequency by which the software checks for text and censors it | |
AVERAGE = 100 # The amount of blur applied to the text | |
EXTRA = 1 # Space to the sides of the text to be censored, in proportion to the size of the text | |
MIN_TEXT = 4 # Minumum umber of characters in each place to be censored | |
def censor_video(input_file, output_file): | |
clip = VideoFileClip(input_file) | |
vidcap = cv2.VideoCapture(input_file) | |
vidcap.set(cv2.CAP_PROP_POS_MSEC,FREQUENCY) | |
index = 0 | |
while has_frames: | |
has_frames, image = vidcap.read() | |
if has_frames: | |
transparent_img = np.zeros((image.shape[0], image.shape[1], image.shape[2]), dtype=np.uint8) | |
data = pytesseract.image_to_data(image, output_type='dict') | |
boxes = len(data['level']) | |
for i in range(boxes): | |
if len(data['text']) < MIN_TEXT: continue | |
(x, y, w, h) = (data['left'][i], data['top'][i], data['width'][i], data['height'][i]) | |
x = x - w * extra | |
y = y - h * extra | |
w = w * extra * 3 | |
h = h * extra * 3 | |
if x < 0: x = 0 | |
if y < 0: y = 0 | |
if y + h > image.shape[0]: y = image.shape[0] - h | |
if x + w > image.shape[1]: x = image.shape[1] - w | |
img = cv2.blur(image[y:y+h, x:x+w], (AVERAGE, AVERAGE)) | |
transparent_img[y:y+h, x:x+w] = img | |
im = ImageClip(transparent_img, duration=FREQUENCY/1000.0) | |
clip = CompositeVideoClip([clip, im.set_start(index * FREQUENCY).set_duration(FREQUENCY)]) | |
index = index + 1 | |
clip.write_videofile(output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment