llinahosna commited on
Commit
1ffed4c
1 Parent(s): c7e9155

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +63 -0
utils.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import re
3
+ from math import sqrt
4
+
5
+ import cv2
6
+
7
+ def get_duration(d):
8
+ return d.minutes*60 + d.seconds + d.milliseconds / 1000
9
+
10
+
11
+ def clean_str(str):
12
+ str = str.replace("\n", " ")
13
+ return re.sub(r'[^A-Za-z0-9 ]+', '', str)
14
+
15
+
16
+ def get_sqrt(n):
17
+ """return the smallest number x such that x**2 > n"""
18
+ x = int(sqrt(n))
19
+ while x**2 < n:
20
+ x = int(x + 1)
21
+ return x
22
+
23
+
24
+ def read_and_preprocess_transcript(path, song_name, n_lines=None):
25
+ """
26
+ Read transcription file, prepend the songname at the start (before first lyrice line),
27
+ set the duration of each lyrics line to last until the next one starts
28
+ """
29
+
30
+ transcript = json.load(open(path))
31
+
32
+ for i in range(len(transcript)):
33
+ if i < len(transcript) - 1:
34
+ transcript[i]['duration'] = transcript[i+1]['start'] - transcript[i]['start']
35
+
36
+ transcript = [{'text': song_name, 'start': 0, 'duration': transcript[0]['start']}] + transcript
37
+
38
+ if n_lines is not None:
39
+ transcript = transcript[:int(n_lines)]
40
+ return transcript
41
+
42
+
43
+ def put_subtitles_on_frame(frame, text, resize_factor=1):
44
+ w, h = frame.shape[:2]
45
+ frame = cv2.resize(frame, (w*resize_factor, h*resize_factor))
46
+ w, h = frame.shape[:2]
47
+ kwargs = {
48
+ 'thickness':2,
49
+ 'fontScale':0.75,
50
+ 'fontFace':cv2.FONT_HERSHEY_SIMPLEX
51
+ }
52
+ (tw, th), _ = cv2.getTextSize(text, **kwargs)
53
+ if tw > w:
54
+ words = text.split()
55
+ first_line = " ".join(words[:len(words) // 2:])
56
+ (tw, th), _ = cv2.getTextSize(first_line, **kwargs)
57
+ frame = cv2.putText(frame, first_line, (w//2 - tw // 2, h - int(th * 3)), color=(255, 255, 255), **kwargs)
58
+ second_line = " ".join(words[len(words) // 2:])
59
+ (tw, th), _ = cv2.getTextSize(second_line, **kwargs)
60
+ frame = cv2.putText(frame, second_line, (w//2 - tw // 2, h - int(th * 1.5)), color=(255, 255, 255), **kwargs)
61
+ else:
62
+ frame = cv2.putText(frame, text, (w//2 - tw // 2, h - int(th * 2)), color=(255, 255, 255), **kwargs)
63
+ return frame