glenn-jocher commited on
Commit
26c2e54
1 Parent(s): f8464b4

Add check_imshow() (#2231)

Browse files

* Add check_imshow()

* Update general.py

* Update general.py

Files changed (2) hide show
  1. detect.py +4 -4
  2. utils/general.py +13 -0
detect.py CHANGED
@@ -9,8 +9,8 @@ from numpy import random
9
 
10
  from models.experimental import attempt_load
11
  from utils.datasets import LoadStreams, LoadImages
12
- from utils.general import check_img_size, check_requirements, non_max_suppression, apply_classifier, scale_coords, \
13
- xyxy2xywh, strip_optimizer, set_logging, increment_path
14
  from utils.plots import plot_one_box
15
  from utils.torch_utils import select_device, load_classifier, time_synchronized
16
 
@@ -45,7 +45,7 @@ def detect(save_img=False):
45
  # Set Dataloader
46
  vid_path, vid_writer = None, None
47
  if webcam:
48
- view_img = True
49
  cudnn.benchmark = True # set True to speed up constant image size inference
50
  dataset = LoadStreams(source, img_size=imgsz, stride=stride)
51
  else:
@@ -118,7 +118,7 @@ def detect(save_img=False):
118
  # Stream results
119
  if view_img:
120
  cv2.imshow(str(p), im0)
121
- cv2.waitKey(1) # 1 millisecond
122
 
123
  # Save results (image with detections)
124
  if save_img:
 
9
 
10
  from models.experimental import attempt_load
11
  from utils.datasets import LoadStreams, LoadImages
12
+ from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
13
+ scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path
14
  from utils.plots import plot_one_box
15
  from utils.torch_utils import select_device, load_classifier, time_synchronized
16
 
 
45
  # Set Dataloader
46
  vid_path, vid_writer = None, None
47
  if webcam:
48
+ view_img = check_imshow()
49
  cudnn.benchmark = True # set True to speed up constant image size inference
50
  dataset = LoadStreams(source, img_size=imgsz, stride=stride)
51
  else:
 
118
  # Stream results
119
  if view_img:
120
  cv2.imshow(str(p), im0)
121
+ cv2.waitKey(1) # 1 millisecond
122
 
123
  # Save results (image with detections)
124
  if save_img:
utils/general.py CHANGED
@@ -95,6 +95,19 @@ def check_img_size(img_size, s=32):
95
  return new_size
96
 
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  def check_file(file):
99
  # Search for file if not found
100
  if os.path.isfile(file) or file == '':
 
95
  return new_size
96
 
97
 
98
+ def check_imshow():
99
+ # Check if environment supports image displays
100
+ try:
101
+ cv2.imshow('test', np.zeros((1, 1, 3)))
102
+ cv2.waitKey(1)
103
+ cv2.destroyAllWindows()
104
+ cv2.waitKey(1)
105
+ return True
106
+ except Exception as e:
107
+ print(f'WARNING: Environment does not support cv2.imshow() or PIL Image.show() image previews\n{e}')
108
+ return False
109
+
110
+
111
  def check_file(file):
112
  # Search for file if not found
113
  if os.path.isfile(file) or file == '':