def __init__(self, train_path):
     self.train_path = train_path
     self.is_shuffled = False
     self.shuffled_vector = []
     self.image_and_label_list = []
     self.sample_count = 0
     self.annotation_post = ".png"
     self.dirProcess = DirProcess()
예제 #2
0
class ClassifySample():
    def __init__(self, train_path):
        self.train_path = train_path
        self.data_and_label_list = []
        self.sample_count = 0
        self.dirProcess = DirProcess()

    def read_sample(self, flag):
        if flag == 0:
            self.data_and_label_list = self.get_image_and_label_list(
                self.train_path)
        elif flag == 1:
            self.data_and_label_list = self.get_pointcloud_and_label_list(
                self.train_path)
        self.sample_count = self.get_sample_count()

    def get_sample_path(self, index):
        temp_index = index % self.sample_count
        img_path, label = self.data_and_label_list[temp_index]
        return img_path, label

    def get_sample_count(self):
        return len(self.data_and_label_list)

    def get_image_and_label_list(self, train_path):
        result = []
        path, _ = os.path.split(train_path)
        images_dir = os.path.join(path, "../JPEGImages")
        for line_data in self.dirProcess.getFileData(train_path):
            data_list = [x.strip() for x in line_data.split() if x.strip()]
            if len(data_list) == 2:
                image_path = os.path.join(images_dir, data_list[0])
                # print(image_path)
                if os.path.exists(image_path):
                    result.append((image_path, int(data_list[1])))
                else:
                    print("%s not exist" % image_path)
            else:
                print("% error" % line_data)
        return result

    def get_pointcloud_and_label_list(self, train_path):
        result = []
        path, _ = os.path.split(train_path)
        data_dir = os.path.join(path, "../pcds")
        for line_data in self.dirProcess.getFileData(train_path):
            data_list = [x.strip() for x in line_data.split() if x.strip()]
            if len(data_list) == 2:
                pcd_path = os.path.join(data_dir, data_list[0])
                # print(pcd_path)
                if os.path.exists(pcd_path):
                    result.append((pcd_path, int(data_list[1])))
                else:
                    print("%s not exist" % pcd_path)
            else:
                print("% error" % line_data)
        return result
 def __init__(self, input_dir, image_size=(416, 416)):
     super().__init__()
     self.image_size = image_size
     self.imageProcess = ImageProcess()
     self.dirProcess = DirProcess()
     self.dataset_process = ImageDataSetProcess()
     temp_files = self.dirProcess.getDirFiles(input_dir, "*.*")
     self.files = list(temp_files)
     self.count = len(self.files)
     self.color = (127.5, 127.5, 127.5)
    def __init__(self, train_path, class_name, is_balance=False):
        self.train_path = train_path
        self.is_blance = is_balance
        self.class_name = class_name

        self.is_shuffled = False
        self.shuffled_vectors = {}
        self.balanced_files = {}
        self.balance_file_count = {}
        self.image_and_label_list = []
        self.sample_count = 0
        self.balanced_file_index = np.zeros(len(self.class_name))

        self.annotation_post = ".xml"
        self.dirProcess = DirProcess()
class CreateSegmentionSample():
    def __init__(self):
        self.dirProcess = DirProcess()
        self.image_process = ImageProcess()
        self.annotation_post = ".png"

    def create_train_and_test(self, inputDir, outputPath, probability):
        annotationsDir = os.path.join(inputDir, "../SegmentLabel")
        saveTrainFilePath = os.path.join(outputPath, "train.txt")
        saveTestFilePath = os.path.join(outputPath, "val.txt")
        saveTrainFilePath = open(saveTrainFilePath, "w")
        saveTestFilePath = open(saveTestFilePath, "w")

        imageList = list(self.dirProcess.getDirFiles(inputDir, "*.*"))
        random.shuffle(imageList)
        for imageIndex, imagePath in enumerate(imageList):
            print(imagePath)
            image = cv2.imdecode(np.fromfile(imagePath, dtype=np.uint8),
                                 cv2.IMREAD_GRAYSCALE)
            path, file_name_and_post = os.path.split(imagePath)
            imageName, post = os.path.splitext(file_name_and_post)
            seg_label_name = imageName + self.annotation_post
            label_path = os.path.join(annotationsDir, seg_label_name)
            if (image is not None) and os.path.exists(label_path):
                if (imageIndex + 1) % probability == 0:
                    saveTestFilePath.write("%s\n" % file_name_and_post)
                else:
                    saveTrainFilePath.write("%s\n" % file_name_and_post)
        saveTrainFilePath.close()
        saveTestFilePath.close()
class ComputeImagesMean():

    def __init__(self, image_size):
        self.image_size = image_size
        self.dir_process = DirProcess()
        self.image_process = ImageProcess()
        self.dataset_process = ImageDataSetProcess()

    def compute(self, train_path):
        numpy_images = []
        path, _ = os.path.split(train_path)
        images_dir = os.path.join(path, "../JPEGImages")
        for line_data in self.dir_process.getFileData(train_path):
            data_list = [x.strip() for x in line_data.split() if x.strip()]
            if len(data_list) >= 1:
                image_path = os.path.join(images_dir, data_list[0])
                src_image, rgb_image = self.image_process.readRgbImage(image_path)
                rgb_image = self.dataset_process.image_resize(rgb_image, self.image_size)
                normaliza_image = self.dataset_process.image_normaliza(rgb_image)
                numpy_images.append(normaliza_image)
            else:
                print("read %s image path error!" % data_list)
        numpy_images = np.stack(numpy_images)
        mean = np.mean(numpy_images, axis=(0, 1, 2))
        std = np.std(numpy_images, axis=(0, 1, 2))
        return mean, std
class ConvertSegmentionLable():
    def __init__(self):
        self.save_label_dir = "SegmentLabel"
        self.annotation_post = ".png"
        self.dirProcess = DirProcess()
        self.image_process = ImageProcess()

    def convert_segment_label(self, label_dir, is_gray, class_list):
        output_dir = os.path.join(label_dir, "../%s" % self.save_label_dir)
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        label_list = list(self.dirProcess.getDirFiles(label_dir, "*.*"))
        for label_path in label_list:
            path, file_name_and_post = os.path.split(label_path)
            print(label_path)
            mask = self.process_segment_label(label_path, is_gray, class_list)
            if mask is not None:
                save_path = os.path.join(output_dir, file_name_and_post)
                cv2.imwrite(save_path, mask)

    def process_segment_label(self, label_path, is_gray, class_list):
        if is_gray:
            mask = self.image_process.read_gray_image(label_path)
        else:
            _, mask = self.image_process.readRgbImage(label_path)
        if mask is not None:
            if is_gray:
                mask = self.convert_gray_label(mask, class_list)
            else:
                mask = self.convert_color_label(mask, class_list)
        return mask

    def convert_gray_label(self, mask, class_list):
        shape = mask.shape  # shape = [height, width]
        result = np.full(shape, 250, dtype=np.uint8)
        for index, value in enumerate(class_list):
            gray_value = int(value[1].strip())
            result[mask == gray_value] = index
        return result

    def convert_color_label(self, mask, class_list):
        shape = mask.shape[:2]  # shape = [height, width]
        result = np.full(shape, 250, dtype=np.uint8)
        for index, value in enumerate(class_list):
            value_list = [int(x) for x in value[1].spilt(',') if x.strip()]
            color_value = np.array(value_list, dtype=np.uint8)
            temp1 = mask[:, :] == color_value
            temp2 = np.sum(temp1, axis=2)
            result[temp2 == 3] = index
        return result
class SegmentSample():
    def __init__(self, train_path):
        self.train_path = train_path
        self.is_shuffled = False
        self.shuffled_vector = []
        self.image_and_label_list = []
        self.sample_count = 0
        self.annotation_post = ".png"
        self.dirProcess = DirProcess()

    def read_sample(self):
        self.image_and_label_list = self.get_image_and_label_list(
            self.train_path)
        self.sample_count = self.get_sample_count()

    def get_sample_path(self, index):
        if self.is_shuffled:
            temp_index = index % self.sample_count
            temp_index = self.shuffled_vector[temp_index]
            img_path, label_path = self.image_and_label_list[temp_index]
        else:
            temp_index = index % self.sample_count
            img_path, label_path = self.image_and_label_list[temp_index]
        return img_path, label_path

    def get_sample_count(self):
        return len(self.image_and_label_list)

    def shuffle_sample(self):
        self.shuffled_vector = np.random.permutation(self.sample_count)
        self.is_shuffled = True

    def get_image_and_label_list(self, train_path):
        result = []
        path, _ = os.path.split(train_path)
        images_dir = os.path.join(path, "../JPEGImages")
        labels_dir = os.path.join(path, "../SegmentLabel")
        for filename_and_post in self.dirProcess.getFileData(train_path):
            filename, post = os.path.splitext(filename_and_post)
            label_filename = filename + self.annotation_post
            label_path = os.path.join(labels_dir, label_filename)
            image_path = os.path.join(images_dir, filename_and_post)
            #print(image_path)
            if os.path.exists(label_path) and \
                    os.path.exists(image_path):
                result.append((image_path, label_path))
            else:
                print("%s or %s not exist" % (label_path, image_path))
        return result
class ImagesLoader(DataLoader):

    def __init__(self, input_dir, image_size=(416, 416)):
        super().__init__()
        self.image_size = image_size
        self.imageProcess = ImageProcess()
        self.dirProcess = DirProcess()
        self.dataset_process = ImageDataSetProcess()
        temp_files = self.dirProcess.getDirFiles(input_dir, "*.*")
        self.files = list(temp_files)
        self.count = len(self.files)
        self.color = (127.5, 127.5, 127.5)

    def __iter__(self):
        self.index = -1
        return self

    def __next__(self):
        self.index += 1
        if self.index == self.count:
            raise StopIteration
        image_path = self.files[self.index]

        # Read image
        srcImage, rgb_image = self.imageProcess.readRgbImage(image_path)

        # Padded resize
        rgb_image, _, _ = self.dataset_process.image_resize_square(rgb_image,
                                                                   self.image_size,
                                                                   self.color)
        rgb_image = self.dataset_process.image_normaliza(rgb_image)
        numpy_image = self.dataset_process.numpy_transpose(rgb_image)
        torch_image = self.all_numpy_to_tensor(numpy_image)
        return srcImage, torch_image

    def __len__(self):
        return self.count
class DetectionSample():
    def __init__(self, train_path, class_name, is_balance=False):
        self.train_path = train_path
        self.is_blance = is_balance
        self.class_name = class_name

        self.is_shuffled = False
        self.shuffled_vectors = {}
        self.balanced_files = {}
        self.balance_file_count = {}
        self.image_and_label_list = []
        self.sample_count = 0
        self.balanced_file_index = np.zeros(len(self.class_name))

        self.annotation_post = ".xml"
        self.dirProcess = DirProcess()

    def read_sample(self):
        if self.is_blance:
            self.balanced_files, self.balance_file_count = \
                self.get_blance_file_list(self.train_path, self.class_name)
        else:
            self.image_and_label_list = self.get_image_and_label_list(
                self.train_path)
        self.sample_count = self.get_sample_count()

    def get_sample_path(self, index, class_index=None):
        if self.is_shuffled:
            if self.is_blance:
                name = self.class_name[class_index]
                files = self.balanced_files[name]
                temp_index = index % self.balance_file_count[name]
                temp_index = self.shuffled_vectors[name][temp_index]
                img_path, label_path = files[temp_index]
            else:
                temp_index = index % self.sample_count
                temp_index = self.shuffled_vectors[temp_index]
                img_path, label_path = self.image_and_label_list[temp_index]
        else:
            if self.is_blance:
                name = self.class_name[class_index]
                files = self.balanced_files[name]
                temp_index = index % self.balance_file_count[name]
                img_path, label_path = files[temp_index]
            else:
                temp_index = index % self.sample_count
                img_path, label_path = self.image_and_label_list[temp_index]
        return img_path, label_path

    def get_sample_start_index(self, index, batch_size, class_index=None):
        if self.is_blance:
            start_index = self.balanced_file_index[class_index]
            name = self.class_name[class_index]
            self.balanced_file_index[class_index] = (start_index + batch_size) % \
                                                    self.balance_file_count[name]
        else:
            start_index = index * batch_size
        return int(start_index)

    def get_sample_count(self):
        result = 0
        if self.is_blance:
            for key, value in self.balance_file_count.items():
                result += value
        else:
            result = len(self.image_and_label_list)
        return result

    def shuffle_sample(self):
        self.shuffled_vectors = {}
        if self.is_blance:
            self.balanced_file_index = np.zeros(len(self.class_name))
            for i in range(0, len(self.class_name)):
                self.shuffled_vectors[self.class_name[i]] = \
                    np.random.permutation(self.balance_file_count[self.class_name[i]])
        else:
            self.shuffled_vectors = np.random.permutation(self.sample_count)
        self.is_shuffled = True

    def get_blance_file_list(self, train_path, class_name):
        file_list = {}
        file_count = {}
        class_count = len(class_name)
        path, _ = os.path.split(train_path)
        for i in range(0, class_count):
            class_file = self.class_name[i] + ".txt"
            class_path = os.path.join(path, class_file)
            file_list[class_name[i]] = self.get_image_and_label_list(
                class_path)
            file_count[class_name[i]] = len(file_list[class_name[i]])
        return file_list, file_count

    def get_image_and_label_list(self, train_path):
        result = []
        path, _ = os.path.split(train_path)
        images_dir = os.path.join(path, "../JPEGImages")
        annotation_dir = os.path.join(path, "../Annotations")
        for filename_and_post in self.dirProcess.getFileData(train_path):
            filename, post = os.path.splitext(filename_and_post)
            annotation_filename = filename + self.annotation_post
            annotation_path = os.path.join(annotation_dir, annotation_filename)
            image_path = os.path.join(images_dir, filename_and_post)
            #print(image_path)
            if os.path.exists(annotation_path) and \
                    os.path.exists(image_path):
                result.append((image_path, annotation_path))
            else:
                print("%s or %s not exist" % (annotation_path, image_path))
        return result
예제 #11
0
 def __init__(self):
     self.dirProcess = DirProcess()
     self.xmlProcess = XMLProcess()
     self.annotation_post = ".xml"
예제 #12
0
class CreateDetectionSample():
    def __init__(self):
        self.dirProcess = DirProcess()
        self.xmlProcess = XMLProcess()
        self.annotation_post = ".xml"

    def createBalanceSample(self, inputTrainPath, outputPath):
        if not os.path.exists(outputPath):
            os.makedirs(outputPath)
        path, _ = os.path.split(inputTrainPath)
        annotationDir = os.path.join(path, "../Annotations")
        imagesDir = os.path.join(path, "../JPEGImages")
        writeFile = self.createWriteFile(outputPath)
        for fileNameAndPost in self.dirProcess.getFileData(inputTrainPath):
            fileName, post = os.path.splitext(fileNameAndPost)
            annotationFileName = fileName + self.annotation_post
            annotationPath = os.path.join(annotationDir, annotationFileName)
            imagePath = os.path.join(imagesDir, fileNameAndPost)
            print(imagePath, annotationPath)
            if os.path.exists(annotationPath) and \
               os.path.exists(imagePath):
                _, _, boxes = self.xmlProcess.parseRectData(annotationPath)
                allNames = [
                    box.name for box in boxes
                    if box.name in detect2d_config.className
                ]
                names = set(allNames)
                print(names)
                for className in names:
                    writeFile[className].write(fileNameAndPost + "\n")

    def createTrainAndTest(self, inputDir, outputPath, probability):

        annotationsDir = os.path.join(inputDir, "../Annotations")
        saveTrainFilePath = os.path.join(outputPath, "train.txt")
        saveTestFilePath = os.path.join(outputPath, "val.txt")
        saveTrainFilePath = open(saveTrainFilePath, "w")
        saveTestFilePath = open(saveTestFilePath, "w")

        imageList = list(self.dirProcess.getDirFiles(inputDir, "*.*"))
        random.shuffle(imageList)
        for imageIndex, imagePath in enumerate(imageList):
            print(imagePath)
            image = cv2.imdecode(np.fromfile(imagePath, dtype=np.uint8),
                                 cv2.IMREAD_GRAYSCALE)
            path, file_name_and_post = os.path.split(imagePath)
            imageName, post = os.path.splitext(file_name_and_post)
            xmlPath = os.path.join(annotationsDir,
                                   "%s%s" % (imageName, self.annotation_post))
            if (image is not None) and os.path.exists(xmlPath):
                if (imageIndex + 1) % probability == 0:
                    saveTestFilePath.write("%s\n" % file_name_and_post)
                else:
                    saveTrainFilePath.write("%s\n" % file_name_and_post)
        saveTrainFilePath.close()
        saveTestFilePath.close()

    def createWriteFile(self, outputPath):
        result = {}
        for className in detect2d_config.className:
            classImagePath = os.path.join(outputPath, className + ".txt")
            result[className] = open(classImagePath, "w")
        return result
예제 #13
0
 def __init__(self, train_path):
     self.train_path = train_path
     self.data_and_label_list = []
     self.sample_count = 0
     self.dirProcess = DirProcess()
 def __init__(self):
     self.save_label_dir = "SegmentLabel"
     self.annotation_post = ".png"
     self.dirProcess = DirProcess()
     self.image_process = ImageProcess()
 def __init__(self):
     self.dirProcess = DirProcess()
     self.image_process = ImageProcess()
     self.annotation_post = ".png"
 def __init__(self, image_size):
     self.image_size = image_size
     self.dir_process = DirProcess()
     self.image_process = ImageProcess()
     self.dataset_process = ImageDataSetProcess()