oskarastrom commited on
Commit
a697d26
·
1 Parent(s): 81970c6

Updated detection

Browse files
Files changed (2) hide show
  1. dataloader.py +1 -1
  2. scripts/detect_frames.py +35 -61
dataloader.py CHANGED
@@ -138,7 +138,7 @@ class YOLOFrameDataset(Dataset):
138
  return img, (h0, w0), (h1, w1) # img, hw_original, hw_resized
139
 
140
  def __len__(self):
141
- return len(self.batches)
142
 
143
  def __iter__(self):
144
  for batch_idx in self.batch_indices:
 
138
  return img, (h0, w0), (h1, w1) # img, hw_original, hw_resized
139
 
140
  def __len__(self):
141
+ return len(self.batch_indices)
142
 
143
  def __iter__(self):
144
  for batch_idx in self.batch_indices:
scripts/detect_frames.py CHANGED
@@ -39,6 +39,8 @@ def main(args, config={}, verbose=True):
39
  print(config)
40
 
41
  dirname = args.frames
 
 
42
 
43
  locations = [
44
  "elwha-val"
@@ -46,84 +48,56 @@ def main(args, config={}, verbose=True):
46
  for loc in locations:
47
 
48
  in_loc_dir = os.path.join(dirname, loc)
49
- out_dir = os.path.join(args.output, loc, "tracker", "data")
50
  print(in_loc_dir)
51
- print(out_dir)
52
-
53
- # run detection + tracking
54
 
55
  seq_list = os.listdir(in_loc_dir)
56
 
57
- iterate_sequences(in_loc_dir, out_dir, config, args.weights, seq_list, verbose)
58
 
59
 
60
- def iterate_sequences(in_dir, out_dir, config, weights, seq_list, verbose):
61
- model, device = setup_model(weights)
62
 
63
- idx = 1
64
  with tqdm(total=len(seq_list), desc="...", ncols=0) as pbar:
65
- for seq in seq_list:
66
- ann_list = []
67
- pbar.update(1)
68
- if (seq.startswith(".")): continue
69
- pbar.set_description("Processing " + seq)
70
- if verbose:
71
- print(" ")
72
- print("(" + str(idx) + "/" + str(len(seq_list)) + ") " + seq)
73
- print(" ")
74
- idx += 1
75
- in_seq_dir = os.path.join(in_dir, seq)
76
- frame_list = detect(in_seq_dir, out_dir, config, seq, model, device, verbose)
77
- for frame in frame_list:
78
- if frame is not None:
79
- for ann in frame:
80
- ann_list.append({
81
- 'image_id': ann[5],
82
- 'category_id': 0,
83
- 'bbox': [ann[0], ann[1], ann[2] - ann[0], ann[3] - ann[1]],
84
- 'score': ann[4]
85
- })
86
- result = json.dumps(ann_list)
87
-
88
- out_seq_dir = os.path.join(out_dir, seq)
89
- os.makedirs(out_seq_dir, exist_ok=True)
90
- with open(os.path.join(out_seq_dir, 'pred.json'), 'w') as f:
91
- f.write(result)
92
-
93
- def iterate_files(in_dir, out_dir, config, weights, verbose):
94
- model, device = setup_model(weights)
95
 
96
- ann_list = []
97
- frame_list = detect(in_dir, out_dir, config, "specified folder", model, device, verbose)
98
- with tqdm(total=len(frame_list), desc="...", ncols=0) as pbar:
99
- for frame in frame_list:
100
- if frame is not None:
101
- for ann in frame:
102
- ann_list.append({
103
- 'image_id': ann[5],
104
- 'category_id': 0,
105
- 'bbox': [ann[0], ann[1], ann[2] - ann[0], ann[3] - ann[1]],
106
- 'score': ann[4]
107
- })
108
  pbar.update(1)
109
- result = json.dumps(ann_list)
110
- with open(os.path.join(out_dir, 'pred.json'), 'w') as f:
111
- f.write(result)
 
 
 
112
 
113
- def detect(in_dir, out_dir, config, seq_name, model, device, verbose):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  #progress_log = lambda p, m: 0
116
 
117
  # create dataloader
118
  dataloader = create_dataloader_frames_only(in_dir)
119
 
120
- try:
121
- inference, image_shapes, width, height = do_detection(dataloader, model, device, verbose=verbose)
122
- except:
123
- print("Error in " + seq_name)
124
- with open(os.path.join(out_dir, "ERROR_" + seq_name + ".txt"), 'w') as f:
125
- f.write("ERROR")
126
- return
127
 
128
 
129
  outputs = do_suppression(inference, conf_thres=config['conf_threshold'], iou_thres=config['nms_iou'], verbose=verbose)
 
39
  print(config)
40
 
41
  dirname = args.frames
42
+
43
+ model, device = setup_model(args.weights)
44
 
45
  locations = [
46
  "elwha-val"
 
48
  for loc in locations:
49
 
50
  in_loc_dir = os.path.join(dirname, loc)
51
+ out_loc_dir = os.path.join(args.output, loc)
52
  print(in_loc_dir)
53
+ print(out_loc_dir)
 
 
54
 
55
  seq_list = os.listdir(in_loc_dir)
56
 
57
+ detect_location(in_loc_dir, out_loc_dir, config, model, device, seq_list, verbose)
58
 
59
 
60
+ def detect_location(in_loc_dir, out_loc_dir, config, model, device, seq_list, verbose):
 
61
 
 
62
  with tqdm(total=len(seq_list), desc="...", ncols=0) as pbar:
63
+ for seq in seq_list:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  pbar.update(1)
66
+ if (seq.startswith(".")): continue
67
+ pbar.set_description("Processing " + seq)
68
+
69
+ in_seq_dir = os.path.join(in_loc_dir, seq)
70
+ out_seq_dir = os.path.join(out_loc_dir, seq)
71
+ os.makedirs(out_seq_dir, exist_ok=True)
72
 
73
+ detect_seq(in_seq_dir, out_seq_dir, config, model, device, verbose)
74
+
75
+ def detect_seq(in_seq_dir, out_seq_dir, config, model, device, verbose):
76
+
77
+ ann_list = []
78
+ frame_list = detect(in_seq_dir, config, model, device, verbose)
79
+ for frame in frame_list:
80
+ if frame is not None:
81
+ for ann in frame:
82
+ ann_list.append({
83
+ 'image_id': ann[5],
84
+ 'category_id': 0,
85
+ 'bbox': [ann[0], ann[1], ann[2] - ann[0], ann[3] - ann[1]],
86
+ 'score': ann[4]
87
+ })
88
+ result = json.dumps(ann_list)
89
+
90
+ with open(os.path.join(out_seq_dir, 'pred.json'), 'w') as f:
91
+ f.write(result)
92
+
93
+ def detect(in_dir, config, model, device, verbose):
94
 
95
  #progress_log = lambda p, m: 0
96
 
97
  # create dataloader
98
  dataloader = create_dataloader_frames_only(in_dir)
99
 
100
+ inference, image_shapes, width, height = do_detection(dataloader, model, device, verbose=verbose)
 
 
 
 
 
 
101
 
102
 
103
  outputs = do_suppression(inference, conf_thres=config['conf_threshold'], iou_thres=config['nms_iou'], verbose=verbose)