Example #1
0
def saveChunks(inputDir, outputDir, relevanceThreshold=255):


    print "saveChunks from %s to %s " % (inputDir, outputDir)
    imageIndex = 1
    for fileName in os.listdir(inputDir):
        path = inputDir + '/' + fileName
        f, file_extension = os.path.splitext(path)

        if file_extension == '.png':
            maskImage = ImageUtils.preprocessMask(Image.open(path),210)
            chunks = ImageUtils.splitImageInChunks(maskImage, chunkWidth, chunkHeight)
            for index, c in enumerate(chunks):
                if(chunkIsRelevant(c)):
                    c.save("%s/chunk-%s-%s-%s%s" % (outputDir, imageIndex, int(index % 20), int(index / 20), file_extension))
            imageIndex+=1
            continue

        if not isfile(path) or file_extension != '.jpg':
            continue


        print "- opening "+path

        srcImage = ImageUtils.preprocessImage(Image.open(path), 240)

        if file_extension == '.jpg':
            chunks = ImageUtils.splitImageInChunks(srcImage, chunkWidth, chunkHeight)

        for index, c in enumerate(chunks):
            savePath = "%s/chunk-%s-%s-%s%s" % (outputDir, imageIndex, int(index % 20), int(index / 20), file_extension)
            if(chunkIsRelevant(c, relevanceThreshold)):
                c.save(savePath, quality=95)

    print "savechunks - finished"
Example #2
0
def getImageAndMaskDifferenceChunks(imgPath):
    srcImage, maskImage = getImageAndMask(imgPath)

    srcImage = ImageUtils.preprocessImage(srcImage,240)
    maskImage = ImageUtils.preprocessMask(maskImage, 210)

    imageChunks = ImageUtils.splitImageInChunks(srcImage, chunkWidth, chunkHeight)
    maskChunks = ImageUtils.splitImageInChunks(maskImage, chunkWidth, chunkHeight)

    return ImageUtils.getDifferenceChunks(imageChunks, maskChunks)
Example #3
0
def activateOnImage(fnn, layerpath, saveWrongChunks=False, breakOnError=True, saveWrongImages=False):
    try:
        chunks = getImageAndMaskDifferenceChunks(layerpath)
    except IOError as e:
        print e
        return
    except ValueError as e:
        print e
        return

    fileName = os.path.basename(layerpath)

    index=1
    needToSaveImage = False
    for c in chunks:
        cFlattened = np.asarray(c).flatten()
        estimate = fnn.activate(cFlattened)
        if estimate[0] < 0.7:
            needToSaveImage = True
            print "estimated error on %s. probability OK / KO: %s/%s" % (fileName, estimate[0], estimate[1])
            if saveWrongChunks:
                c.save(outputPath+"/chunk-%s.jpg" %(index))

            if breakOnError:
                break
            index+=1

    if needToSaveImage and saveWrongImages:
        srcImage, maskImage = getImageAndMask(layerpath)
        srcImage.save(outputPath+"/image-%s-original-image.jpg" %(index))
        maskImage.save(outputPath+"/image-%s-original-mask.png" %(index))

        srcImage = ImageUtils.preprocessImage(srcImage,240)
        maskImage = ImageUtils.preprocessMask(maskImage, 210)
        srcImage.save(outputPath+"/image-%s-elab-image.jpg" %(index))
        maskImage.save(outputPath+"/image-%s-elab-mask.png" %(index))

    return