def add_subjects_to_path(self, path, data): # Create label file label_file = open(join(path, 'labels.txt'), 'w') labels = Label.objects.filter(task=self.task) label_dict = {} counter = 0 for label in labels: label_file.write(label.name + '\n') label_dict[label.name] = counter counter += 1 label_file.close() # Create file_list.txt file and copy images file_list = open(os.path.join(path, 'file_list.txt'), 'w') labeled_images = ImageAnnotation.objects.filter(task=self.task, rejected=False) for labeled_image in labeled_images: label = ImageLabel.objects.get(image=labeled_image) name = labeled_image.image.filename dataset_path = join(path, label.label.name) create_folder(dataset_path) image_id = labeled_image.image.id new_extension = 'png' new_filename = os.path.join(dataset_path, str(image_id) + '.' + new_extension) copy_image(name, new_filename) file_list.write(new_filename + ' ' + str(label_dict[label.label.name]) + '\n') file_list.close()
def add_subjects_to_path(self, path, data): # For each subject for subject in data: subject_path = join(path, subject.dataset.name, subject.name) frames = KeyFrameAnnotation.objects.filter( image_annotation__task=self.task, image_annotation__image__subject=subject) for frame in frames: # Check if image was rejected if frame.image_annotation.rejected: continue # Get image sequence image_sequence = frame.image_annotation.image # Copy image frames sequence_id = os.path.basename( os.path.dirname(image_sequence.format)) subject_subfolder = join(subject_path, str(sequence_id)) create_folder(subject_subfolder) target_name = os.path.basename(image_sequence.format).replace( '#', str(frame.frame_nr)) target_gt_name = os.path.splitext(target_name)[0] + "_gt.mhd" filename = image_sequence.format.replace( '#', str(frame.frame_nr)) new_filename = join(subject_subfolder, target_name) copy_image(filename, new_filename) # Get control points to create segmentation x_scaling = 1 if new_filename.endswith('.mhd'): image_mhd = MetaImage(filename=new_filename) image_size = image_mhd.get_size() spacing = image_mhd.get_spacing() if spacing[0] != spacing[1]: # In this case we have to compensate for a change in with real_aspect = image_size[0] * spacing[0] / ( image_size[1] * spacing[1]) current_aspect = float(image_size[0]) / image_size[1] new_width = int(image_size[0] * (real_aspect / current_aspect)) new_height = image_size[1] x_scaling = float(image_size[0]) / new_width print(image_size[0], new_width, image_size[1], new_height) image = np.asarray(image_mhd.get_pixel_data()) else: image_pil = PIL.Image.open(new_filename) image_size = image_pil.size spacing = [1, 1] image = np.asarray(image_pil) self.save_segmentation(frame, image_size, join(subject_subfolder, target_gt_name), spacing, x_scaling, image) return True, path
def add_subjects_to_path(self, path, data): # Get labels for this task and write it to labels.txt label_file = open(join(path, 'labels.txt'), 'w') labels = Label.objects.filter(task=self.task) label_dict = {} counter = 0 for label in labels: label_file.write(label.name + '\n') label_dict[label.id] = counter counter += 1 label_file.close() # For each subject for subject in data: subject_path = join(path, subject.name) create_folder(subject_path) frames = KeyFrameAnnotation.objects.filter( image_annotation__task=self.task, image_annotation__image__subject=subject, image_annotation__rejected=False) for frame in frames: image_sequence = frame.image_annotation.image # Copy image filename = image_sequence.format.replace( '#', str(frame.frame_nr)) target_name = os.path.basename(image_sequence.format).replace( '#', str(frame.frame_nr)) new_filename = join(subject_path, target_name) copy_image(filename, new_filename) # Write bounding boxes txt file boxes = BoundingBox.objects.filter(image=frame) with open(join(subject_path, str(frame.frame_nr) + '.txt'), 'w') as f: for box in boxes: center_x = round(box.x + box.width * 0.5) center_y = round(box.y + box.height * 0.5) label = label_dict[box.label.id] f.write('{} {} {} {} {}\n'.format( label, center_x, center_y, box.width, box.height)) return True, path
def add_subjects_to_path(self, path, data): # For each subject for subject in data: subject_path = join(path, subject.dataset.name, subject.name) frames = KeyFrameAnnotation.objects.filter( image_annotation__task=self.task, image_annotation__image__subject=subject) for frame in frames: # Check if image was rejected if frame.image_annotation.rejected: continue # Get image sequence image_sequence = frame.image_annotation.image # Copy image frames sequence_id = os.path.basename( os.path.dirname(image_sequence.format)) subject_subfolder = join(subject_path, str(sequence_id)) create_folder(subject_subfolder) target_name = os.path.basename(image_sequence.format).replace( '#', str(frame.frame_nr)) target_gt_name = os.path.splitext(target_name)[0] + "_gt.mhd" filename = image_sequence.format.replace( '#', str(frame.frame_nr)) new_filename = join(subject_subfolder, target_name) copy_image(filename, new_filename) # Get control points to create segmentation if new_filename.endswith('.mhd'): image_mhd = MetaImage(filename=new_filename) image_size = image_mhd.get_size() spacing = image_mhd.get_spacing() else: image_pil = PIL.Image.open(new_filename) image_size = image_pil.size spacing = [1, 1] self.save_segmentation(frame, image_size, join(subject_subfolder, target_gt_name), spacing) return True, path
def export(self, form): datasets = form.cleaned_data['dataset'] delete_existing_data = form.cleaned_data['delete_existing_data'] # Create dir, delete old if it exists path = form.cleaned_data['path'] if delete_existing_data: # Delete path try: os.stat(path) rmtree(path) except: # Folder does not exist pass # Create folder again try: os.mkdir(path) except: return False, 'Failed to create directory at ' + path else: # Check that folder exist try: os.stat(path) except: return False, 'Path does not exist: ' + path # Create label file label_file = open(os.path.join(path, 'labels.txt'), 'w') labels = Label.objects.filter(task=self.task) labelDict = {} counter = 0 for label in labels: label_file.write(label.name + '\n') labelDict[label.name] = counter counter += 1 label_file.close() # Create file_list.txt file file_list = open(os.path.join(path, 'file_list.txt'), 'w') labeled_images = ProcessedImage.objects.filter( task=self.task, image__dataset__in=datasets, rejected=False) for labeled_image in labeled_images: name = labeled_image.image.filename dataset_path = os.path.join(path, labeled_image.image.dataset.name) try: os.mkdir(dataset_path) # Make dataset path if doesn't exist except: pass image_id = labeled_image.image.id new_extension = form.cleaned_data['output_image_format'] new_filename = os.path.join(dataset_path, str(image_id) + '.' + new_extension) copy_image(name, new_filename) # Get image label label = ImageLabel.objects.get(image=labeled_image) file_list.write(new_filename + ' ' + str(labelDict[label.label.name]) + '\n') file_list.close() return True, path