Skip to content

Instantly share code, notes, and snippets.

View ThomasParistech's full-sized avatar
🚴

Thomas Rouch ThomasParistech

🚴
View GitHub Profile
import math
import multiprocessing
from typing import Optional
import imagesize
import psutil
from psutil._common import bytes2human
def bytes_of_uint8_img(img_path: str, colored: bool) -> int:
# /usr/bin/python3
"""Process files in parallel."""
import math
from typing import Callable
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
import numpy as np
# /usr/bin/python3
"""Multiprocess folder of images"""
import os
from typing import List
import cv2
import numpy as np
from multi_process import KwargsType
from multi_process import multiprocess
def _profiled_batch_func(process_func: Callable[..., None],
list_kwargs: List[KwargsType],
shared_kwargs: KwargsType) -> List[Tuple[float, float]]:
"""Process and profile a batch"""
profiling_events: List[Tuple[float, float]] = []
for kwargs in list_kwargs:
start_time = time.perf_counter()
process_func(**kwargs, **shared_kwargs)
end_time = time.perf_counter()
if PROFILE:
profiling_events = pool.map_unordered(_profiled_batch_func, params,
progress_bar=True)
fname = get_function_name(process_func)
for idx, list_events in enumerate(profiling_events):
for start_time, end_time in list_events:
push_profiling_event(fname, start_time, end_time,
f"Multiprocess_{idx}")
else:
pool.map_unordered(_batch_func, params, progress_bar=True)
from typing import List
import cv2
import numpy as np
def equirec_to_cubemap(equirec: np.ndarray, out_size: int) -> List[np.ndarray]:
"""Convert an equirectangular image to a list of cubefaces (FRBLUD)"""
height, width = equirec.shape[:2]
u, v = np.meshgrid(sample_pixels(-1, 1, out_size),
import numpy as np
def get_up_mask(height: int, width: int) -> np.ndarray:
"""
Get the mask corresponding to the projection of the UP cubeface
on the equirectangular image
"""
mask = np.zeros((height, width // 4), bool)
from typing import Tuple
import numpy as np
def norm_to_face(face_size: int, mat: np.ndarray) -> np.ndarray:
"""Convert from [-1,1] tp [0, face_size]"""
return np.clip((mat+1)*0.5*face_size, 0, face_size)
def get_frbl_maps(out_width: int,
from typing import Tuple
import numpy as np
def get_ud_maps(out_width: int,
out_height: int,
face_size: int) -> Tuple[np.ndarray, np.ndarray]:
"""Generate xy-maps containing UP,DOWN info"""
theta = sample_pixels(-np.pi, np.pi, out_width)
phi = sample_pixels(-np.pi/2, np.pi/2, out_height)
from typing import List
import numpy as np
import cv2
def cubemap_to_equirec(cubefaces: List[np.ndarray]) -> np.ndarray:
"""Convert a list of cubefaces (FRBLUD) into an equirectangular image"""
# Stack cubefaces horizontally
cubemap = np.hstack(cubefaces)