diff --git a/common/research/__pycache__/constants.cpython-37.pyc b/common/research/__pycache__/constants.cpython-37.pyc deleted file mode 100644 index cb8a14f78b84fe640a606939105fdd40b1af32ed..0000000000000000000000000000000000000000 Binary files a/common/research/__pycache__/constants.cpython-37.pyc and /dev/null differ diff --git a/common/research/dataset/twitch/__pycache__/make_thumbnails.cpython-37.pyc b/common/research/dataset/twitch/__pycache__/make_thumbnails.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8163b02737ad289f5a28e1a6cd97028d04b25fca Binary files /dev/null and b/common/research/dataset/twitch/__pycache__/make_thumbnails.cpython-37.pyc differ diff --git a/common/research/dataset/twitch/__pycache__/robot_view.cpython-37.pyc b/common/research/dataset/twitch/__pycache__/robot_view.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f03b68d95d8a69fb27956dec65e129a41b90c359 Binary files /dev/null and b/common/research/dataset/twitch/__pycache__/robot_view.cpython-37.pyc differ diff --git a/common/research/dataset/twitch/is_robot_view.py b/common/research/dataset/twitch/is_robot_view.py deleted file mode 100644 index f671e6b481c5fabe0cdeb5a1d135887e93adc5f9..0000000000000000000000000000000000000000 --- a/common/research/dataset/twitch/is_robot_view.py +++ /dev/null @@ -1,53 +0,0 @@ -from os import remove -from pathlib import Path -from shutil import move - -import numpy as np -from scipy.spatial import distance -from skimage import io -from watchdog.events import FileSystemEvent, FileSystemEventHandler -from watchdog.observers import Observer - -from research.constants import TWITCH_DSET - - -ref_image = io.imread('mask.jpg') - -_MASK = ref_image[:, :, 1] > 50 -_REF_IMG_MASKED = ref_image*_MASK[:, :, np.newaxis] -_THRESHOLD = 23 - - -def is_image_from_robot_view(path_to_image: Path) -> bool: - img = io.imread(path_to_image) - img_masked = img * _MASK[:, :, np.newaxis] - return distance.euclidean(img_masked.flatten() / 255, _REF_IMG_MASKED.flatten() / 255) < _THRESHOLD - - -class NewFrameHandler(FileSystemEventHandler): - - def __init__(self): - self.res_dir = (TWITCH_DSET / 'robots-views') - self.res_dir.mkdir(exist_ok=True, parents=True) - - def on_created(self, event: FileSystemEvent): - if event.is_directory or not event.src_path.endswith('jpg'): - return - file_path = Path(event.src_path) - if is_image_from_robot_view(file_path): - res = self.res_dir / f'{file_path.parent.name}-{file_path.name}' - move(file_path, res) - else: - remove(file_path) - - def __hash__(self): - return hash(self.__class__.__name__) - - -if __name__ == '__main__': - obs = Observer() - obs.schedule(NewFrameHandler(), str(TWITCH_DSET / 'raw-frames'), recursive=True) - obs.start() - - while 1: - pass diff --git a/common/research/dataset/twitch/make_thumbnails.py b/common/research/dataset/twitch/make_thumbnails.py index 58efda5361444c79be33858f4aa4af050ece4b37..2b947d4c75506fdc474aae21061fff922c4924f4 100644 --- a/common/research/dataset/twitch/make_thumbnails.py +++ b/common/research/dataset/twitch/make_thumbnails.py @@ -48,10 +48,3 @@ class ThumbnailsGenerator: def _get_frame_number(self): return int(ffmpeg.probe(str(self.video_path))['format']['duration'].split('.')[0]) - - -if __name__ == '__main__': - _video_name = sys.argv[1] - print(f'Fragmenting video {_video_name}') - ThumbnailsGenerator(_video_name).run() - diff --git a/common/research/dataset/twitch/robot_view.py b/common/research/dataset/twitch/robot_view.py new file mode 100644 index 0000000000000000000000000000000000000000..3988273dc4f60270da7b8b3fe14f3a984ed50eaa --- /dev/null +++ b/common/research/dataset/twitch/robot_view.py @@ -0,0 +1,38 @@ +from os import remove +from pathlib import Path +from shutil import move + +import numpy as np +from scipy.spatial import distance +from skimage import io +from skimage.transform import resize + +from research.constants import TWITCH_DSET + +RES_DIR: Path = TWITCH_DSET / 'robots-views' +RES_DIR.mkdir(parents=True, exist_ok=True) + +ref_image = io.imread(f'{__file__}/../mask.jpg') + +_MASK = ref_image[:, :, 1] > 50 +_REF_IMG_MASKED = ref_image*_MASK[:, :, np.newaxis] +_THRESHOLD = 23 + + +def is_image_from_robot_view(path_to_image: Path) -> bool: + img = io.imread(path_to_image) + img_masked = img * _MASK[:, :, np.newaxis] + return distance.euclidean(img_masked.flatten() / 255, _REF_IMG_MASKED.flatten() / 255) < _THRESHOLD + + +def process_image(path_to_image: Path): + if is_image_from_robot_view(path_to_image): + res_path = RES_DIR / f'{path_to_image.parent.name}-{path_to_image.name}' + move(str(path_to_image), str(res_path)) + else: + remove(str(path_to_image)) + + +def process_all_images_in_dir(dir_path: Path): + for path_to_image in dir_path.glob('*/*.jpg'): + process_image(path_to_image) diff --git a/common/research/scripts/monitor_new_twitch_frames.py b/common/research/scripts/monitor_new_twitch_frames.py new file mode 100644 index 0000000000000000000000000000000000000000..c70a9dea55780407acc4d05240ffc20080e344bf --- /dev/null +++ b/common/research/scripts/monitor_new_twitch_frames.py @@ -0,0 +1,9 @@ +from time import sleep + +from research.constants import TWITCH_DSET +from research.dataset.twitch.robot_view import process_all_images_in_dir + +if __name__ == '__main__': + while 1: + process_all_images_in_dir(TWITCH_DSET / 'raw-frames') + sleep(.1) diff --git a/common/research/scripts/split_video.py b/common/research/scripts/split_video.py new file mode 100644 index 0000000000000000000000000000000000000000..dffc460fdd0fe89e4d52024d1cd1860a9bd16bef --- /dev/null +++ b/common/research/scripts/split_video.py @@ -0,0 +1,8 @@ +import sys + +from research.dataset.twitch.make_thumbnails import ThumbnailsGenerator + +if __name__ == '__main__': + _video_name = sys.argv[1] + print(f'Fragmenting video {_video_name}') + ThumbnailsGenerator(_video_name).run()