Bai360/Cotton2
0
1# YOLOv5 ๐ by Ultralytics, GPL-3.0 license2"""3utils/initialization4"""5 6import contextlib7import platform8import threading9 10 11def emojis(str=''):12 # Return platform-dependent emoji-safe version of string13 return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str14 15 16class TryExcept(contextlib.ContextDecorator):17 # YOLOv5 TryExcept class. Usage: @TryExcept() decorator or 'with TryExcept():' context manager18 def __init__(self, msg=''):19 self.msg = msg20 21 def __enter__(self):22 pass23 24 def __exit__(self, exc_type, value, traceback):25 if value:26 print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}"))27 return True28 29 30def threaded(func):31 # Multi-threads a target function and returns thread. Usage: @threaded decorator32 def wrapper(*args, **kwargs):33 thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True)34 thread.start()35 return thread36 37 return wrapper38 39 40def join_threads(verbose=False):41 # Join all daemon threads, i.e. atexit.register(lambda: join_threads())42 main_thread = threading.current_thread()43 for t in threading.enumerate():44 if t is not main_thread:45 if verbose:46 print(f'Joining thread {t.name}')47 t.join()48 49 50def notebook_init(verbose=True):51 # Check system software and hardware52 print('Checking setup...')53 54 import os55 import shutil56 57 from utils.general import check_font, check_requirements, is_colab58 from utils.torch_utils import select_device # imports59 60 check_font()61 62 import psutil63 from IPython import display # to display images and clear console output64 65 if is_colab():66 shutil.rmtree('/content/sample_data', ignore_errors=True) # remove colab /sample_data directory67 68 # System info69 if verbose:70 gb = 1 << 30 # bytes to GiB (1024 ** 3)71 ram = psutil.virtual_memory().total72 total, used, free = shutil.disk_usage("/")73 display.clear_output()74 s = f'({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)'75 else:76 s = ''77 78 select_device(newline=False)79 print(emojis(f'Setup complete โ
{s}'))80 return display81 