+from tkinter import *\r
+from PIL import ImageTk, Image\r
+import os\r
+import sys\r
+\r
+root = Tk()\r
+root.title('Hentai Tinder')\r
+DEBUG = False\r
+\r
+# Todo:\r
+\r
+\r
+def get_img(path):\r
+ img = Image.open(path).resize((250, 250), Image.ANTIALIAS)\r
+ return ImageTk.PhotoImage(img)\r
+\r
+\r
+def print_res(res, save_path):\r
+ print("saving results...")\r
+ with open(save_path, "w") as f:\r
+ for r in res:\r
+ f.write(r + '\n')\r
+\r
+\r
+class MainWindow:\r
+\r
+ def __init__(self, main):\r
+ self.canvas = Canvas(main, width=250, height=300)\r
+ self.canvas.pack()\r
+\r
+ main.bind('<Left>', self.left_key)\r
+ main.bind('<Right>', self.right_key)\r
+ main.bind('<Up>', self.up_key)\r
+ main.bind('<Down>', self.down_key)\r
+\r
+ self.results = []\r
+ self.my_images = []\r
+ self.batch_number = 0\r
+ self.my_image_number = 0\r
+\r
+ self.save_file = "results.txt"\r
+ self.ds_dir = "thighs_dataset"\r
+ if not os.path.isdir(self.ds_dir):\r
+ sys.exit('dataset directory not found!')\r
+ self.fnames = os.listdir(self.ds_dir)\r
+ if len(self.fnames) == 0:\r
+ sys.exit('found no data in dataset directory!')\r
+\r
+ for fname in self.fnames:\r
+ self.results.append(str(fname))\r
+\r
+ self.batch_size = 10\r
+ self.load_img_batch(0)\r
+\r
+ # load save-file\r
+ if os.path.isfile(self.save_file):\r
+ print(f"found {self.save_file}, loading...")\r
+ counter = 0\r
+ self.results = []\r
+ with open(self.save_file, "r") as f:\r
+ for line in f.read().splitlines():\r
+ self.results.append(line)\r
+ if "True" in line or "False" in line:\r
+ counter += 1\r
+ if counter == len(self.results):\r
+ sys.exit(f"{self.save_file} is already completed")\r
+ self.batch_number = int(counter/self.batch_size)\r
+ self.load_img_batch(self.batch_number)\r
+ self.my_image_number = counter % self.batch_size\r
+\r
+ # set first image on canvas\r
+ self.image_on_canvas = self.canvas.create_image(0, 0, anchor=NW, image=self.my_images[self.my_image_number])\r
+\r
+ # buttons\r
+ self.button1 = Button(main, text="Smash", command=self.on_button1)\r
+ self.button1.pack(side=RIGHT, padx=20)\r
+\r
+ self.button2 = Button(main, text="Pass", command=self.on_button2)\r
+ self.button2.pack(side=LEFT, padx=20)\r
+\r
+ self.button3 = Button(main, text="Go back", command=self.on_button3)\r
+ self.button3.pack()\r
+\r
+ self.button4 = Button(main, text="Save", command=self.on_button4)\r
+ self.button4.pack()\r
+\r
+ # labels\r
+ index = self.my_image_number + (self.batch_number * self.batch_size)\r
+ self.label1 = Label(main, text=self.results[index].split('.')[0])\r
+ self.label1.place(x=80, y=260)\r
+\r
+ self.label2 = Label(main, text=f"{index+1}/{len(self.fnames)}")\r
+ self.label2.place(x=100, y=280)\r
+\r
+ def change_image(self):\r
+ self.my_image_number += 1\r
+ if DEBUG:\r
+ print(f"img: {self.my_image_number}, batch: {self.batch_number}")\r
+ print(self.results)\r
+\r
+ if self.my_image_number == len(self.my_images):\r
+ if DEBUG:\r
+ print("end of batch")\r
+ if self.batch_number == 9:\r
+ print_res(self.results, self.save_file)\r
+ print("exiting...")\r
+ root.destroy()\r
+ sys.exit(0)\r
+ else:\r
+ if DEBUG:\r
+ print("loading new batch")\r
+ self.batch_number += 1\r
+ self.load_img_batch(self.batch_number)\r
+ self.my_image_number = 0\r
+ # change image and labels\r
+ self.canvas.itemconfig(self.image_on_canvas, image=self.my_images[self.my_image_number])\r
+ index = self.my_image_number + (self.batch_number * self.batch_size)\r
+ self.label1.configure(text=self.results[index].split('.')[0])\r
+ self.label2.configure(text=f"{index+1}/{len(self.fnames)}")\r
+\r
+ def on_button1(self):\r
+ # Smash\r
+ index = self.my_image_number + (self.batch_number * self.batch_size)\r
+ self.results[index] = self.results[index].split('_')[0] + "_True"\r
+ self.change_image()\r
+\r
+ def on_button2(self):\r
+ # Pass\r
+ index = self.my_image_number + (self.batch_number * self.batch_size)\r
+ self.results[index] = self.results[index].split('_')[0] + "_False"\r
+ self.change_image()\r
+\r
+ def on_button3(self):\r
+ # Go back\r
+ if not (self.my_image_number == 0 and self.batch_number == 0): # can not go back at first image\r
+ # Revert last label\r
+ index = self.my_image_number + (self.batch_number * self.batch_size)\r
+ self.results[index-1] = self.results[index-1].split('_')[0] # remove previous label\r
+\r
+ if self.my_image_number == 0: # already in new batch\r
+ self.batch_number -= 1\r
+ self.my_image_number = 8 # will become 9 in change_image()\r
+ self.load_img_batch(self.batch_number) # load previous batch\r
+ else:\r
+ self.my_image_number -= 2 # will increase once in change_image()\r
+ self.change_image()\r
+\r
+ def on_button4(self):\r
+ # Save\r
+ print_res(self.results, self.save_file)\r
+\r
+ def load_img_batch(self, index):\r
+ if DEBUG:\r
+ print(f"load_img_batch: {index}")\r
+ start = self.batch_size * index\r
+ end = start + self.batch_size\r
+ self.my_images = []\r
+ for fname in self.fnames[start:end]:\r
+ self.my_images.append(get_img(os.path.join(self.ds_dir, fname)))\r
+\r
+ def left_key(self, _):\r
+ self.on_button2()\r
+\r
+ def right_key(self, _):\r
+ self.on_button1()\r
+\r
+ def up_key(self, _):\r
+ self.on_button3()\r
+\r
+ def down_key(self, _):\r
+ self.on_button4()\r
+\r
+\r
+MainWindow(root)\r
+root.mainloop()\r