예제 #1
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
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 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 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
예제 #5
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