CoolFace
Apppublic

Shingome/Image_Processing

sourceHugging Faceotherupdated 2y agoView on Hugging Face
0likes
create_dataset.py115 linesDownload Raw Back to src
1import tkinter as tk2from random import randint as rd3import numpy as np4from PIL import ImageTk, Image5 6 7class App:8    def save(fun):9        def saved(self):10            if self.dataset:11                dataset = np.asarray(self.dataset, dtype=object)12                try:13                    old_dataset = np.load('./../dataset.npy',14                                          allow_pickle=True)15                    dataset = np.vstack((dataset, old_dataset))16                    np.save('./../dataset.npy', dataset)17                except:18                    np.save('./../dataset.npy', dataset)19                    print('Создан новый файл')20                print(dataset.shape[0])21            fun(self)22 23        return saved24 25    def __init__(self):26        self.window = tk.Tk()27        self.window.resizable(False, False)28        self.window.title('dataset')29        self.window.geometry('550x320')30 31        self.dataset = []32 33        button_135 = tk.Button(self.window, text='135', width=10, height=5, command=lambda: self.save_image(4))34        button_135.place(x=450, y=10)35 36        button_45 = tk.Button(self.window, text='45', width=10, height=5, command=lambda: self.save_image(3))37        button_45.place(x=450, y=110)38 39        button_180 = tk.Button(self.window, text='180', width=10, height=5, command=lambda: self.save_image(2))40        button_180.place(x=350, y=10)41 42        button_90 = tk.Button(self.window, text='90', width=10, height=5, command=lambda: self.save_image(1))43        button_90.place(x=350, y=110)44 45        button_none = tk.Button(self.window, text='none', width=10, height=5, command=lambda: self.save_image(0))46        button_none.place(x=350, y=210)47 48        button_next = tk.Button(self.window, text='next', width=10, height=5, command=lambda: self.next_image())49        button_next.place(x=450, y=210)50 51        self.images = np.load('./../images_for_dataset.npy')52 53        self.canvas = tk.Canvas(self.window, height=300, width=300)54        self.image_prev = self.images[rd(0, np.shape(self.images)[0])]55        self.image = Image.fromarray(self.image_prev)56        self.image = self.image.resize((300, 300), resample=Image.NEAREST)57        self.photo = ImageTk.PhotoImage(self.image)58        self.image = self.canvas.create_image(0, 0, anchor='nw', image=self.photo)59        self.canvas.place(x=10, y=10)60 61        self.window.mainloop()62 63    @save64    def next_image(self):65        self.dataset = []66        self.image_prev = self.images[rd(0, np.shape(self.images)[0])]67        self.image = Image.fromarray(self.image_prev)68        self.image = self.image.resize((300, 300), resample=Image.NEAREST)69        self.photo = ImageTk.PhotoImage(self.image)70        self.image = self.canvas.create_image(0, 0, anchor='nw', image=self.photo)71        self.canvas.place(x=10, y=10)72 73    def save_image(self, answer):74        image_mat = np.asarray(self.image_prev) / 25575        image_array = np.reshape(image_mat, (1, 64))76        self.dataset.append([answer, image_array])77        self.dataset.append([answer, np.ones((1, 64)) - image_array])78 79        if answer in (3, 4):80            image_array = image_mat.T81            image_array = np.reshape(image_array, (1, 64))82            self.dataset.append([answer, image_array])83            self.dataset.append([answer, np.ones((1, 64)) - image_array])84 85            image_array = np.rot90(np.rot90(image_mat))86            image_array = np.reshape(image_array, (1, 64))87            self.dataset.append([answer, image_array])88            self.dataset.append([answer, np.ones((1, 64)) - image_array])89 90            image_array = image_mat.T91            image_array = np.reshape(image_array, (1, 64))92            self.dataset.append([answer, image_array])93            self.dataset.append([answer, np.ones((1, 64)) - image_array])94 95        else:96            image_array = np.flipud(image_mat)97            image_array = np.reshape(image_array, (1, 64))98            self.dataset.append([answer, image_array])99            self.dataset.append([answer, np.ones((1, 64)) - image_array])100 101            image_array = np.fliplr(image_mat)102            image_array = np.reshape(image_array, (1, 64))103            self.dataset.append([answer, image_array])104            self.dataset.append([answer, np.ones((1, 64)) - image_array])105 106            image_array = np.flipud(image_mat)107            image_array = np.reshape(image_array, (1, 64))108            self.dataset.append([answer, image_array])109            self.dataset.append([answer, np.ones((1, 64)) - image_array])110 111        self.next_image()112 113 114app = App()115