k20hcmus/FishEye8K
3
1import contextlib2import platform3import threading4 5 6def emojis(str=''):7 # Return platform-dependent emoji-safe version of string8 return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str9 10 11class TryExcept(contextlib.ContextDecorator):12 # YOLOv5 TryExcept class. Usage: @TryExcept() decorator or 'with TryExcept():' context manager13 def __init__(self, msg=''):14 self.msg = msg15 16 def __enter__(self):17 pass18 19 def __exit__(self, exc_type, value, traceback):20 if value:21 print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}"))22 return True23 24 25def threaded(func):26 # Multi-threads a target function and returns thread. Usage: @threaded decorator27 def wrapper(*args, **kwargs):28 thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True)29 thread.start()30 return thread31 32 return wrapper33 34 35def join_threads(verbose=False):36 # Join all daemon threads, i.e. atexit.register(lambda: join_threads())37 main_thread = threading.current_thread()38 for t in threading.enumerate():39 if t is not main_thread:40 if verbose:41 print(f'Joining thread {t.name}')42 t.join()43 44 45def notebook_init(verbose=True):46 # Check system software and hardware47 print('Checking setup...')48 49 import os50 import shutil51 52 from utils.general import check_font, check_requirements, is_colab53 from utils.torch_utils import select_device # imports54 55 check_font()56 57 import psutil58 from IPython import display # to display images and clear console output59 60 if is_colab():61 shutil.rmtree('/content/sample_data', ignore_errors=True) # remove colab /sample_data directory62 63 # System info64 if verbose:65 gb = 1 << 30 # bytes to GiB (1024 ** 3)66 ram = psutil.virtual_memory().total67 total, used, free = shutil.disk_usage("/")68 display.clear_output()69 s = f'({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)'70 else:71 s = ''72 73 select_device(newline=False)74 print(emojis(f'Setup complete ✅ {s}'))75 return display76 