コード例 #1
0
def processDirectory(classifier, inputFolder, outputFolder=None):
    logger = logging.getLogger("TestClassifier")
    acceptableExtensions = ('jpg', 'jpeg', 'png')
    try:
        shutil.rmtree(os.path.join(outputFolder, 'positive'))
        shutil.rmtree(os.path.join(outputFolder, 'negative'))
        os.makedirs(os.path.join(outputFolder, 'positive'))
        os.makedirs(os.path.join(outputFolder, 'negative'))
    except OSError:
        pass

    for filename in os.listdir(inputFolder):
        if filename.endswith(acceptableExtensions):
            logger.debug('Processing %s' % (filename,))
            image = Image(os.path.join(inputFolder, filename), windowSize=(30, 30), shiftSize=(3, 3))
            # _, windows = image.process()
            image.prepare()
            print filename
            s = ((np.array(image.image.shape) - np.array(image.windowSize)) // np.array(image.shiftSize)) + 1
            print s
            image.windowsAmountInfo = s
            windows = sliding_window(image = image.image, windowSize=(30, 30),shiftSize=(3, 3), flatten=True)
            l = windows.shape
            windows = windows.reshape(l[0],900)
            windows = np.insert(windows, 0, 0, axis=1 )

            result = classifier.predict(windows)
            print np.sum(result)
コード例 #2
0
def processDirectory(classifier, inputFolder, outputFolder=None):
    logger = logging.getLogger("TestClassifier")
    acceptableExtensions = ('jpg', 'jpeg', 'png')
    try:
        if os.path.exists(os.path.join(outputFolder, 'positive')):
            shutil.rmtree(os.path.join(outputFolder, 'positive'))
        if os.path.exists(os.path.join(outputFolder, 'negative')):
            shutil.rmtree(os.path.join(outputFolder, 'negative'))
    except OSError:
        pass

    try:
        os.makedirs(os.path.join(outputFolder, 'positive'))
        os.makedirs(os.path.join(outputFolder, 'negative'))
    except OSError:
        pass

    for filename in os.listdir(inputFolder):
        if filename.endswith(acceptableExtensions):
            logger.debug('Processing %s' % (filename,))
            image = Image(os.path.join(inputFolder, filename))
            _, windows = image.process()
            result = classifier.predict(windows)

            out = os.path.join(outputFolder, 'negative')
            px, py = -1, -1
            for i, (w, r) in enumerate(zip(windows, result)):
                if r:
                    xc = (i / image.windowsAmountInfo[1])
                    yc = (i % image.windowsAmountInfo[1])
                    x = xc * image.shiftSize[0]
                    y = yc * image.shiftSize[1]
                    b = image.bounds
                    drawRectangle(image.sourceImage, (x + b[0].start - image.missingRows,
                                                      y + b[1].start - image.missingColumns,
                                                      x+image.windowSize[0]-1 + b[0].start - image.missingRows,
                                                      y+image.windowSize[1]-1 + b[1].start - image.missingColumns)
                    )
                    if xc == px and yc == py + 1:
                        out = os.path.join(outputFolder, 'positive')
                    px, py = xc, yc

            imsave(os.path.join(out, filename), image.sourceImage)
コード例 #3
0
    def slice_images(self, dir_in, dir_out, h_size, v_size):
        """
        1. Read images from directory and slice they into small images
        """
        # test
        # A = np.arange(120).reshape((10, 12))
        # print A
        # slice_image_to_small_images(A, out_dir+'image_%04d.jpg')
        image_files = FileHelper.read_images_in_dir(dir_in)
        i = 0
        for filename in image_files:
            print filename.split(".")[0] + "_%04d.jpg"
            # im = imread(os.path.join(dir_in, filename))
            # im = rgb2gray(im)
            im = Image(os.path.join(dir_in, filename))
            im.prepare()

            self.__slice_image_to_small_images(
                im.image, os.path.join(dir_out, filename.split(".")[0] + "_%04d.jpg"), h_size, v_size
            )