From f4f5ca0dff4bf1d7faf22105da7fb141a28d504b Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 16 Feb 2021 13:30:43 -0800 Subject: [PATCH 1/3] Add check_imshow() --- detect.py | 8 ++++---- utils/general.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/detect.py b/detect.py index 3f1d6c521b67..22bf21b4c825 100644 --- a/detect.py +++ b/detect.py @@ -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 @@ -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: @@ -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: diff --git a/utils/general.py b/utils/general.py index 24807483f5f4..9b2b0b71d0a2 100755 --- a/utils/general.py +++ b/utils/general.py @@ -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: + print('WARNING: Environment does not support cv2.imshow() or PIL.Image.show() image displays') + return False + + def check_file(file): # Search for file if not found if os.path.isfile(file) or file == '': From 2d9bd5f2b78b28321afd949f79207e49c5504d49 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 16 Feb 2021 13:47:07 -0800 Subject: [PATCH 2/3] Update general.py --- utils/general.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/general.py b/utils/general.py index 9b2b0b71d0a2..1ec9621f50c2 100755 --- a/utils/general.py +++ b/utils/general.py @@ -103,8 +103,8 @@ def check_imshow(): cv2.destroyAllWindows() cv2.waitKey(1) return True - except: - print('WARNING: Environment does not support cv2.imshow() or PIL.Image.show() image displays') + except Exception as e: + print(f'WARNING: Environment does not support cv2.imshow() or PIL Image.show() image previews.\n{e}') return False From 0b5e9cdaf706773133175d8f08af20c86e4205c3 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 16 Feb 2021 13:56:34 -0800 Subject: [PATCH 3/3] Update general.py --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 1ec9621f50c2..2d3e83ede35e 100755 --- a/utils/general.py +++ b/utils/general.py @@ -104,7 +104,7 @@ def check_imshow(): 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}') + print(f'WARNING: Environment does not support cv2.imshow() or PIL Image.show() image previews\n{e}') return False