# Path: util/util_visualize.py
# def convert2rgb(tensor, key, **kargs):
#     found = False
#     for k in KEYWORDS:
#         if k in key:
#             convert = getattr(Converter, k + '2img')
#             found = True
#             break
#     if not found:
#         return None
#     else:
#         return convert(tensor, **kargs)
# 
# def is_key_image(key):
#     """check if the given key correspondes to images
# 
#     Arguments:
#         key {str} -- key of a data pack
# 
#     Returns:
#         bool -- [True if the given key correspondes to an image]
#     """
# 
#     for k in KEYWORDS:
#         if k in key:
#             return True
#     return False
# 
# Path: util/util_flow.py
# def flow2img(flow_data, maxrad=None):
#     """
#     convert optical flow into color image
#     :param flow_data:
#     :return: color image
#     """
#     # print(flow_data.shape)
#     # print(type(flow_data))
#     u = flow_data[:, :, 0]
#     v = flow_data[:, :, 1]
# 
#     UNKNOW_FLOW_THRESHOLD = 1e7
#     pr1 = abs(u) > UNKNOW_FLOW_THRESHOLD
#     pr2 = abs(v) > UNKNOW_FLOW_THRESHOLD
#     idx_unknown = (pr1 | pr2)
#     u[idx_unknown] = v[idx_unknown] = 0
# 
#     # get max value in each direction
#     '''
#     maxu = -999.
#     maxv = -999.
#     minu = 999.
#     minv = 999.
#     maxu = max(maxu, np.max(u))
#     maxv = max(maxv, np.max(v))
#     minu = min(minu, np.min(u))
#     minv = min(minv, np.min(v))
#     '''
# 
#     rad = np.sqrt(u ** 2 + v ** 2)
#     if maxrad is None:
#         maxrad = max(-1, np.max(rad))
#     u = u / maxrad + np.finfo(float).eps
#     v = v / maxrad + np.finfo(float).eps
# 
#     img = compute_color(u, v)
# 
#     idx = np.repeat(idx_unknown[:, :, np.newaxis], 3, axis=2)
#     img[idx] = 0
# 
#     return img


from util.util_visualize import convert2rgb, is_key_image
from torch.multiprocessing import Pool
from util.util_flow import flow2img
from os.path import join
from PIL import Image
import atexit
import numpy as np
        tags = pack['tags'] if 'tags' in pack.keys() else None
        for b in range(batch_size):
            content += "<tr>\n"
            for k in sorted(list(pack.keys())):
                # get the ones that are useful:
                if is_key_image(k):
                    if tags is not None:
                        prefix = '%s_%s.png' % (k, tags[b])
                    else:
                        prefix = '%s_%04d_%04d.png' % (k, batch_idx, b)
                    if epoch_folder is not None:
                        link = join(epoch_folder, prefix)
                    else:
                        link = prefix
                    # html is at outdir/../, so link is epochXXX/
                    content += f"<td><img src=\"{link}\" style=\"max-width:100%;height:auto;\"></td>\n"
            content += "</tr>\n"
        self.html_logger.epoch_content['content'] += content

    @staticmethod
    def _visualize(pack, batch_idx, outdir):
        # this thread saves the packed tensor into individual images
        batch_size = pack['batch_size']
        tags = pack['tags'] if 'tags' in pack.keys() else None
        for k, v in pack.items():
            rgb_tensor = convert2rgb(v, k)
            if rgb_tensor is None:
                continue
            for b in range(batch_size):
                if 'flow' in k:

# Path: util/util_visualize.py
# def convert2rgb(tensor, key, **kargs):
#     found = False
#     for k in KEYWORDS:
#         if k in key:
#             convert = getattr(Converter, k + '2img')
#             found = True
#             break
#     if not found:
#         return None
#     else:
#         return convert(tensor, **kargs)
# 
# def is_key_image(key):
#     """check if the given key correspondes to images
# 
#     Arguments:
#         key {str} -- key of a data pack
# 
#     Returns:
#         bool -- [True if the given key correspondes to an image]
#     """
# 
#     for k in KEYWORDS:
#         if k in key:
#             return True
#     return False
# 
# Path: util/util_flow.py
# def flow2img(flow_data, maxrad=None):
#     """
#     convert optical flow into color image
#     :param flow_data:
#     :return: color image
#     """
#     # print(flow_data.shape)
#     # print(type(flow_data))
#     u = flow_data[:, :, 0]
#     v = flow_data[:, :, 1]
# 
#     UNKNOW_FLOW_THRESHOLD = 1e7
#     pr1 = abs(u) > UNKNOW_FLOW_THRESHOLD
#     pr2 = abs(v) > UNKNOW_FLOW_THRESHOLD
#     idx_unknown = (pr1 | pr2)
#     u[idx_unknown] = v[idx_unknown] = 0
# 
#     # get max value in each direction
#     '''
#     maxu = -999.
#     maxv = -999.
#     minu = 999.
#     minv = 999.
#     maxu = max(maxu, np.max(u))
#     maxv = max(maxv, np.max(v))
#     minu = min(minu, np.min(u))
#     minv = min(minv, np.min(v))
#     '''
# 
#     rad = np.sqrt(u ** 2 + v ** 2)
#     if maxrad is None:
#         maxrad = max(-1, np.max(rad))
#     u = u / maxrad + np.finfo(float).eps
#     v = v / maxrad + np.finfo(float).eps
# 
#     img = compute_color(u, v)
# 
#     idx = np.repeat(idx_unknown[:, :, np.newaxis], 3, axis=2)
#     img[idx] = 0
# 
#     return img


# Path: visualize/html_visualizer.py
from util.util_visualize import convert2rgb, is_key_image
from torch.multiprocessing import Pool
from util.util_flow import flow2img
from os.path import join
from PIL import Image
import atexit
import numpy as np

        tags = pack['tags'] if 'tags' in pack.keys() else None
        for b in range(batch_size):
            content += "<tr>\n"
            for k in sorted(list(pack.keys())):
                # get the ones that are useful:
                if is_key_image(k):
                    if tags is not None:
                        prefix = '%s_%s.png' % (k, tags[b])
                    else:
                        prefix = '%s_%04d_%04d.png' % (k, batch_idx, b)
                    if epoch_folder is not None:
                        link = join(epoch_folder, prefix)
                    else:
                        link = prefix
                    # html is at outdir/../, so link is epochXXX/
                    content += f"<td><img src=\"{link}\" style=\"max-width:100%;height:auto;\"></td>\n"
            content += "</tr>\n"
        self.html_logger.epoch_content['content'] += content

    @staticmethod
    def _visualize(pack, batch_idx, outdir):
        # this thread saves the packed tensor into individual images
        batch_size = pack['batch_size']
        tags = pack['tags'] if 'tags' in pack.keys() else None
        for k, v in pack.items():
            rgb_tensor = convert2rgb(v, k)
            if rgb_tensor is None:
                continue
            for b in range(batch_size):
                if 'flow' in k:

                    img = flow2img(rgb_tensor[b, :, :, :]) / 255