def __display_videos(self): for v_path, l_path in self.data.items(): cv2.destroyAllWindows() video = cv2.VideoCapture(v_path) fps = video.get(cv2.CAP_PROP_FPS) ms = int((1 / fps) * 1000) v_name = os.path.basename(v_path) lc = LabelCreator(self.l_type) frame_no = 0 cv2.namedWindow(v_name) while True: grabbed, frame = video.read() if not grabbed: break bboxes = lc.get_boxes_for_frame(l_path, frame_no) imgu.draw_text('Frame number: ' + str(frame_no), frame) if len(bboxes) > 0: bboxes = bbu.convert_center_to_2points(bboxes) imgu.draw_bbox(bboxes, frame, color=(0, 0, 255), thickness=5) old_size = frame.shape[:2] frame = imgu.resize_img(frame, self.frame_h, self.frame_w) if len(bboxes) > 0: # bboxes = bbu.convert_center_to_2points(bboxes) bboxes = bbu.resize_boxes(bboxes, old_size, (self.frame_h, self.frame_w)) imgu.draw_bbox(bboxes, frame, thickness=1) cv2.imshow(v_name, frame) k = cv2.waitKey(ms) if k == 27: break if k == ord('p'): input('Press enter to continue.') frame_no += 1
def distance(imgs, labels, new_h, new_w): x_dist = [] y_dist = [] t_dist = [] for img, label in zip(imgs, labels): bboxes = du.get_boxes(label) if bboxes is None: continue if bboxes.shape[0] < 2: continue w, h = Image.open(img).size h_new, w_new = su.compute_new_size(h, w, new_h, new_w) bboxes = bbu.resize_boxes(bboxes, (h, w), (h_new, w_new)) distances = compute_distances(bboxes) for item in distances: x_dist.append(item[0]) y_dist.append(item[1]) t_dist.append(item[2]) print('Sanity check. Items in x: %d, items in y: %d, items in t: %d' % (len(x_dist), len(y_dist), len(t_dist))) print('min x: %d, min y: %d, min total: %d' % (np.amin(x_dist), np.amin(y_dist), np.amin(t_dist))) print('max x: %d, max y: %d, max total: %d' % (np.max(x_dist), np.max(y_dist), np.max(t_dist))) print('avg x: %.2f, avg y: %.2f, avg total: %.2f' % (np.average(x_dist), np.average(y_dist), np.average(t_dist))) return x_dist, y_dist, t_dist
def get_boxes_for_frame(self, labels_path, frame_no, old_shape=None, new_shape=None): bboxes = [] if self.l_type == 'txt_okutama': data = self.read_txt(labels_path) bboxes = self.get_okutama_bbox(frame_no, data) if self.l_type == 'txt_virat': pass if self.l_type == 'xgtf_mdrone': data = self.read_xml(labels_path) bboxes = self.get_minidrone_bbox(frame_no, data) if self.l_type == 'xgtf_UFC1': data = self.read_xml(labels_path) bboxes = self.get_ufc1_bboxes(frame_no, data) if self.l_type == 'xgtf_UFC2': data = self.read_xml(labels_path) bboxes = self.get_ufc2_bboxes(frame_no, data) if (new_shape is not None) and (old_shape is not None): if bboxes: bbu.resize_boxes(bboxes, old_shape, new_shape) return bboxes
def boxes_dimensions(labels, imgs, new_h, new_w): boxes_w = [] boxes_h = [] for img, label in zip(imgs, labels): bboxes = du.get_boxes(label) if bboxes is None: continue if bboxes.shape[0] < 2: continue w, h = Image.open(img).size h_new, w_new = su.compute_new_size(h, w, new_h, new_w) bboxes = bbu.resize_boxes(bboxes, (h, w), (h_new, w_new)) for item in bboxes: boxes_w.append(item[2]) boxes_h.append(item[3]) return boxes_w, boxes_h
def __display_images(self): lc = LabelCreator(self.l_type) for data, label in self.data.items(): img = cv2.imread(data, cv2.IMREAD_COLOR) old_size = img.shape[:2] bboxes = lc.get_boxes_for_image(label, os.path.basename(data)) img = imgu.resize_img(img, self.frame_h, self.frame_w) if len(bboxes) > 0: bboxes = bbu.convert_center_to_2points(bboxes) bboxes = bbu.resize_boxes(bboxes, old_size, img.shape[:2]) imgu.draw_bbox(bboxes, img, color=[0, 0, 255], thickness=1) else: continue img = imgu.pad_img(img, self.frame_h, self.frame_w) # else: # continue cv2.imshow('Image', img) k = cv2.waitKey(1 * 500) if k == 27: break if k == ord('p'): input('Press enter to continue.')