def pull_item(self, index): while True: img_id = self.ids[index] img_path = self._imgpath % img_id target = ET.parse(self._annopath % img_id).getroot() img = Image.open(img_path) if img.mode == 'L': img = img.convert('RGB') width, height = img.size if self.target_transform is not None: target = self.target_transform(target, width, height) bbox_labels = target target = np.array(target) if target.ndim != 2: index = random.randrange(0, len(self.ids)) continue img, sample_labels = preprocess(img, bbox_labels, self.mode, img_path) sample_labels = np.array(sample_labels) if len(sample_labels) > 0: target = np.hstack( (sample_labels[:, 1:], sample_labels[:, 0][:, np.newaxis])) assert (target[:, 2] > target[:, 0]).any() assert (target[:, 3] > target[:, 1]).any() break else: index = random.randrange(0, len(self.ids)) return torch.from_numpy(img), target
def pull_item(self, index): while True: image_path = self.fnames[index] img = Image.open(image_path) if img.mode == 'L': img = img.convert('RGB') im_width, im_height = img.size boxes = self.annotransform(np.array(self.boxes[index]), im_width, im_height) label = np.array(self.labels[index]) bbox_labels = np.hstack((label[:, np.newaxis], boxes)).tolist() img, sample_labels = preprocess(img, bbox_labels, self.mode, image_path) sample_labels = np.array(sample_labels) if len(sample_labels) > 0: target = np.hstack( (sample_labels[:, 1:], sample_labels[:, 0][:, np.newaxis])) assert (target[:, 2] > target[:, 0]).any() assert (target[:, 3] > target[:, 1]).any() break else: index = random.randrange(0, self.num_samples) if self.mono_mode == True: im = 0.299 * img[0] + 0.587 * img[1] + 0.114 * img[2] return torch.from_numpy(np.expand_dims( im, axis=0)), target, im_height, im_width return torch.from_numpy(img), target, im_height, im_width
def pull_item(self, index): while True: # image_path = self.fnames[index] image_path = os.path.join(self.root, self.fnames[index]) img = Image.open(image_path) if img.mode == 'L': img = img.convert('RGB') im_width, im_height = img.size boxes = self.annotransform(np.array(self.boxes[index]), im_width, im_height) label = np.array(self.labels[index]) bbox_labels = np.hstack((label[:, np.newaxis], boxes)).tolist() img, sample_labels = preprocess(img, bbox_labels, self.mode, image_path) sample_labels = np.array(sample_labels) if len(sample_labels) > 0: face_target = np.hstack( (sample_labels[:, 1:], sample_labels[:, 0][:, np.newaxis])) assert (face_target[:, 2] > face_target[:, 0]).any() assert (face_target[:, 3] > face_target[:, 1]).any() # img = img.astype(np.float32) face_box = face_target[:, :-1] head_box = self.expand_bboxes(face_box) head_target = np.hstack( (head_box, face_target[:, -1][:, np.newaxis])) break else: index = random.randrange(0, self.num_samples) return torch.from_numpy(img), face_target, head_target
def pull_item(self, index): while True: image_path = self.fnames[index] img = np.asarray(cv2.imread(image_path, cv2.IMREAD_COLOR), dtype=np.uint8)[...,::-1] # BGR -> RGB boxes = np.array(self.boxes[index], dtype=np.int32) if self.mode=='train' and cfg.apply_distort: auged_d = spaaug(image=img, bboxes=boxes, category_id=[0]*len(boxes)) img, boxes = auged_d['image'], np.asarray(auged_d['bboxes'], dtype=np.float32) im_height, im_width = img.shape[:2] if len(boxes)>0: # if has bounding boxes boxes = np.round(np.asarray(boxes, dtype=np.float32)).astype(np.int32) boxes[:,[0,2]] = boxes[:,[0,2]].clip(0, im_width-1) boxes[:,[1,3]] = boxes[:,[1,3]].clip(0, im_height-1) boxes = boxes[(boxes[:,0]<boxes[:,2]) & (boxes[:,1]<boxes[:,3])] # delete invalid bbox boxes = boxes.astype(np.float32) if len(boxes)>0: boxes = self.annotransform(boxes, im_width, im_height) bbox_labels = np.pad(boxes, ((0,0),(1,0)), 'constant', constant_values=1.0).tolist() img, sample_labels = preprocess( img, bbox_labels, self.mode, image_path) else: sample_labels = [] sample_labels = np.array(sample_labels) if len(sample_labels) > 0: face_target = np.hstack( (sample_labels[:, 1:], sample_labels[:, 0][:, np.newaxis])) assert (face_target[:, 2] > face_target[:, 0]).all() assert (face_target[:, 3] > face_target[:, 1]).all() #img = img.astype(np.float32) face_box = face_target[:, :-1] head_box = self.expand_bboxes(face_box) head_target = np.hstack((head_box, face_target[ :, -1][:, np.newaxis])) break else: index = random.randrange(0, self.num_samples) img_torch = torch.from_numpy(img) ''' img = img[::-1] img += cfg.img_mean img = img[::-1] img = np.transpose(img, (1,2,0)).astype(np.uint8) img = Image.fromarray(img) draw = ImageDraw.Draw(img) w,h = img.size for bbox in sample_labels: bbox = (bbox[1:] * np.array([w, h, w, h])).tolist() draw.rectangle(bbox,outline='red') img.save('{:d}.jpg'.format(index)) ''' return img_torch, face_target, head_target
def pull_item(self, index): while True: image_path = self.fnames[index] img = Image.open(image_path) if img.mode == 'L': img = img.convert('RGB') boxes = self.boxes[index] # Apply aug img = np.array(img) # (h, w, c) if self.mode == 'train' and cfg.apply_distort: img, boxes = self.augmentation.ultimate_aug(img, boxes) img = Image.fromarray(img) im_width, im_height = img.size boxes = self.annotransform(np.array(boxes), im_width, im_height) label = np.array(self.labels[index][:boxes.shape[0]]) bbox_labels = np.hstack((label[:, np.newaxis], boxes)).tolist() img, sample_labels = preprocess(img, bbox_labels, self.mode, image_path) sample_labels = np.array(sample_labels) if len(sample_labels) > 0: target = np.hstack( (sample_labels[:, 1:], sample_labels[:, 0][:, np.newaxis])) assert (target[:, 2] > target[:, 0]).any() assert (target[:, 3] > target[:, 1]).any() break else: index = random.randrange(0, self.num_samples) #img = Image.fromarray(img) ''' draw = ImageDraw.Draw(img) w,h = img.size for bbox in sample_labels: bbox = (bbox[1:] * np.array([w, h, w, h])).tolist() draw.rectangle(bbox,outline='red') img.save('image.jpg') ''' return torch.from_numpy(img), target, im_height, im_width
def pull_item(self, index): while True: image_path = self.fnames[index] img = Image.open(image_path) if img.mode == 'L': img = img.convert('RGB') im_width, im_height = img.size boxes = self.annotransform( np.array(self.boxes[index]), im_width, im_height) label = np.array(self.labels[index]) bbox_labels = np.hstack((label[:, np.newaxis], boxes)).tolist() img, sample_labels = preprocess( img, bbox_labels, self.mode, image_path) sample_labels = np.array(sample_labels) if len(sample_labels) > 0: face_target = np.hstack( (sample_labels[:, 1:], sample_labels[:, 0][:, np.newaxis])) assert (face_target[:, 2] > face_target[:, 0]).any() assert (face_target[:, 3] > face_target[:, 1]).any() #img = img.astype(np.float32) face_box = face_target[:, :-1] head_box = self.expand_bboxes(face_box) head_target = np.hstack((head_box, face_target[ :, -1][:, np.newaxis])) break else: index = random.randrange(0, self.num_samples) #img = Image.fromarray(img) ''' draw = ImageDraw.Draw(img) w,h = img.size for bbox in sample_labels: bbox = (bbox[1:] * np.array([w, h, w, h])).tolist() draw.rectangle(bbox,outline='red') img.save('image.jpg') ''' return torch.from_numpy(img), face_target, head_target
def pull_item(self, index): while True: try: image_path = self.fnames[index] img = Image.open(image_path) if img.mode == 'L': img = img.convert('RGB') im_width, im_height = img.size boxes = self.annotransform( np.array(self.boxes[index]), im_width, im_height) label = np.array(self.labels[index]) bbox_labels = np.hstack((label[:, np.newaxis], boxes)).tolist() img, sample_labels = preprocess( img, bbox_labels, self.mode, image_path) sample_labels = np.array(sample_labels) if len(sample_labels) > 0: target = np.hstack( (sample_labels[:, 1:], sample_labels[:, 0][:, np.newaxis])) assert (target[:, 2] > target[:, 0]).any() assert (target[:, 3] > target[:, 1]).any() break else: index = random.randrange(0, self.num_samples) except Exception as e: print('Error in WIDERDetection:', image_path, e) index = random.randrange(0, self.num_samples) #img = Image.fromarray(img) ''' draw = ImageDraw.Draw(img) w,h = img.size for bbox in sample_labels: bbox = (bbox[1:] * np.array([w, h, w, h])).tolist() draw.rectangle(bbox,outline='red') img.save('image.jpg') ''' return torch.from_numpy(img), target, im_height, im_width