Skip to content

Commit 68b4d6c

Browse files
authored
Add is_colab() function (ultralytics#3018)
1 parent 17f6f4f commit 68b4d6c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

utils/general.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,20 @@ def get_latest_run(search_dir='.'):
5151
return max(last_list, key=os.path.getctime) if last_list else ''
5252

5353

54-
def isdocker():
54+
def is_docker():
5555
# Is environment a Docker container
5656
return Path('/workspace').exists() # or Path('/.dockerenv').exists()
5757

5858

59+
def is_colab():
60+
# Is environment a Google Colab instance
61+
try:
62+
import google.colab
63+
return True
64+
except Exception as e:
65+
return False
66+
67+
5968
def emojis(str=''):
6069
# Return platform-dependent emoji-safe version of string
6170
return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str
@@ -81,7 +90,7 @@ def check_git_status():
8190
print(colorstr('github: '), end='')
8291
try:
8392
assert Path('.git').exists(), 'skipping check (not a git repository)'
84-
assert not isdocker(), 'skipping check (Docker image)'
93+
assert not is_docker(), 'skipping check (Docker image)'
8594
assert check_online(), 'skipping check (offline)'
8695

8796
cmd = 'git fetch && git config --get remote.origin.url'
@@ -138,7 +147,8 @@ def check_img_size(img_size, s=32):
138147
def check_imshow():
139148
# Check if environment supports image displays
140149
try:
141-
assert not isdocker(), 'cv2.imshow() is disabled in Docker environments'
150+
assert not is_docker(), 'cv2.imshow() is disabled in Docker environments'
151+
assert not is_colab(), 'cv2.imshow() is disabled in Google Colab environments'
142152
cv2.imshow('test', np.zeros((1, 1, 3)))
143153
cv2.waitKey(1)
144154
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)