Skip to content

Commit

Permalink
Add check_imshow() (ultralytics#2231)
Browse files Browse the repository at this point in the history
* Add check_imshow()

* Update general.py

* Update general.py
  • Loading branch information
glenn-jocher committed Feb 16, 2021
1 parent c017c75 commit b08667f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, non_max_suppression, apply_classifier, scale_coords, \
xyxy2xywh, strip_optimizer, set_logging, increment_path
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path
from utils.plots import plot_one_box
from utils.torch_utils import select_device, load_classifier, time_synchronized

Expand Down Expand Up @@ -45,7 +45,7 @@ def detect(save_img=False):
# Set Dataloader
vid_path, vid_writer = None, None
if webcam:
view_img = True
view_img = check_imshow()
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=imgsz, stride=stride)
else:
Expand Down Expand Up @@ -118,7 +118,7 @@ def detect(save_img=False):
# Stream results
if view_img:
cv2.imshow(str(p), im0)
cv2.waitKey(1) # 1 millisecond
cv2.waitKey(1) # 1 millisecond

# Save results (image with detections)
if save_img:
Expand Down
13 changes: 13 additions & 0 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ def check_img_size(img_size, s=32):
return new_size


def check_imshow():
# Check if environment supports image displays
try:
cv2.imshow('test', np.zeros((1, 1, 3)))
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)
return True
except Exception as e:
print(f'WARNING: Environment does not support cv2.imshow() or PIL Image.show() image previews\n{e}')
return False


def check_file(file):
# Search for file if not found
if os.path.isfile(file) or file == '':
Expand Down

0 comments on commit b08667f

Please sign in to comment.