glenn-jocher commited on
Commit
496ec33
1 Parent(s): 68f6361

Update detect.py

Browse files

Added some recent updates that were missing, and updated the filename with an if else.

Files changed (1) hide show
  1. detect.py +9 -7
detect.py CHANGED
@@ -46,7 +46,7 @@ def detect(save_img=False):
46
  dataset = LoadImages(source, img_size=imgsz)
47
 
48
  # Get names and colors
49
- names = model.names if hasattr(model, 'names') else model.modules.names
50
  colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]
51
 
52
  # Run inference
@@ -80,6 +80,7 @@ def detect(save_img=False):
80
  p, s, im0 = path, '', im0s
81
 
82
  save_path = str(Path(out) / Path(p).name)
 
83
  s += '%gx%g ' % img.shape[2:] # print string
84
  gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh
85
  if det is not None and len(det):
@@ -95,12 +96,8 @@ def detect(save_img=False):
95
  for *xyxy, conf, cls in det:
96
  if save_txt: # Write to file
97
  xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
98
- if dataset.frame == 0:
99
- with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as f:
100
- f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
101
- else:
102
- with open(save_path[:save_path.rfind('.')] + '_' + str(dataset.frame) + '.txt', 'a') as f:
103
- f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
104
 
105
  if save_img or view_img: # Add bbox to image
106
  label = '%s %.2f' % (names[int(cls)], conf)
@@ -160,3 +157,8 @@ if __name__ == '__main__':
160
 
161
  with torch.no_grad():
162
  detect()
 
 
 
 
 
 
46
  dataset = LoadImages(source, img_size=imgsz)
47
 
48
  # Get names and colors
49
+ names = model.module.names if hasattr(model, 'module') else model.names
50
  colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]
51
 
52
  # Run inference
 
80
  p, s, im0 = path, '', im0s
81
 
82
  save_path = str(Path(out) / Path(p).name)
83
+ txt_path = save_path[:save_path.rfind('.')] + ('_%g' % dataset.frame if dataset.mode == 'video' else '')
84
  s += '%gx%g ' % img.shape[2:] # print string
85
  gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh
86
  if det is not None and len(det):
 
96
  for *xyxy, conf, cls in det:
97
  if save_txt: # Write to file
98
  xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
99
+ with open(txt_path + '.txt', 'a') as f:
100
+ f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
 
 
 
 
101
 
102
  if save_img or view_img: # Add bbox to image
103
  label = '%s %.2f' % (names[int(cls)], conf)
 
157
 
158
  with torch.no_grad():
159
  detect()
160
+
161
+ # Update all models
162
+ # for opt.weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt', 'yolov3-spp.pt']:
163
+ # detect()
164
+ # create_pretrained(opt.weights, opt.weights)