def _core_(self, imgId): img_info = self.coco.loadImgs(imgId)[0] image_name = img_info['file_name'] print(image_name) annIds = self.coco.getAnnIds(imgIds=img_info['id'], catIds=self.catIds, iscrowd=None) anns = self.coco.loadAnns(annIds) pseudomasks = [] height = img_info['height'] width = img_info['width'] pseudomasks = np.zeros((height, width), dtype=np.float64) for ann in anns: pointobb = ann['pointobb'] pseudomask = pointobb2pseudomask(height, width, pointobb) pseudomasks += pseudomask # self.progress_bar.update() if save_np: if self.add_seg: return_flag = True pseudomask_file = os.path.join(self.save_path, image_name) stuff_things = cocoSegmentationToPng(self.coco, imgId, pseudomask_file, vis=False, return_flag=return_flag) pseudomask_seg = (stuff_things[:, :, 0] + pseudomasks) # pseudomask_seg_max = np.max(pseudomask_seg) # pseudomask_seg_min = np.min(pseudomask_seg) # pseudomask_seg = (pseudomask_seg - pseudomask_seg_min) / (pseudomask_seg_max - pseudomask_seg_min) * 255.0 pseudomask_seg = pseudomask_seg * 10.0 pseudomask_seg = pseudomask_seg.astype(np.uint8) # pseudomasks_ = show_grayscale_as_heatmap(pseudomask_seg, True, return_img=True) if return_flag: # pseudomask_file = os.path.join(self.save_path, image_name.split('.png')[0]) # np.save(pseudomask_file, pseudomasks) cv2.imwrite(pseudomask_file, pseudomask_seg) else: pseudomask_file = os.path.join(self.save_path, image_name.split('.png')[0]) np.save(pseudomask_file, pseudomasks) else: image_file = os.path.join(self.imgDir, image_name) img = cv2.imread(image_file) pseudomask_file = os.path.join(self.save_path, image_name) pseudomasks_ = show_grayscale_as_heatmap(pseudomasks, False, return_img=True) alpha = 0.6 beta = (1.0 - alpha) pseudomasks = cv2.addWeighted(pseudomasks_, alpha, img, beta, 0.0) cv2.imwrite(pseudomask_file, pseudomasks) return image_name
def __generate_pseudomask(self, imgId): img_info = self.coco.loadImgs(imgId)[0] image_name = img_info['file_name'] # if image_name != 'P2802__1.0__4914___4225.png': # return annIds = self.coco.getAnnIds(imgIds=img_info['id'], catIds=self.catIds, iscrowd=None) anns = self.coco.loadAnns(annIds) pseudomasks = [] height = img_info['height'] width = img_info['width'] area_map = np.ones((height, width)) * 1024 * 1024 pseudomasks = np.zeros((height, width), dtype=np.int32) anchor_image = self.anchor_image[self.encode] for ann in anns: pointobb = ann['pointobb'] label_mask = self.coco.annToMask(ann) == 1 area = np.sum(label_mask) transformed, mask_location = pointobb2pseudomask(pointobb, anchor_image, host_height = height, host_width = width) transformed = transformed.astype(np.int32) if self.method == 'min_area': temp_pseudomasks = np.zeros((height, width), dtype=np.int32) temp_pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]] = transformed pseudomasks[label_mask] = np.where(area < area_map[label_mask], temp_pseudomasks[label_mask], pseudomasks[label_mask]) area_map[label_mask] = np.where(area < area_map[label_mask], area, area_map[label_mask]) elif self.method == 'min_score': pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]] = np.where(transformed > pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]], transformed, pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]]) # save pseudomask pseudomask_file = os.path.join(self.save_path, image_name) pseudomasks = np.clip(pseudomasks, 0, 255) # pseudomasks = pseudomasks * 255.0 pseudomasks = pseudomasks.astype(np.uint8) cv2.imwrite(pseudomask_file, pseudomasks) if self.save_vis: image_file = os.path.join(self.imgDir, image_name) img = cv2.imread(image_file) pseudomask_vis_file = os.path.join(self.save_vis_path, image_name) pseudomasks_ = show_grayscale_as_heatmap(pseudomasks / 255.0, self.show_pseudomask, return_img=True) alpha = 0.6 beta = (1.0 - alpha) pseudomasks = cv2.addWeighted(pseudomasks_, alpha, img, beta, 0.0) cv2.imwrite(pseudomask_vis_file, pseudomasks)
264.6916809082031, 741.2076416015625, 82.70415496826172, 145.08529663085938, -0.42366891543622837 ], [ 373.6033630371094, 713.7134399414062, 81.72265625, 179.64755249023438, -0.4245613407175139 ]] for thetaobb in thetaobbs: pointobb = thetaobb2pointobb(thetaobb) pointobbs.append(pointobb) pseudomasks = np.zeros((height, width), dtype=np.int32) for pointobb in pointobbs: transformed, mask_location = pointobb2pseudomask(pointobb, anchor_image, host_height=height, host_width=width) # show_grayscale_as_heatmap(transformed) transformed = transformed.astype(np.int32) pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]] = np.where( transformed > pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]], transformed, pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]]) pseudomasks_ = show_grayscale_as_heatmap(pseudomasks / 255.0, False, return_img=True)