from ..patch_match import PyramidPatchMatcher import functools, os import numpy as np from PIL import Image from tqdm import tqdm class TableManager: def __init__(self): pass def task_list(self, n): tasks = [] max_level = 1 while (1<=n: break meta_data = { "source": i, "target": j, "level": level + 1 } tasks.append(meta_data) tasks.sort(key=functools.cmp_to_key(lambda u, v: u["level"]-v["level"])) return tasks def build_remapping_table(self, frames_guide, frames_style, patch_match_engine, batch_size, desc=""): n = len(frames_guide) tasks = self.task_list(n) remapping_table = [[(frames_style[i], 1)] for i in range(n)] for batch_id in tqdm(range(0, len(tasks), batch_size), desc=desc): tasks_batch = tasks[batch_id: min(batch_id+batch_size, len(tasks))] source_guide = np.stack([frames_guide[task["source"]] for task in tasks_batch]) target_guide = np.stack([frames_guide[task["target"]] for task in tasks_batch]) source_style = np.stack([frames_style[task["source"]] for task in tasks_batch]) _, target_style = patch_match_engine.estimate_nnf(source_guide, target_guide, source_style) for task, result in zip(tasks_batch, target_style): target, level = task["target"], task["level"] if len(remapping_table[target])==level: remapping_table[target].append((result, 1)) else: frame, weight = remapping_table[target][level] remapping_table[target][level] = ( frame * (weight / (weight + 1)) + result / (weight + 1), weight + 1 ) return remapping_table def remapping_table_to_blending_table(self, table): for i in range(len(table)): for j in range(1, len(table[i])): frame_1, weight_1 = table[i][j-1] frame_2, weight_2 = table[i][j] frame = (frame_1 + frame_2) / 2 weight = weight_1 + weight_2 table[i][j] = (frame, weight) return table def tree_query(self, leftbound, rightbound): node_list = [] node_index = rightbound while node_index>=leftbound: node_level = 0 while (1<=leftbound: node_level += 1 node_list.append((node_index, node_level)) node_index -= 1<