Security fixes for IPython (#11069)
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
1 parent
7dee52f94d
commit
3c0a6e664b
4 files changed
+24
-12
No files matched your search
+6
-3
@@ -21,14 +21,13 @@ import pandas as pd
|
|||||||
import requests
|
import requests
|
||||||
import torch
|
import torch
|
||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
from IPython.display import display
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from torch.cuda import amp
|
from torch.cuda import amp
|
||||||
|
|
||||||
from utils import TryExcept
|
from utils import TryExcept
|
||||||
from utils.dataloaders import exif_transpose, letterbox
|
from utils.dataloaders import exif_transpose, letterbox
|
||||||
from utils.general import (LOGGER, ROOT, Profile, check_requirements, check_suffix, check_version, colorstr,
|
from utils.general import (LOGGER, ROOT, Profile, check_requirements, check_suffix, check_version, colorstr,
|
||||||
increment_path, is_notebook, make_divisible, non_max_suppression, scale_boxes, xywh2xyxy,
|
increment_path, is_jupyter, make_divisible, non_max_suppression, scale_boxes, xywh2xyxy,
|
||||||
xyxy2xywh, yaml_load)
|
xyxy2xywh, yaml_load)
|
||||||
from utils.plots import Annotator, colors, save_one_box
|
from utils.plots import Annotator, colors, save_one_box
|
||||||
from utils.torch_utils import copy_attr, smart_inference_mode
|
from utils.torch_utils import copy_attr, smart_inference_mode
|
||||||
@@ -767,7 +766,11 @@ class Detections:
|
|||||||
|
|
||||||
im = Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im # from np
|
im = Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im # from np
|
||||||
if show:
|
if show:
|
||||||
display(im) if is_notebook() else im.show(self.files[i])
|
if is_jupyter():
|
||||||
|
from IPython.display import display
|
||||||
|
display(im)
|
||||||
|
else:
|
||||||
|
im.show(self.files[i])
|
||||||
if save:
|
if save:
|
||||||
f = self.files[i]
|
f = self.files[i]
|
||||||
im.save(save_dir / f) # save
|
im.save(save_dir / f) # save
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
# Base ------------------------------------------------------------------------
|
# Base ------------------------------------------------------------------------
|
||||||
gitpython>=3.1.30
|
gitpython>=3.1.30
|
||||||
ipython # interactive notebook
|
|
||||||
matplotlib>=3.2.2
|
matplotlib>=3.2.2
|
||||||
numpy>=1.18.5
|
numpy>=1.18.5
|
||||||
opencv-python>=4.1.1
|
opencv-python>=4.1.1
|
||||||
@@ -43,6 +42,7 @@ setuptools>=65.5.1 # Snyk vulnerability fix
|
|||||||
# tritonclient[all]~=2.24.0
|
# tritonclient[all]~=2.24.0
|
||||||
|
|
||||||
# Extras ----------------------------------------------------------------------
|
# Extras ----------------------------------------------------------------------
|
||||||
|
# ipython # interactive notebook
|
||||||
# mss # screenshots
|
# mss # screenshots
|
||||||
# albumentations>=1.0.3
|
# albumentations>=1.0.3
|
||||||
# pycocotools>=2.0.6 # COCO mAP
|
# pycocotools>=2.0.6 # COCO mAP
|
||||||
|
|||||||
+4
-2
@@ -60,17 +60,19 @@ def notebook_init(verbose=True):
|
|||||||
check_font()
|
check_font()
|
||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
from IPython import display # to display images and clear console output
|
|
||||||
|
|
||||||
if is_colab():
|
if is_colab():
|
||||||
shutil.rmtree('/content/sample_data', ignore_errors=True) # remove colab /sample_data directory
|
shutil.rmtree('/content/sample_data', ignore_errors=True) # remove colab /sample_data directory
|
||||||
|
|
||||||
# System info
|
# System info
|
||||||
|
display = None
|
||||||
if verbose:
|
if verbose:
|
||||||
gb = 1 << 30 # bytes to GiB (1024 ** 3)
|
gb = 1 << 30 # bytes to GiB (1024 ** 3)
|
||||||
ram = psutil.virtual_memory().total
|
ram = psutil.virtual_memory().total
|
||||||
total, used, free = shutil.disk_usage('/')
|
total, used, free = shutil.disk_usage('/')
|
||||||
display.clear_output()
|
with contextlib.suppress(Exception): # clear display if ipython is installed
|
||||||
|
from IPython import display
|
||||||
|
display.clear_output()
|
||||||
s = f'({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)'
|
s = f'({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)'
|
||||||
else:
|
else:
|
||||||
s = ''
|
s = ''
|
||||||
|
|||||||
+13
-6
@@ -29,7 +29,6 @@ from typing import Optional
|
|||||||
from zipfile import ZipFile, is_zipfile
|
from zipfile import ZipFile, is_zipfile
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import IPython
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import pkg_resources as pkg
|
import pkg_resources as pkg
|
||||||
@@ -77,10 +76,18 @@ def is_colab():
|
|||||||
return 'google.colab' in sys.modules
|
return 'google.colab' in sys.modules
|
||||||
|
|
||||||
|
|
||||||
def is_notebook():
|
def is_jupyter():
|
||||||
# Is environment a Jupyter notebook? Verified on Colab, Jupyterlab, Kaggle, Paperspace
|
"""
|
||||||
ipython_type = str(type(IPython.get_ipython()))
|
Check if the current script is running inside a Jupyter Notebook.
|
||||||
return 'colab' in ipython_type or 'zmqshell' in ipython_type
|
Verified on Colab, Jupyterlab, Kaggle, Paperspace.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if running inside a Jupyter Notebook, False otherwise.
|
||||||
|
"""
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
from IPython import get_ipython
|
||||||
|
return get_ipython() is not None
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def is_kaggle():
|
def is_kaggle():
|
||||||
@@ -429,7 +436,7 @@ def check_img_size(imgsz, s=32, floor=0):
|
|||||||
def check_imshow(warn=False):
|
def check_imshow(warn=False):
|
||||||
# Check if environment supports image displays
|
# Check if environment supports image displays
|
||||||
try:
|
try:
|
||||||
assert not is_notebook()
|
assert not is_jupyter()
|
||||||
assert not is_docker()
|
assert not is_docker()
|
||||||
cv2.imshow('test', np.zeros((1, 1, 3)))
|
cv2.imshow('test', np.zeros((1, 1, 3)))
|
||||||
cv2.waitKey(1)
|
cv2.waitKey(1)
|
||||||
|
|||||||
Reference in new issue
Block a user