You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

573 rivejä
22 KiB

  1. from abc import ABC
  2. import json
  3. import logging
  4. import os
  5. import random
  6. import subprocess
  7. from ast import literal_eval
  8. from pipeline.utils import SourceMedia, Source, Feature, Interval
  9. # for loudness detection
  10. import soundfile
  11. import pyloudnorm
  12. # for word detection
  13. from faster_whisper import WhisperModel, BatchedInferencePipeline
  14. logger = logging.getLogger(__name__)
  15. class FeatureExtractor(ABC):
  16. """Feature extractor interface."""
  17. # TODO: #API -- decide if .features will be a member variable
  18. def _run_get_output(self, cmd: list, cwd:str=".") -> str:
  19. """Run a command and return the output as a string
  20. Defined to be mocked out in tests via unittest.mock.patch
  21. """
  22. return subprocess.run(cmd, stdout=subprocess.PIPE, cwd=cwd).stdout.decode("utf-8")
  23. def setup(self):
  24. """Setup the feature extractor -- validate input files & config"""
  25. def run(self):
  26. """Run the feature extractor -- extract features"""
  27. def teardown(self):
  28. """Teardown the feature extractor -- clean up any temporary files created during setup"""
  29. class LaughterFeatureExtractor(FeatureExtractor):
  30. """Feature extractor for laughter detection.
  31. This class is responsible for extracting features corresponding to laughter in media files.
  32. Here:
  33. setup() is used to validate input files & config, which may involve processing video files to extract audio
  34. run() is used to extract features from the audio using jrgillick's laughter-detection
  35. teardown() is used to clean up any temporary files created during setup according to the config
  36. See: https://github.com/jrgillick/laughter-detection for the laughter-detection library
  37. """
  38. _PREPEND_TIME = 7.0 # seconds before the laugh
  39. _APPEND_TIME = 3.0 # seconds after the laugh
  40. def __init__(self, input_files=None, config=None):
  41. """It is expected that input_files is a SourceMedia object"""
  42. self.input_files = input_files
  43. self.config = config
  44. self.features = []
  45. def _laughdetect(self, audio_file) -> list:
  46. """Run laughter detection on the audio file
  47. Returns a list of 2-tuples, each representing a laugh instance in the audio file
  48. """
  49. laugh_detector_dir = "/home/robert/mounts/980data/code/laughter-detection/"
  50. laugh_detector_script = "segment_laughter.py"
  51. # fake output for testing
  52. # laugh_detector_path = "tests/fake_segment_laughter.py"
  53. laugh_detector_cmd = ["python", f"{laugh_detector_dir}{laugh_detector_script}",
  54. f"--input_audio_file={audio_file}"]
  55. # run command, capture output, ignore exit status
  56. # use self._run_get_output to allow mocking in tests
  57. laugh_output = self._run_get_output(laugh_detector_cmd, laugh_detector_dir)
  58. # ↑ have to include cwd to keep laughter-detection imports happy
  59. # also, it isn't happy if no output dir is specified but we get laughs so it's grand
  60. # laughs are lines in stdout that start with "instance:", followed by a space and a 2-tuple of floats
  61. # so jump to the 10th character and evaluate the rest of the line
  62. return [literal_eval(instance[10:])
  63. for instance in laugh_output.splitlines()
  64. if instance.startswith("instance: ")]
  65. def _adjust_features(self):
  66. """Adjust features according to config
  67. Generically, this ensures features conform to config - min/max feature length, etc.
  68. In the context of LaughterFeatureExtractor, there is some secret sauce: things that
  69. cause a laugh generally /precede/ the laugh, so we want more team before the detected start
  70. than at the end. For example, for a minimum feature length of 15s, we might prepend 10 seconds,
  71. and append 5 seconds (for example), or 12s and 3s. We may wish to do this pre/post adjustment
  72. for all laughter features found, regardless of length.
  73. TODO: figure out how we're going to handle length adjustments
  74. TODO: config for length adjustments per design doc
  75. TODO: play with numbers more to see what works best
  76. """
  77. for feature in self.features:
  78. # do the pre & post adjustment
  79. feature.interval.move_start(-self._PREPEND_TIME, relative=True)
  80. feature.interval.move_end(self._APPEND_TIME, relative=True)
  81. def setup(self):
  82. """Setup the laughter feature extractor -- validate input files & config
  83. jrgillick's laughter-detection library can work with AV files directly
  84. TODO: validate input files
  85. TODO: handle config
  86. """
  87. logger.debug("LaughterFeatureExtractor setup")
  88. # Validate input files
  89. if not self.input_files:
  90. raise ValueError("No input files provided")
  91. # TODO: convert video to audio if needed
  92. def run(self):
  93. """Extract laughter features for each input file"""
  94. if self.input_files:
  95. for file in self.input_files:
  96. # adjust this call for better test mocking
  97. laughs = self._laughdetect(file.path)
  98. for laugh in laughs:
  99. start, end = laugh
  100. self.features.append(Feature(interval=Interval(start=start, end=end),
  101. source=file, feature_extractor="laughter"))
  102. # TODO: implement options eg minimum feature length
  103. # adjust features
  104. self._adjust_features()
  105. def teardown(self):
  106. pass
  107. class RandomFeatureExtractor(FeatureExtractor):
  108. """Feature extractor for random feature generation.
  109. This class is responsible for generating random features for testing purposes.
  110. Here:
  111. setup() is used to validate input files & config
  112. run() is used to generate random features
  113. teardown() is used to clean up any temporary files created during setup according to the config
  114. """
  115. NUM_FEATURES = 5
  116. MAX_DURATION = 20.0
  117. def __init__(self, input_files=None, config=None):
  118. """It is expected that input_files is a SourceMedia object"""
  119. self.input_files = input_files
  120. self.config = config
  121. self.features = []
  122. def setup(self):
  123. """Setup the random feature extractor -- validate input files & config"""
  124. logger.debug("RandomFeatureExtractor setup")
  125. # Validate input files
  126. if not self.input_files:
  127. raise ValueError("No input files provided")
  128. def run(self):
  129. """Generate random features for each input file"""
  130. # check self.input_files is of type SourceMedia
  131. if not self.input_files or not isinstance(self.input_files, SourceMedia):
  132. raise ValueError("No input files provided")
  133. for file in self.input_files:
  134. for _ in range(self.NUM_FEATURES):
  135. # round to 3 decimal places
  136. duration = random.random() * self.MAX_DURATION
  137. start = random.random() * file.duration() - duration
  138. self.features.append(Feature(interval=Interval(start=start, duration=duration),
  139. source=file, feature_extractor="random"))
  140. def teardown(self):
  141. pass
  142. class LoudAudioFeatureExtractor(FeatureExtractor):
  143. """Feature extractor for loud audio detection.
  144. This class is responsible for extracting features corresponding to loud audio in media files.
  145. Here:
  146. setup() is used to validate input files & config, and extracting audio
  147. run() uses pyloudnorm to detect loud audio
  148. teardown() is used to clean up temporary files created during setup (if specified by config)
  149. """
  150. _CONFIG_DEFAULT_NUM_FEATURES = 15 # keep the top 5 loudnesses
  151. _CONFIG_DEFAULT_MIN_DURATION = 5.00 # seconds
  152. def __init__(self, input_files=None, config=None,
  153. num_features=_CONFIG_DEFAULT_NUM_FEATURES,
  154. min_duration=_CONFIG_DEFAULT_MIN_DURATION):
  155. if not input_files:
  156. raise ValueError("No input files provided!")
  157. self.input_files = input_files
  158. self.config = config
  159. self.features = []
  160. self._num_features = num_features
  161. self._min_duration = min_duration
  162. def _audio_file_from_path(self, path: str) -> str:
  163. """Return the audio file path given a video file path
  164. Example:
  165. - in = "/path/to/video.mp4"
  166. - out = "/tmp/video.mp4.wav"
  167. """
  168. OUTPUT_DIR = "/tmp"
  169. return f"{OUTPUT_DIR}/{os.path.basename(path)}.wav"
  170. def _get_loudnesses(self, data, meter, rate, window_size, stride_size):
  171. """Extract loudnesses from the audio data using pyloudnorm
  172. return a list of 2-tuples, each representing a timecode and loudness value
  173. """
  174. loudnesses = []
  175. for w in range(0, len(data)-window_size, stride_size):
  176. window = data[w:w+window_size, 0:2] # extract window
  177. loudnesses.append( (w/rate, meter.integrated_loudness(window)) )
  178. return loudnesses
  179. def _loudnorm(self, audio_file):
  180. """Run pyloudnorm on the audio file"""
  181. data, rate = soundfile.read(audio_file) # load audio (with shape (samples, channels))
  182. meter = pyloudnorm.Meter(rate=rate,block_size=0.3) # create BS.1770 meter
  183. loudness_features = []
  184. window_size = int(rate * 0.5) # 500ms
  185. stride_size = int(rate * 0.5) # 500ms -- no overlap
  186. # for w in range(data.shape[0]//100):
  187. # loudnesses.append(meter.integrated_loudness(data[w:w+int(0.3*rate),0:2]))
  188. loudnesses = self._get_loudnesses(data, meter, rate, window_size, stride_size)
  189. for timecode, loudval in sorted([l for l in loudnesses if float(l[1]) != float("-inf")], key=lambda x: x[1], reverse=True):
  190. # print(f"Timecode: {timecode}, Loudness: {loudval}")
  191. loudness_features.append((timecode, round(loudval, 3))) # round to 3 DP
  192. return loudness_features
  193. def _keep_num(self, features, num=_CONFIG_DEFAULT_NUM_FEATURES, margin=10.0) -> list:
  194. """Keep the top n features (default: 5)
  195. Approach:
  196. - for range in 0-n
  197. + expand the nth top feature to min duration
  198. (move start back by 0.5*min_duration, end forward by 0.5*min_duration)
  199. + drop any features that are now in that feature's range (plus margin)
  200. - return the top n features
  201. Each feature is a Feature object, with an Interval object
  202. """
  203. keep_features = []
  204. # ensure features are sorted by score
  205. features = sorted(features, key=lambda x: x.score, reverse=True)
  206. for i in range(num):
  207. current_feature = features.pop(0)
  208. # expand the feature to min_duration
  209. current_feature.interval.move_start(-0.5*self._min_duration, relative=True)
  210. current_feature.interval.move_end(0.5*self._min_duration, relative=True)
  211. keep_features.append(current_feature)
  212. # drop any features that are now in that feature's range (plus margin)
  213. features = [f for f in features if
  214. (f.interval.end < current_feature.interval.start-margin or
  215. f.interval.start > current_feature.interval.end+margin)]
  216. return keep_features
  217. def setup(self):
  218. """extract audio from video files to be processed by pyloudnorm
  219. TODO: config -- hardcoded for now
  220. """
  221. # pyloudnorm expects WAV files
  222. for file in self.input_files:
  223. audio_file = self._audio_file_from_path(file.path)
  224. # ffmpeg -i input.mp4 -vn -acodec pcm_s16le output.wav
  225. subprocess.run(["ffmpeg", "-y", "-i", file.path, "-vn", "-acodec", "pcm_s16le", audio_file],
  226. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  227. def run(self):
  228. """Use pyloudnorm to detect loud audio"""
  229. for file in self.input_files:
  230. audio_file = self._audio_file_from_path(file.path)
  231. loudnesses = self._loudnorm(audio_file)
  232. features = []
  233. for time, loudness in loudnesses:
  234. features.append(Feature(interval=Interval(start=time, duration=0.500),
  235. source=file, feature_extractor="loudness",
  236. score=loudness))
  237. # prune features list to keep self.num_features
  238. self.features = self._keep_num(features, self._num_features)
  239. class VideoActivityFeatureExtractor(FeatureExtractor):
  240. """Feature extractor for video activity detection.
  241. This class is responsible for extracting features corresponding to high activity in video files.
  242. Uses ffmpeg's scdet filter with threshold of zero.
  243. Here:
  244. setup() is used to validate input files & config
  245. run() is used to extract features from the video using OpenCV
  246. teardown() is used to clean up any temporary files created during setup according to the config
  247. #TODO: minimum duration -- consider whether to do here, or expand duration post-consolidation
  248. """
  249. _CONFIG_DEFAULT_NUM_FEATURES = 15 # keep the top 5 activity moments
  250. _CONFIG_DEFAULT_MIN_DURATION = 5.00 # seconds
  251. def __init__(self, input_files=None, config=None,
  252. num_features=_CONFIG_DEFAULT_NUM_FEATURES,
  253. min_duration=_CONFIG_DEFAULT_MIN_DURATION):
  254. if not input_files:
  255. raise ValueError("No input files provided!")
  256. self.input_files = input_files
  257. self.config = config
  258. self.features = []
  259. self._num_features = num_features
  260. self._min_duration = min_duration
  261. def _scdet(self, video_file):
  262. """Run scdet filter on the video file"""
  263. ffmpeg_cmd = ["ffmpeg", "-i", video_file, "-vf", "scdet=threshold=0", "-f", "null", "-"]
  264. # output is of the form:
  265. # [scdet @ 0x7f0798003d00] lavfi.scd.score: 0.031, lavfi.scd.time: 23.65
  266. # [scdet @ 0x7f0798003d00] lavfi.scd.score: 0.006, lavfi.scd.time: 23.70
  267. # capture output, extract time & score
  268. scdet_output = subprocess.run(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stderr.decode("utf-8")
  269. # extract time & score
  270. scores = []
  271. for line in scdet_output.splitlines():
  272. if "lavfi.scd.score" in line:
  273. scores.append( (float(line.split(",")[1].split(":")[1]),
  274. float(line.split(",")[0].split(":")[1]))
  275. )
  276. return scores
  277. def _nonoverlap_mean(self, scores, window_size=0.500) -> list:
  278. """Take the mean of non-overlapping windows of scores
  279. Input: list of tuples in the format (time, score)
  280. Output: list of tuples in the format (time, mean_score) (reduced set)
  281. """
  282. means = []
  283. current_window = []
  284. current_window_start = 0.0
  285. for time, score in scores:
  286. if time - current_window_start > window_size:
  287. # calculate mean of current window
  288. mean_score = sum([s for _, s in current_window]) / len(current_window)
  289. means.append((current_window_start, round(mean_score, 3)))
  290. # reset window
  291. current_window = []
  292. current_window_start = time
  293. current_window.append((time, score))
  294. return means
  295. def _drop_lowest(self, scores, percent=33):
  296. """Drop the lowest n% scores from the list"""
  297. scores = sorted(scores, key=lambda x: x[1], reverse=True)
  298. return scores[:int(len(scores) * (percent / 100))]
  299. def _keep_num(self, features, num=_CONFIG_DEFAULT_NUM_FEATURES, margin=10.0) -> list:
  300. """Keep the top n features (default: 5)
  301. Approach:
  302. - for range in 0-n
  303. + expand the nth top feature to min duration
  304. (move start back by 0.5*min_duration, end forward by 0.5*min_duration)
  305. + drop any features that are now in that feature's range (plus margin)
  306. - return the top n features
  307. Each feature is a Feature object, with an Interval object
  308. """
  309. keep_features = []
  310. # ensure features are sorted by score
  311. features = sorted(features, key=lambda x: x.score, reverse=True)
  312. for i in range(num):
  313. current_feature = features.pop(0)
  314. # expand the feature to min_duration
  315. current_feature.interval.move_start(-0.5*self._min_duration, relative=True)
  316. current_feature.interval.move_end(0.5*self._min_duration, relative=True)
  317. keep_features.append(current_feature)
  318. # drop any features that are now in that feature's range (plus margin)
  319. features = [f for f in features if
  320. (f.interval.end < current_feature.interval.start-margin or
  321. f.interval.start > current_feature.interval.end+margin)]
  322. return keep_features
  323. def setup(self):
  324. pass
  325. def run(self):
  326. for file in self.input_files:
  327. scores = self._scdet(file.path)
  328. means = sorted(self._nonoverlap_mean(scores), key=lambda x: x[1], reverse=True)
  329. features = []
  330. for time, score in self._drop_lowest(means, 66):
  331. features.append(Feature(interval=Interval(start=time, duration=0.500),
  332. source=file, feature_extractor="videoactivity",
  333. score=score))
  334. # prune features list to keep self.num_features
  335. self.features = self._keep_num(features, self._num_features)
  336. def teardown(self):
  337. pass
  338. class JSONFeatureExtractor(FeatureExtractor):
  339. """(Re-)create features from a JSON file
  340. The JSON file can have one of two formats:
  341. - the format produced by the pipleline (@see: video_producers.py:JSONProducer)
  342. - a simplified format which is easier for manual creation
  343. """
  344. def __init__(self, input_files=None, config=None):
  345. if not input_files:
  346. raise ValueError("No input files provided!")
  347. self.input_files = input_files
  348. self.config = config
  349. self.features = []
  350. def setup(self):
  351. pass
  352. def _interval_from_dict(self, d):
  353. return Interval(start=d["start"], duration=d["duration"])
  354. def _source_from_dict(self, d):
  355. return Source(d["source"], d["path"], d["provider"])
  356. def _read_json_from_file(self, file):
  357. """Read a JSON file and return the contents
  358. Method exists to allow for mocking in tests
  359. """
  360. with open(file, "r") as f:
  361. return json.load(f)
  362. def run(self):
  363. # only pipeline JSON format for now
  364. # TODO: add support for simplified format
  365. for file in self.input_files:
  366. features_from_json = self._read_json_from_file(file.path)
  367. for feature in features_from_json:
  368. self.features.append(Feature(interval=self._interval_from_dict(feature["interval"]),
  369. source=self._source_from_dict(feature["source"]),
  370. feature_extractor=feature["feature_extractor"],
  371. score=feature["score"]))
  372. def teardown(self):
  373. pass
  374. class WordFeatureExtractor(FeatureExtractor):
  375. """Feature extractor for specific word detection (uses Whisper)"""
  376. # set defaults for whisper settings
  377. DEFAULT_MODEL_SIZE = "medium"
  378. DEFAULT_DEVICE = "cpu"
  379. DEFAULT_COMPUTE_TYPE = "int8"
  380. DEFAULT_BEAM_SIZE = 5
  381. DEFAULT_BATCH_SIZE = 16
  382. DEFAULT_PIPELINE_TYPE = "batched" # or "stream"
  383. words = []
  384. def _transcribe(self, model, file, **kwargs):
  385. """Defined here to allow for mocking in tests"""
  386. return model.transcribe(file, **kwargs)
  387. def _whispermodel(self, model_size=DEFAULT_MODEL_SIZE,
  388. device=DEFAULT_DEVICE, compute_type=DEFAULT_COMPUTE_TYPE):
  389. """Defined here to allow for mocking out in tests"""
  390. return WhisperModel(model_size, device=device, compute_type=compute_type)
  391. def _batched_inference_pipeline(self, model):
  392. """Defined here to allow for mocking out in tests"""
  393. return BatchedInferencePipeline(model=model)
  394. def __init__(self, input_files=None, config=None):
  395. if not input_files:
  396. raise ValueError("No input files provided!")
  397. self.input_files = input_files
  398. self.config = config
  399. self.features = []
  400. def setup(self, words=[]):
  401. """Setup the word feature extractor -- validate input files & config
  402. Whisper expects a list of words to search for in the audio
  403. """
  404. logger.debug("WordFeatureExtractor setup")
  405. # Validate words - raise a notice if none provided
  406. if len(words) == 0:
  407. logger.warning("No words provided for detection")
  408. self.words = words
  409. # TODO: consider stripping punctuation since Whisper produces words+punctuation
  410. # and we might want to strip the punctuation there too
  411. def run(self):
  412. """Extract features corresponding to supplied target words (defined in setup) for each input file
  413. Use Whisper to detect words in the audio, then match these to target words and create features
  414. Note: if no words are supplied we can exit early
  415. """
  416. if len(self.words) == 0: return
  417. if self.DEFAULT_PIPELINE_TYPE == "batched":
  418. batched = True
  419. else:
  420. batched = False
  421. # no early exit
  422. # TODO: consider maybe loglevel notice of estimated time! consider also: max execution time config?
  423. # TODO: config options for model size, device, compute type
  424. model = self._whispermodel() # NB uses defaults, TODO: add config options
  425. # NOTE: batched not available on pypi yet at time of writing
  426. if batched:
  427. batched_model = self._batched_inference_pipeline(model)
  428. for file in self.input_files:
  429. # transcribe the audio file
  430. if batched:
  431. segments, _ = self._transcribe(batched_model, file.path, batch_size=self.DEFAULT_BATCH_SIZE)
  432. else:
  433. segments, _ = self._transcribe(model, file.path, beam_size=self.DEFAULT_BEAM_SIZE)
  434. # process the segments
  435. # segment has: start, end, text
  436. for segment in segments:
  437. # check if any of the words are in the segment
  438. for word in segment.text.split():
  439. if word in self.words:
  440. self.features.append(Feature(interval=Interval(start=segment.start, end=segment.end),
  441. source=file, feature_extractor="word",
  442. score=1.0))