def __init__(self, path, det='mask_rcnn', track='deepsort_mask_rcnn'): self.cam_name = os.path.split(path)[-1] print(f'\tCreating object for camera {self.cam_name}...') self.gt = w3utils.parse_aicity_rects(os.path.join( path, 'gt', 'gt.txt')) self.detections = { k[4:-4]: w3utils.parse_aicity_rects(os.path.join(path, 'det', k)) for k in os.listdir(os.path.join(path, 'det')) if '.txt' in k and any(t in k for t in dets2use) } self.trackings = { k[5:-4]: w3utils.parse_aicity_rects(os.path.join(path, 'mtsc', k)) for k in os.listdir(os.path.join(path, 'mtsc')) if '.txt' in k and any(t in k for t in tracks2use) } self.num_frames = 0 self.videopath = os.path.join(path, 'vdo.avi') self.roi = cv2.imread(os.path.join(path, 'roi.jpg')) self.FPS = 10 if 'c015' not in path else 8 self.det = det self.track = track self.last_frame = np.zeros((100, 100), dtype=np.uint8)
def gt_sizes_analysis(args): areas = [] for (sequence, cameras) in TRAINING_VIDEOS: for camera in cameras: print(f"> S{sequence:02d}-C{camera:03d}") GT_PATH = get_GT_path(sequence, camera) # dict[frame_num] = [{'bbox': [x1, y1, x2, y2]. 'conf':x, 'id':i}] det_rects = parse_aicity_rects(GT_PATH) tracklets = { } # tracklets["id"]=[[cx, cy, w, h], ... [x, y, w, h]] for frame_num in det_rects: for detection in det_rects[frame_num]: bbox = detection["bbox"] tr_id = detection["id"] if tr_id not in tracklets: tracklets[tr_id] = [] areas.append( round(bbox[2] - bbox[0]) * round(bbox[3] - bbox[1])) # width * height print(f"Minimum area: {np.min(areas)}") plt.figure() plt.title(f"Area distribution of GT detections") plt.xlabel("Area") sns.distplot(areas, bins=1000, hist=True, kde=False, norm_hist=True) plt.xlim((0, 100000)) plt.savefig(f"plot_areas.png", dpi=250)
def run(args, norm_th=25, area_th=8874): for (sequence, cameras) in VIDEOS_LIST: for camera in cameras: print(f"> S{sequence:02d}-C{camera:03d}") results_path = os.path.join(args.input, f"S{sequence:02d}", f"C{camera:03d}") for root, dirs, files in os.walk(results_path, topdown=False): for name in files: to_remove_ids = [] from_path = os.path.join(root, name) to_folder = root.replace(args.input, args.output) to_path = os.path.join(to_folder, name) #print(from_path) print(to_path) if not os.path.exists(to_folder): os.makedirs(to_folder) # dict[frame_num] = [{'bbox': [x1, y1, x2, y2]. 'conf':x, 'id':i}] det_rects = parse_aicity_rects(from_path) tracklets = { } # tracklets["id"]=[[cx, cy, w, h], ... [x, y, w, h]] for frame_num in det_rects: for detection in det_rects[frame_num]: bbox = detection["bbox"] tr_id = detection["id"] if tr_id not in tracklets: tracklets[tr_id] = [] tracklets[tr_id].append([ round((bbox[0] + bbox[2]) / 2), round((bbox[1] + bbox[3]) / 2), round(bbox[2] - bbox[0]), round(bbox[3] - bbox[1]) ]) to_remove_ids = [ tr_id for tr_id in tracklets if np.linalg.norm( np.round(np.std(tracklets[tr_id], axis=0)).astype( int)) < norm_th ] cleaned_dict = {} for frame_num in det_rects: cleaned_dict[frame_num] = [ det for det in det_rects[frame_num] if det["id"] not in to_remove_ids and (det["bbox"][2] - det["bbox"][0]) * (det["bbox"][3] - det["bbox"][1]) >= area_th ] save_aicity_rects(to_path, cleaned_dict)
def __init__(self, seq_num, cam_ids=[], det='mask_rcnn', track='deepsort_mask_rcnn'): if not cam_ids: cam_ids = list(range(1000)) # dirty ugly patch but whatever print(f'Creating object for sequence S{str(seq_num).zfill(2)}...') path = os.path.join(DATA_PATH, 'train', f'S{str(seq_num).zfill(2)}') self.cams = { p: MOTCamera(os.path.join(path, p), det=det, track=track) for p in os.listdir(path) if int(p[1:]) in cam_ids } self.num_cams = len(self.cams) self.timestamps = {} with open( os.path.join(TIMESTAMP_PATH, f'S{str(seq_num).zfill(2)}.txt'), 'r') as f: for l in f: k, v = l.split() self.timestamps[k] = float(v) self.gif_buffer = {k: [] for k in self.cams} self.gifs_to_save = {k: {} for k in self.cams} self.gif_flag = False self.dtstring = datetime.datetime.now().strftime('%Y_%m_%d-%H_%M_%S') os.makedirs(os.path.join('w6gifs', self.dtstring), exist_ok=True) try: # Add our detections print(os.path.join('mtrackings', f'S{str(seq_num).zfill(2)}')) print( os.listdir( os.path.join('mtrackings', f'S{str(seq_num).zfill(2)}'))) for cam in os.listdir( os.path.join('mtrackings', f'S{str(seq_num).zfill(2)}')): print( os.path.join('mtrackings', f'S{str(seq_num).zfill(2)}', cam)) for algorithm in os.listdir( os.path.join('mtrackings', f'S{str(seq_num).zfill(2)}', cam)): if int(cam[1:]) in cam_ids: self.cams[cam].trackings[ algorithm[:-4]] = w3utils.parse_aicity_rects( os.path.join('mtrackings', f'S{str(seq_num).zfill(2)}', cam, algorithm), zero_index=0) except FileNotFoundError: print('Error reading our detections')
def parked_cars_analysis(args): stds = [] stds_per_detector = {} for det in DETECTIONS: stds_per_detector[det] = [] for (sequence, cameras) in TRAINING_VIDEOS: for camera in cameras: print(f"> S{sequence:02d}-C{camera:03d}") results_path = os.path.join(args.input, f"S{sequence:02d}", f"C{camera:03d}") for root, dirs, files in os.walk(results_path, topdown=False): for name in files: from_path = os.path.join(root, name) to_folder = from_path.replace(root, args.output) to_path = os.path.join(to_folder, name) #print(from_path) #print(to_path) #we find the type of detection used detector = None for det in DETECTIONS: if det in from_path: detector = det if detector is None: raise Exception( "Tracking results with no detector associated.") if not os.path.exists(to_folder): os.makedirs(to_folder) # dict[frame_num] = [{'bbox': [x1, y1, x2, y2]. 'conf':x, 'id':i}] det_rects = parse_aicity_rects(from_path) tracklets = { } # tracklets["id"]=[[cx, cy, w, h], ... [x, y, w, h]] for frame_num in det_rects: for detection in det_rects[frame_num]: bbox = detection["bbox"] tr_id = detection["id"] if tr_id not in tracklets: tracklets[tr_id] = [] tracklets[tr_id].append([ round((bbox[0] + bbox[2]) / 2), round((bbox[1] + bbox[3]) / 2), round(bbox[2] - bbox[0]), round(bbox[3] - bbox[1]) ]) tracklets_info = {} stds = [] for tr_id in tracklets: tr_mean = np.round(np.mean(tracklets[tr_id], axis=0)).astype(int) tr_std = np.round(np.std(tracklets[tr_id], axis=0)).astype(int) std_norm = int(round(np.linalg.norm(tr_std))) #print(std_norm) stds.append(std_norm) stds_per_detector[detector] += stds for det, col in zip(DETECTIONS, COLORS): plt.figure() plt.title(f"Detector: {det}") plt.xlabel("STD norm of (cx, cy, w, h)") sns.distplot(stds_per_detector[det], bins=100, color=col, hist=True, kde=False, norm_hist=True) plt.xlim((0, 300)) plt.ylim(0, 0.04) plt.savefig(f"plot_{det}.png", dpi=250)
def main(args): if args.detections not in DETECTIONS: raise Exception("Detections not supported") AI_GT_RECTS_PATH = DETECTIONS[args.detections] if not os.path.exists(VIDEO_PATH): print("Video does not exist.") return filename = f"{args.detections}_{args.tracker}_{args.tracker_life}_{args.threshold}" results_path = args.output if not os.path.exists(results_path): os.makedirs(results_path) writer = imageio.get_writer(f"{filename}.mp4", fps=25) reader = imageio.get_reader(VIDEO_PATH) tr_mgr = TracksManager(tracker_type=args.tracker, tracker_life=args.tracker_life, min_iou_th=0.5) det_rects = utils.parse_aicity_rects(AI_GT_RECTS_PATH) #gt_rects_detformat = {f: [{'bbox': r, 'conf':1} for r in v] for f, v in gt_rects.items()} trackid2colors = {} results_dict = {} prvs = None feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7) lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) color = np.random.randint(0, 255, (100, 3)) gif_buffer = [] for i, frame in tqdm(enumerate(reader)): if args.min != -1 and i < args.min: continue frame_key = f"f_{i}" if prvs is None: prvs = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) p0 = cv2.goodFeaturesToTrack(prvs, mask=None, **feature_params) mask = np.zeros_like(frame) continue detector_bboxes = [] if frame_key in det_rects: detector_bboxes = [ list(np.array(det["bbox"]).astype(int)) + [ det["conf"], ] for det in det_rects[frame_key] if det["conf"] > args.threshold ] #print(detector_bboxes) #detections = tr_mgr.update(frame, detector_bboxes) frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) p1, st, err = cv2.calcOpticalFlowPyrLK(prvs, frame_gray, p0, None, **lk_params) # Select good points good_new = p1[st == 1] good_old = p0[st == 1] # dibuja las lineas for j, (new, old) in enumerate(zip(good_new, good_old)): a, b = new.ravel() c, d = old.ravel() mask = cv2.line(mask, (a, b), (c, d), color[j].tolist(), 5) frame = cv2.circle(frame, (a, b), 10, color[j].tolist(), -1) img = cv2.add(frame, mask) writer.append_data(img) gif_buffer.append(gif_preprocess(img)) prvs = frame_gray p0 = good_new.reshape(-1, 1, 2) if args.max != -1 and i >= args.max: break path = f"{filename}.gif" imageio.mimsave(path, gif_buffer) #optimize(path) print(f"DONE!") # {counter} frames processed") print(f"Saving to... {args.output}") writer.close() reader.close() print(f"Saved to '{results_path}'")
def main(args): if args.detections not in DETECTIONS: raise Exception("Detections not supported") AI_GT_RECTS_PATH = DETECTIONS[args.detections] if not os.path.exists(VIDEO_PATH): print("Video does not exist.") return filename = f"{args.detections}_{args.tracker}_{args.tracker_life}_{args.threshold}" results_path = args.output if not os.path.exists(results_path): os.makedirs(results_path) writer = imageio.get_writer(f"{filename}.mp4", fps=25) reader = imageio.get_reader(VIDEO_PATH) tr_mgr = TracksManager(tracker_type = args.tracker, tracker_life = args.tracker_life, min_iou_th = 0.5) det_rects = utils.parse_aicity_rects(AI_GT_RECTS_PATH) #gt_rects_detformat = {f: [{'bbox': r, 'conf':1} for r in v] for f, v in gt_rects.items()} trackid2colors = {} results_dict = {} prvs = None gif_buffer = [] for i, frame in tqdm(enumerate(reader)): frame_key = f"f_{i}" if prvs is None: prvs = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) hsv = np.zeros_like(frame) hsv[...,1] = 255 continue detector_bboxes = [] if frame_key in det_rects: detector_bboxes = [list(np.array(det["bbox"]).astype(int)) + [det["conf"], ] for det in det_rects[frame_key] if det["conf"] > args.threshold] #print(detector_bboxes) #detections = tr_mgr.update(frame, detector_bboxes) frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) hsv = np.zeros_like(hsv) hsv[...,1] = 255 for (x0, y0, x, y, conf) in detector_bboxes: h, w = y - y0, x - x0 h_m = int(0.1*h) w_m = int(0.1*w) x0, y0 = max(0, x0 - w_m), max(0, y0 - h_m) x, y = min(frame.shape[1], x + w_m), min(frame.shape[0], y + h_m) flow = cv2.calcOpticalFlowFarneback(prvs[y0:y, x0:x],frame[y0:y, x0:x], None, 0.5, 3, 15, 3, 5, 1.2, 0) mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1]) hsv[y0:y, x0:x, 0] = ang*180/np.pi/2 hsv[y0:y, x0:x, 2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB) writer.append_data(rgb) gif_buffer.append(gif_preprocess(rgb)) prvs = frame if args.max != -1 and i >= args.max: break path = f"{filename}.gif" imageio.mimsave(path, gif_buffer) #optimize(path) print(f"DONE!")# {counter} frames processed") print(f"Saving to... {args.output}") writer.close() reader.close() print(f"Saved to '{results_path}'")