예제 #1
0
    def __init__(self, path, logger=""):
        # define main path
        self.path = path

        # folder environment
        self.avgpath = ""
        self.subfolders = {}

        # theres an uncatched error...
        try:
            self.makeavgdir()
        except FileNotFoundError as e:
            print("Folder not found")
        except:
            print("WTF")

        # create log file in the avg folder
        if logger == "":
            # create a temporary log that only prints to console
            self.mylog = Logger("Avgrgb")
        elif isinstance(logger, Logger):
            # inheriths the logger from somewhere else
            self.mylog = logger
        elif isfile(logger):
            self.mylog = Logger("Avgrgb", logger)
        elif logger == "auto file":
            self.mylog = Logger("Avgrgb",
                                pathfile=join(self.avgpath +
                                              "myavglogger.txt"))

        self.imgs = []
        self.avg = MyRGBImg()
        self.algs = []
        self.algimgs = []
예제 #2
0
def run_clean_up(folder):
    debug_mode = True

    mylog = Logger("Clean up",
                   folder + "main_logfile.txt",
                   debug_mode=debug_mode)

    mylog.log("clean up for folder: ")
    mylog.log(folder)

    avg = AvgFolderMem(folder)

    subfolders = ["processed_images", "aligned_images", "correlation_images"]

    for subfolder in subfolders:
        mylog.log("Cleaned: " + subfolder)
        shutil.rmtree(avg.subfolders[subfolder])
예제 #3
0
    def __init__(self, path, logger=""):
        # define main path
        self.path = path

        # folder environment
        self.avgpath = ""
        self.subfolders = {}

        # creates the necessary subfolders
        try:
            self.makeavgdir()
        except FileNotFoundError as e:
            print("Folder not found")
        except:
            print("WTF")

        # create log file in the avg folder
        if logger == "":
            # create a temporary log that only prints to console
            self.mylog = Logger("Avgrgb")
        elif isinstance(logger, Logger):
            # inheriths the logger from somewhere else
            self.mylog = logger
        elif isfile(logger):
            self.mylog = Logger("Avgrgb", logger)
        elif logger == "auto file":
            self.mylog = Logger("Avgrgb",
                                pathfile=join(self.avgpath +
                                              "myavglogger.txt"))

        # instead of loading all the pictures in one array
        # create a path array that reads the pictures at will

        self.imgs_names = []
        self.avg = MyRGBImg()
        self.algs = []
예제 #4
0
def run_average_rgb(folder):
    debug_mode = True

    datasetpath = folder

    memsave = True  # True | False

    mean_mode = "Mean"  # Mean | Median | Mode

    algimages = True  # True | False

    usealgimages = True  # True | False

    mylog = Logger("Average RGB dataset",
                   datasetpath + "main_logfile.txt",
                   debug_mode=debug_mode)
    mylog.log("Debug mode: " + "ON" if debug_mode == True else "OFF")
    mylog.log("For the folder:")
    mylog.log(datasetpath)
    mylog.log("Averaging type: rgb")
    mylog.log("Memory saving mode: " + str(memsave))

    if memsave:
        avg = AvgRGBMemSave(datasetpath)
    else:
        avg = AvgRGB(datasetpath)

    avg.gather_pictures_names()

    # build the informatiosn
    mylog.log("number of pictures:" + str(len(avg.imgs_names)))
    image = avg.get_image(0)
    mylog.log("Size of images: " + str(image.data.shape[0]) + "x" +
              str(image.data.shape[1]))

    mylog.log("Loaded names")

    if algimages:
        avg.load_algs()
        mylog.log("Loaded aligments: " + str(len(avg.algs)))

    mylog.log("--- Start preporcessing ---", True)

    if algimages:
        avg.align_images(debug=debug_mode)
        mylog.log("Aligned Images", True)

    avg.average(mode=mean_mode, aligned=usealgimages, debug=debug_mode)

    mylog.log("Averaged Images", True)

    avg.save_avg()

    mylog.log("- End process -")
예제 #5
0
def run_create_rgb_dataset(folder):

    debug_mode = True

    # set parameters
    testdatasetpath = folder
    mypicname = "./data/volpe-2.png"

    n_pictures = 25
    min_a = -10
    max_a = 10
    min_xs = -25
    max_xs = 25
    min_ys = -25
    max_ys = 25
    flip_angle = True

    mylog = Logger("Create RGB dataset",
                   testdatasetpath + "main_logfile.txt",
                   debug_mode=debug_mode)

    mylog.log("Creating dataset in:\n" + testdatasetpath)
    mylog.log("Using the picture: " + mypicname)

    mylog.log("Creating dataset with {0} pictures".format(n_pictures))
    mylog.log("With rotations from {0} to {1} degree".format(min_a, max_a))
    mylog.log("With shift in x: from {0} to {1} and y: from {2} to {3}".format(
        min_xs, max_xs, min_ys, max_ys))
    mylog.log(
        "The dataset will be generated by {0} randomly flipping rotations and translations"
        .format("" if flip_angle == True else "not"))

    if not isdir(testdatasetpath):
        mkdir(testdatasetpath)
        mylog.log("Created test dataset path")

    # create a test dataset:
    mypic = MyRGBImg(mypicname)
    mypic.binning(2)

    mylog.log("Processing done")

    template_folder = join(testdatasetpath, "template_folder")
    if not isdir(template_folder):
        mkdir(template_folder)

    mypic.save(join(template_folder, "template.png"))
    if debug_mode:
        mypic.show_image()
        plt.show()

    mylog.log(
        "------------------------------\nCreating dataset\n------------------------------"
    )

    np.random.seed(10)

    logpathdir = join(testdatasetpath, "tlog")
    if not isdir(logpathdir):
        mkdir(logpathdir)

    with open(join(logpathdir, "mytransformations.log"), 'w') as f:
        angles = np.random.uniform(min_a, max_a, n_pictures)
        for i in range(n_pictures):
            image = deepcopy(mypic)
            if flip_angle:
                anglefirst = False if np.random.randint(0, 2) == 0 else True
            else:
                anglefirst = True

            angle = angles[i]
            dx = np.random.randint(min_xs, max_xs)
            dy = np.random.randint(min_ys, max_ys)

            f.write("{0} {1} {2} {3}\n".format(anglefirst, dx, dy, angle))
            mylog.log(
                "Pictrue with: rot first {0}, angle: {1:.2f}, shift x: {2}, y: {3} created"
                .format(anglefirst, angle, dx, dy))

            if anglefirst:
                image.rotate(angle)
                image.move(dx, dy)
            else:
                image.move(dx, dy)
                image.rotate(angle)

            if debug_mode:
                image.show_image()
                plt.show()

            image.save(join(testdatasetpath, "pic_" + str(i) + ".png"))
예제 #6
0
def run_average_gray(folder):
    # options
    debug_mode = True

    # chose path to image sequence folder
    datasetpath = folder

    memsave = True  # True | False

    preprocop = [("convert to grayscale", ), ("square it", ), ("binning", 0),
                 ("transpose", ), ("normalize", )]

    custom_template = False  # True | False
    template_image_path = folder + "template_folder/template.png"  # path to image

    auto_template_type = "UseFirstImage"  # "Use First Image" | "Average"
    save_template = True  # True | False

    align_images = True  # True | False
    align_mode = "fixed"  # "fixed | tree" // fixed still fastest option
    align_space = (-1, 1, 0.1)  # (min angle, max angle, precision)

    # logger
    mylog = Logger("Averaging Gray",
                   datasetpath + "main_logfile.txt",
                   debug_mode=debug_mode)

    mylog.log("Debug mode: " + "ON" if debug_mode == True else "OFF")
    mylog.log("For the folder:")
    mylog.log(datasetpath)
    mylog.log("Averaging type: grey")
    mylog.log("Memory saving mode: " + str(memsave))

    mylog.log(
        "------------------------------\nLoading dataset\n------------------------------"
    )
    if memsave:
        avg = AvgFolderMem(datasetpath)
    else:
        avg = AvgFolder(datasetpath)

    avg.gather_pictures()
    # build the informatiosn
    mylog.log("number of pictures:" + str(avg.init_imgs.n))
    image = avg.init_imgs.get_image(0)
    mylog.log("Size of images: " + str(image.data.shape[0]) + "x" +
              str(image.data.shape[1]))

    mylog.log("--- Start preporcessing ---", True)

    nametofunc = {}
    nametofunc[preprocop[0]] = lambda: avg.c2gscale()
    nametofunc[preprocop[1]] = lambda: avg.squareit()
    nametofunc[preprocop[2]] = lambda n: avg.binning(n)
    nametofunc[preprocop[3]] = lambda: avg.transpose()
    nametofunc[preprocop[4]] = lambda: avg.normalize()

    for name in preprocop:
        if len(name) == 1:
            nametofunc[name]()
            mylog.log("Process: " + name[0])
        if len(name) == 2:
            nametofunc[name](name[1])
            mylog.log("Process: " + name[0] + "Arguments: " + str(name[1]))
    mylog.log("Processing took: ", True)

    mylog.log(
        "------------------------------\nGenerating template\n------------------------------"
    )

    if custom_template:
        custom_t = MyImage(template_image_path)
        custom_t.convert2grayscale()
        mylog.log("Template loaded from: " + template_image_path)
        mylog.log("Template image is: {0}x{1}".format(custom_t.get_sizex(),
                                                      custom_t.get_sizey()))
        avg.generate_template(custom_t)
    else:
        avg.generate_template(auto_template_type)
        mylog.log("Template generated: " + auto_template_type)

    mylog.log("Template generated", True)

    if save_template:
        avg.save_template()

    if debug_mode:
        avg.template.show_image()
        plt.show()
        avg.template.inspect()

    if align_images:
        mylog.log(
            "------------------------------\nAlignment\n------------------------------"
        )
        mylog.log("Choosen Aligment: " + align_mode)
        alignnames = ["min angle: ", " |max angle: ", " |precision: "]
        mylog.log("".join(a + str(d) for a, d in zip(alignnames, align_space)))
        avg.align_images(align_mode, align_space, debug_mode)
        avg.save_shifts()

        mylog.log("Alignment done", True)
        if avg.anglestree != None:
            mylog.log("Numnber of template generated: " +
                      str(len(avg.anglestree.angles_nodes)))
            mylog.log("Normally would be: " + str(
                len(np.arange(align_space[0], align_space[1], align_space[2])))
                      )
        else:
            mylog.log("Numnber of template generated: " +
                      str(avg.templaterotsft.n))
        mylog.log("Shifts saved")

        mylog.log(
            "------------------------------\nAverage\n------------------------------"
        )
        avg.average(debug=debug_mode)
        avg.save_avg()
        mylog.log("Average Complete", True)

        if debug_mode:
            avg.avg.show_image()
            plt.show()
            avg.avg.inspect()

    mylog.log("End procedure", True)
예제 #7
0
        s = ""
        if self.content_type == "text":
            s += self.content.text
        elif self.content_type in ["sticker", "video","document", "photo"] :
            s += self.content.info()
        else:
            s += "message content not supported"
        return s

    def __str__(self):
        return "mID: {0}|Chat: {1}|date: {2}".format(self.message_id, self.chat, self.date)


if __name__ == "__main__":
    
    lg = Logger("Message Parser", "message_parser_log.txt")
    
    msg = me.sticker_message
    
    print(" ORIGINAL MESSAGE ")
    
    print(msg)
    
    print("\n MY MESSAGE ")

    lg.startTimer()
    for i in range(100000):
        qmsg = Message(msg, opt=False)
    lg.log("store query, processing took: ", True)

    lg.startTimer()
예제 #8
0
class AvgRGBMemSave(object):
    ''' class to manage the colored average'''

    # ------ initialize method ------
    def __init__(self, path, logger=""):
        # define main path
        self.path = path

        # folder environment
        self.avgpath = ""
        self.subfolders = {}

        # creates the necessary subfolders
        try:
            self.makeavgdir()
        except FileNotFoundError as e:
            print("Folder not found")
        except:
            print("WTF")

        # create log file in the avg folder
        if logger == "":
            # create a temporary log that only prints to console
            self.mylog = Logger("Avgrgb")
        elif isinstance(logger, Logger):
            # inheriths the logger from somewhere else
            self.mylog = logger
        elif isfile(logger):
            self.mylog = Logger("Avgrgb", logger)
        elif logger == "auto file":
            self.mylog = Logger("Avgrgb",
                                pathfile=join(self.avgpath +
                                              "myavglogger.txt"))

        # instead of loading all the pictures in one array
        # create a path array that reads the pictures at will

        self.imgs_names = []
        self.avg = MyRGBImg()
        self.algs = []

        # TODO:
        # transform the getter setters into iterators

    def get_image(self, index):
        ''' returns a MyRGBImg given the index, loads the original images'''
        # read the image corresponding to the path
        pathtopic = join(self.path, self.imgs_names[index])
        myimg = MyRGBImg()
        myimg.read_from_file(pathtopic)
        return myimg

    def get_alg_image(self, index):
        ''' returns a MyRGBImg given the index, loads the aligned images'''
        filename, ext = splitext(self.imgs_names[index])
        pathtopic = join(self.subfolders["aligned_rgb_images"],
                         ("alg_" + filename + ".png"))
        myimg = MyRGBImg()
        myimg.read_from_file(pathtopic)
        return myimg

    def save_alg_image(self, index, algimg):
        ''' Saves a MyRGBImg given the index, into the alg folder'''
        filename, ext = splitext(self.imgs_names[index])
        # do I have to normalize?
        if not np.all(algimg.data <= 1) or not np.all(algimg.data >= 0):
            algimg.limit(1.0)
        algimg.save(
            join(self.subfolders["aligned_rgb_images"],
                 ("alg_" + filename + ".png")))

    def gather_pictures_names(self):
        '''Checks the folder self.path for images and constructs the array
        self.imgs_names which is used to gather the pictures
        '''
        p = self.path
        # gather all the files
        filenames = [f for f in listdir(p) if isfile(join(p, f))]
        for filename in filenames:
            path, name, ext = get_pathname(filename)
            # select only the pictures
            if ext in ['.png', '.jpg']:
                self.imgs_names.append(filename)
        self.mylog.log("Loaded {0} images.".format(len(self.imgs_names)))

    def average(self, mode="Mode", aligned=True, debug=False, transition=True):
        if mode == "Mean":
            self.average_mean(aligned, debug, transition)
        if mode == "Median":
            self.average_median(aligned, debug, transition)
        if mode == "Mode":
            self.average_mode(aligned, debug, transition)
        if mode == "Sum":
            self.average_sum(aligned, debug, transition)

    def average_mode(self, aligned=True, debug=False, transition=True):
        ''' Calculates the mode of the image array. The mode should represent
        the most frequent pixel value in an image array.
        For size reasons the array is split in quadrants.
        '''

        # define the function to get the images according to the alignment
        sizedataset = len(self.imgs_names)
        if aligned:
            get_img = lambda i: self.get_alg_image(i)  #self.get_alg_image(0)
        else:
            get_img = lambda i: self.get_image(i)  #self.get_image(0)

        # get the image size
        dimx = get_img(0).data.shape[0]
        dimy = get_img(0).data.shape[1]

        # get image half size
        dimx2 = int(dimx / 2)
        dimy2 = int(dimy / 2)

        # quadrants coordinates as array indices
        quadrant = [(0, dimx2, 0, dimy2), (dimx2, dimx, 0, dimy2),
                    (0, dimx2, dimy2, dimy), (dimx2, dimx, dimy2, dimy)]

        # decide how deep should be the the measured frequency
        # 128 = 8 mil colors
        # True color 24 bbp = 256 = 16'777'260 colors
        depth = 128
        darray = np.arange(depth)

        resq = []
        for q in quadrant:

            # calculate for each image, inside a quadrant the mode
            # x, y, c, freq
            stack = np.zeros((dimx2, dimy2, 3, depth), dtype=np.uint32)
            for i in range(sizedataset):
                pic = get_img(i)
                # for each pixel of the image i
                for x, ix in zip(range(q[0], q[1]), range(0, dimx2)):
                    for y, iy in zip(range(q[2], q[3]), range(0, dimy2)):
                        for c in range(3):
                            # test in which bin the pixel goes
                            # len(darray) = depth
                            # the sistem could work with a dictionary too
                            # stack[ix, iy, ic][d] += value
                            # key error -> add the vaule
                            # it should spare memory and computation
                            for d in range(len(darray) - 1):
                                if pic.data[x, y, c] < 0 or pic.data[x, y,
                                                                     c] > 1:
                                    raise ValueError("image not normalized")
                                pv = pic.data[x, y, c] * depth
                                if pv >= darray[d] and pv < darray[d + 1]:
                                    stack[ix, iy, c, d] += 1

            # construct the resulting quadrant and store it in the resq list
            resquadrant = np.zeros((dimx2, dimx2, 3))
            for x in range(0, dimx2):
                for y in range(0, dimy2):
                    for c in range(0, 3):
                        # for each pixel of quadrant calculate which pixel is
                        # the most frequent
                        maxfreq = 0
                        maxvalue = 0
                        for i in range(depth):
                            if stack[x, y, c, i] > maxfreq:
                                maxfreq = stack[x, y, c, i]

                                maxvalue = darray[i] / float(depth)
                        resquadrant[x, y, c] = maxvalue
            # this are the averaged quadrants
            calcquadrant = MyRGBImg(resquadrant)
            resq.append(calcquadrant)

        # recompose the picture
        picdata = MyRGBImg(np.zeros((dimx, dimy, 3)))
        for i, q in enumerate(quadrant):
            for x, ix in zip(range(q[0], q[1]),
                             range(quadrant[0][0], quadrant[0][1])):
                for y, iy in zip(range(q[2], q[3]),
                                 range(quadrant[0][2], quadrant[0][3])):
                    for c in range(3):
                        picdata.data[x, y, c] = resq[i].data[ix, iy, c]
        self.avg = picdata

        if debug:
            picdata.show_image()
            plt.show()

    def average_median(self, aligned=True, debug=False, transition=True):
        ''' Calculats the mean of a serie of pictures aligned takes the 
        pictures from the the rgb aligned folder, while if False takes the 
        original images
        '''
        self.mylog.log("started the median procedure")

        # Chose which serie to average
        sizedataset = len(self.imgs_names)
        if aligned:
            get_img = lambda i: self.get_alg_image(i)  #self.get_alg_image(0)
        else:
            get_img = lambda i: self.get_image(i)  #self.get_image(0)

        # get image sizes
        dimx = get_img(0).data.shape[0]
        dimy = get_img(0).data.shape[1]
        dimx2 = int(dimx / 2)
        dimy2 = int(dimy / 2)

        # get the quadrant coordinates
        quadrant = [(0, dimx2, 0, dimy2), (dimx2, dimx, 0, dimy2),
                    (0, dimx2, dimy2, dimy), (dimx2, dimx, dimy2, dimy)]

        # construct the median
        resq = []
        for q in quadrant:
            # for each quadrant
            stack = np.zeros((dimx2, dimy2, 3, sizedataset))
            for i in range(sizedataset):
                self.mylog.log("quadrant {0} image {1}".format(q, i))
                pic = get_img(i)
                pic.data = pic.data[q[0]:q[1], q[2]:q[3], :]
                stack[:, :, :, i] = pic.data
            # calculate median
            med = np.median(stack, axis=3)
            medpic = MyRGBImg(med)
            medpic.show_image()
            plt.show()
            resq.append(medpic)

        # recompose the picture
        picdata = MyRGBImg(np.zeros((dimx, dimy, 3)))
        for i, q in enumerate(quadrant):
            for x, ix in zip(range(q[0], q[1]),
                             range(quadrant[0][0], quadrant[0][1])):
                for y, iy in zip(range(q[2], q[3]),
                                 range(quadrant[0][2], quadrant[0][3])):
                    for c in range(3):
                        picdata.data[x, y, c] = resq[i].data[ix, iy, c]

        # show resulting image
        if debug:
            picdata.show_image()
            plt.show()

        self.avg = picdata

    def average_mean(self, aligned=True, debug=False, transition=True):
        ''' performs the mean of the images, aligned is True will use the
        aligned pictures while if false will use the original picture, 
        for the transition, each averaging step is printed out
        '''

        self.mylog.log("started the mean averaging procedure")

        sizedataset = len(self.imgs_names)

        if aligned:
            picture = self.get_alg_image(0)
        else:
            picture = self.get_image(0)

        # initialize sum variable
        s = MyRGBImg(np.zeros(picture.data.shape))
        #s = color.rgb2lab(s.data)

        for i in range(sizedataset):
            if debug:
                self.mylog.log("Averaging image: " + str(i))
            #load the picture
            if aligned:
                picture = self.get_alg_image(i)
            else:
                picture = self.get_image(i)
            # convert both to lab
            #im = color.rgb2lab(picture.data)
            im = picture.data

            #perform operations
            s += im

            # if the transition is true show what happens to each picture
            if transition:
                tr = s / float(i + 1)
                #avg = MyRGBImg(color.lab2rgb(tr))
                avg = tr
                avg.save(
                    join(self.subfolders["avg_transition"],
                         "avg_tr_" + str(i) + ".png"))

        # calculate the average
        s = s / float(sizedataset)
        #self.avg = MyRGBImg(color.lab2rgb(s))
        self.avg = s

        # small trick to align the image in the correct sense if they are
        # squared
        if self.avg.data.shape[0] == self.avg.data.shape[1]:
            self.avg.rotate(90)
            self.avg.flip_V()

    def average_sum(self, aligned=True, debug=False, transition=True):
        if aligned:
            get_img = lambda i: self.get_alg_image(i)  #self.get_alg_image(0)
        else:
            get_img = lambda i: self.get_image(i)  #self.get_image(0)

        sizedataset = len(self.imgs_names)
        s = MyRGBImg(np.zeros(get_img(0).data.shape))
        for i in range(sizedataset):
            if debug:
                self.mylog.log("Averaging image: " + str(i))
            #load the picture
            picture = get_img(i)

            #perform operations
            s += picture

        # calculate the average
        self.avg = s
        self.avg.limit(1)

    def load_algs(self):
        ''' This function loads the alignments calculated by the avg class'''
        with open(join(self.subfolders["results"], "shifts_log.txt")) as f:
            lines = f.readlines()

        self.algs = []
        for line in lines:
            sdata = line.split(' | ')
            sdata = [d.strip() for d in sdata]
            data = [int(sdata[0]), int(sdata[1]), float(sdata[2])]
            self.algs.append(data)

    def align_images(self, debug=False):
        ''' this function, given the shifts calculated by avg folder class and
        loaded with the self.load_algs method, aligns the original images
        '''
        self.mylog.log("Align procedure started")

        self.algimgs = []
        for i in range(len(self.imgs_names)):
            if debug:
                self.mylog.log("Aligning image:" + str(i))

            # load picture to align
            algimage = self.get_image(i)
            algimage.inspect()

            if algimage.data.shape[0] == algimage.data.shape[1]:
                # operations needed to align the rgb images
                algimage.rotate(90)
                algimage.flip_V()
                algimage.rotate(self.algs[i][2])
                algimage.move(-self.algs[i][0], -self.algs[i][1])
            else:
                # still doesnt work...
                algimage.squareit()
                algimage.rotate(self.algs[i][2])
                algimage.move(-self.algs[i][0], -self.algs[i][1])

            print("- After -")
            algimage.inspect()
            # save the image
            self.save_alg_image(i, algimage)

    def save_avg(self, name="avg_rgb.png"):
        ''' saves the average '''
        self.avg.save(join(self.subfolders["results"], name))

    def makeavgdir(self):
        ''' create a folder average into the dataset path
         avg
          |- processed_images
          |- aligned_images
          |- correlation_images
          |- results
          |- aligned_rgb_images
          |- avg_transition
        '''
        self.avgpath = join(self.path, "avg")
        if not isdir(self.avgpath):
            mkdir(self.avgpath)

        subfolders = ["aligned_rgb_images", "results", "avg_transition"]
        for folder in subfolders:
            self.subfolders[folder] = join(self.avgpath, folder)
            if not isdir(self.subfolders[folder]):
                mkdir(self.subfolders[folder])

    def transpose(self):
        ''' transpose all images '''
        for i, img in enumerate(self.imgs):
            img.transpose()
            self.imgs.set_image(i, img)
        self.mylog.log("images transposed")

    def normalize(self):
        ''' normalize all images '''
        for i, img in enumerate(self.imgs):
            img.normalize()
            self.imgs.set_image(i, img)
        self.mylog.log("images normalized")

    def binning(self, n=1):
        ''' bins all images '''
        for i, img in enumerate(self.imgs):
            img.binning(n)
            self.imgs.set_image(i, img)
        self.mylog.log("images binned")