Spaces:
Running
Running
File size: 1,655 Bytes
2012550 4eed331 |
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 |
from .base import CensoringMethod
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import cv2
class TextCensor(CensoringMethod):
def __init__(self,
text,
font="arial.ttf",
scale_factor=0.5,
text_color="white",
background_color=None,
draw_background=True):
self.text = text
self.font_path = font
self.scale_factor = scale_factor
self.text_color = text_color
self.background_color = background_color
self.draw_background = draw_background
def apply(self, frame, bbox):
x1, y1, x2, y2 = bbox[:4]
center_x = (x1 + x2) // 2
center_y = (y1 + y2) // 2
font_size = int(max(x2-x1, y2-y1) * self.scale_factor)
try:
font = ImageFont.truetype(self.font_path, font_size)
except OSError as e:
raise ValueError(f"Could not open file at {self.font_path}")
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_frame = Image.fromarray(rgb_frame)
draw = ImageDraw.Draw(pil_frame)
if self.draw_background and self.background_color:
draw.rectangle([x1, y1, x2, y2], fill=self.background_color)
self.draw_overlay(draw, center_x, center_y, font)
out_image = cv2.cvtColor(np.array(pil_frame), cv2.COLOR_RGB2BGR)
return out_image
def draw_overlay(self, draw, x, y, font):
draw.text(
xy=(x, y),
text=self.text,
anchor="mm",
font=font,
fill=self.text_color
) |