|
| 1 | +import os |
| 2 | +import json |
| 3 | +import pickle |
| 4 | +import warnings |
| 5 | + |
| 6 | +from .methods import LucasKanade, SimplifiedOpticalFlow, DirectionalLucasKanade, IDIMethod |
| 7 | +from .video_reader import VideoReader |
| 8 | + |
| 9 | +method_mappings = { |
| 10 | + "LucasKanade": LucasKanade, |
| 11 | + "SimplifiedOpticalFlow": SimplifiedOpticalFlow, |
| 12 | + "DirectionalLucasKanade": DirectionalLucasKanade, |
| 13 | +} |
| 14 | + |
| 15 | +def load_analysis(analysis_path, input_file=None, load_results=True, root=None): |
| 16 | + """Load the previous analysis and create a pyIDI object. |
| 17 | +
|
| 18 | + :param analysis_path: Path to analysis folder (e.g. video_pyidi_analysis/analysis_001/) |
| 19 | + :type analysis_path: str |
| 20 | + :param input_file: new location of the cih file, if None, the location in settings.txt |
| 21 | + is used, defaults to None |
| 22 | + :type input_file: str or None, optional |
| 23 | + :param load_results: if False, the displacements are not loaded, |
| 24 | + only points and settings, defaults to True |
| 25 | + :type load_results: bool, optional |
| 26 | + :param root: root directory for the analysis (needed when the ``VideoReader`` requires it), |
| 27 | + defaults to None. |
| 28 | + :type root: str or None, optional |
| 29 | + :return: pyIDI object and settings dict |
| 30 | + :rtype: tuple |
| 31 | + """ |
| 32 | + with open(os.path.join(analysis_path, 'settings.json'), 'r') as f: |
| 33 | + settings = json.load(f) |
| 34 | + |
| 35 | + if input_file is None: |
| 36 | + video = VideoReader(settings['input_file'], root=root) |
| 37 | + else: |
| 38 | + video = VideoReader(input_file, root=root) |
| 39 | + |
| 40 | + method_name = settings['method'] |
| 41 | + if method_name not in method_mappings: |
| 42 | + raise ValueError(f"Method {method_name} not one of {list(method_mappings.keys())}") |
| 43 | + |
| 44 | + idi: IDIMethod = method_mappings[method_name](video) |
| 45 | + |
| 46 | + with open(os.path.join(analysis_path, 'points.pkl'), 'rb') as f: |
| 47 | + points = pickle.load(f) |
| 48 | + |
| 49 | + if load_results: |
| 50 | + with open(os.path.join(analysis_path, 'results.pkl'), 'rb') as f: |
| 51 | + results = pickle.load(f) |
| 52 | + |
| 53 | + idi.displacements = results |
| 54 | + |
| 55 | + idi.set_points(points) |
| 56 | + |
| 57 | + return video, idi, settings['settings'] |
| 58 | + |
0 commit comments