Esempio n. 1
0
    def visualization(self, img, prediction):
        """Plot list of predictions on barchart

        Args:
            img (np.array): image as array
            prediction (list): array of emotions probabilities

        Returns:
            list: barchart as array

        """
        config = Config()
        config.show_legend = False
        config.print_labels = True
        config.show_y_labels = False
        config.show_y_guides = False
        config.show_x_guides = False
        config.max_scale = 12
        config.width = img.width
        config.height = img.height

        custom_style = Style(background='transparent',
                             plot_background='transparent',
                             foreground='transparent',
                             colors=('#19d5ff', '#19d5ff', '#19d5ff',
                                     '#19d5ff', '#19d5ff'),
                             opacity='.8',
                             value_label_font_size=img.width // 30,
                             value__label_font_family='monospace')

        labels = ['Angry', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']

        data = list(zip(prediction, labels))
        data_dict = []
        for val, label in data:
            data_dict.append({'value': val, 'label': label})

        bar_chart = pygal.HorizontalBar(config=config, style=custom_style)
        bar_chart.add('', data_dict)
        imgByteArr = BytesIO()
        bar_chart.render_to_png(imgByteArr)  #pygal, no comments
        #bar = Image.open('./tmp.png')
        bar = Image.open(imgByteArr)
        return bar