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: annotations = ImageAnnotation.objects.filter( task=self.task, image__subject=subject, rejected=False) for annotation in annotations: frames = KeyFrameAnnotation.objects.filter( image_annotation=annotation) storage_path = join(path, subject.name) create_folder(storage_path) with open(join(storage_path, str(annotation.id) + '.txt'), 'w') as f: f.write(subject.name + '\n') f.write(annotation.image.format + '\n') f.write((annotation.comments).encode( 'ascii', 'ignore').decode('ascii').replace( '\n', '<br>') + '\n') # Encoding fix # Get aspect ratio to correct x landmarks, because they are stored with isotropic spacing, while images # are often not stored in isotropic spacing metaimage = MetaImage( filename=annotation.image.format.replace('#', str(0))) spacingX = metaimage.get_spacing()[0] spacingY = metaimage.get_spacing()[1] aspect = (spacingY / spacingX) for frame in frames: # Write bounding boxes txt file landmarks = Landmark.objects.filter(image=frame) for landmark in landmarks: label = label_dict[landmark.label.id] f.write( f'{frame.frame_nr} {label} {int(round(landmark.x*aspect))} {landmark.y}\n' ) 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): 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 if it doesn't exist create_folder(path) self.add_subjects_to_path(path, form.cleaned_data['subjects']) return True, path
def export(self, form): 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 if it doesn't exist create_folder(path) try: os.stat(path) except: return False, 'Failed to create directory at ' + path # Create training folder training_path = join(path, 'training') create_folder(training_path) validation_path = join(path, 'validation') create_folder(validation_path) self.add_subjects_to_path(training_path, form.cleaned_data['subjects_training']) self.add_subjects_to_path(validation_path, form.cleaned_data['subjects_validation']) return True, path
def add_subjects_to_path(self, path, form): # Create label file label_file = open(join(path, 'labels.txt'), 'w') # Create a .txt file with the labels matching the sequence name sequence_label_file = open(join(path, 'sequence_to_label.txt'), 'w') # Find labels with parents labels = form.cleaned_data['labels'] label_dict = {} has_parent_dict = {} label_file.write('All labels involved: \n\n') for label_id in labels: label = Label.objects.get(pk=label_id) label_name = get_complete_label_name(label) label_file.write(label_name + '\n') has_parent_dict[label_name] = False label_dict[label_name] = None for start_label in has_parent_dict: for full_label in has_parent_dict: if full_label.startswith(start_label) & (start_label != full_label): has_parent_dict[full_label] = True # Assign children to the parent class label_file.write( '\nClassification based on the following parent labels: \n\n') counter = 0 for label in has_parent_dict: if has_parent_dict[label] == False: label_file.write(label + '\n') for label_name in label_dict: if label_name.startswith(label): label_dict[label_name] = counter counter += 1 nb_parent_classes = counter label_file.close() # For each subject subjects = Subject.objects.filter(dataset__task=self.task) for subject in subjects: # Get labeled images labeled_images = ImageAnnotation.objects.filter( task=self.task, rejected=False, image__subject=subject) if labeled_images.count() == 0: continue width = form.cleaned_data['width'] height = form.cleaned_data['height'] sequence_frames = [] labels = [] for labeled_image in labeled_images: label = ImageLabel.objects.get( image__image_annotation=labeled_image) if get_complete_label_name(label.label) in label_dict.keys(): # Get sequence key_frame = KeyFrameAnnotation.objects.get( image_annotation=labeled_image) image_sequence = labeled_image.image nr_of_frames = image_sequence.nr_of_frames start_frame = 0 end_frame = nr_of_frames if form.cleaned_data[ 'displayed_frames_only'] and not self.task.show_entire_sequence: start_frame = max( 0, key_frame.frame_nr - self.task.frames_before) end_frame = min( nr_of_frames, key_frame.frame_nr + self.task.frames_after + 1) for i in range(start_frame, end_frame): # Get image filename = image_sequence.format.replace('#', str(i)) if filename[-4:] == '.mhd': metaimage = MetaImage(filename=filename) image = metaimage.get_image() else: image = PIL.Image.open(filename) # Setup assigned colormode if form.cleaned_data['colormode'] != image.mode: image = image.convert( form.cleaned_data['colormode']) # Resize image = image.resize((width, height), PIL.Image.BILINEAR) # Convert to numpy array and normalize image_array = np.array(image).astype(np.float32) image_array /= 255 if len(image_array.shape) != 3: image_array = image_array[..., None] sequence_frames.append(image_array) labels.append(label_dict[get_complete_label_name( label.label)]) if form.cleaned_data['sequence_wise'] and len( sequence_frames) > 0: input = np.array(sequence_frames, dtype=np.float32) output = np.array(labels, dtype=np.uint8) sequence_label_file.write( join( subject.name, os.path.basename( os.path.dirname(image_sequence.format))) + '\t' + str(output[0]) + '\n') if form.cleaned_data['image_dim_ordering'] == 'theano': input = np.transpose(input, [0, 3, 1, 2]) if form.cleaned_data['categorical']: output = to_categorical( output, nb_classes=nb_parent_classes) subj_path = join(path, subject.name) create_folder(subj_path) try: os.stat(subj_path) except: return False, 'Failed to create directory at ' + subj_path f = h5py.File( join( subj_path, os.path.basename( os.path.dirname(image_sequence.format) + '.hd5')), 'w') f.create_dataset("data", data=input, compression="gzip", compression_opts=4, dtype='float32') f.create_dataset("label", data=output, compression="gzip", compression_opts=4, dtype='uint8') f.close() sequence_frames = [] labels = [] if not form.cleaned_data['sequence_wise'] and len( sequence_frames) > 0: input = np.array(sequence_frames, dtype=np.float32) output = np.array(labels, dtype=np.uint8) if form.cleaned_data['image_dim_ordering'] == 'theano': input = np.transpose(input, [0, 3, 1, 2]) if form.cleaned_data['categorical']: output = to_categorical(output, nb_classes=len(label_dict)) f = h5py.File(join(path, subject.name + '.hd5'), 'w') f.create_dataset("data", data=input, compression="gzip", compression_opts=4, dtype='float32') f.create_dataset("label", data=output, compression="gzip", compression_opts=4, dtype='uint8') f.close() sequence_label_file.close()