aboutsummaryrefslogtreecommitdiff
path: root/backend/tol_data/eol/review_imgs.py
diff options
context:
space:
mode:
Diffstat (limited to 'backend/tol_data/eol/review_imgs.py')
-rwxr-xr-xbackend/tol_data/eol/review_imgs.py33
1 files changed, 27 insertions, 6 deletions
diff --git a/backend/tol_data/eol/review_imgs.py b/backend/tol_data/eol/review_imgs.py
index 9fb462c..145f338 100755
--- a/backend/tol_data/eol/review_imgs.py
+++ b/backend/tol_data/eol/review_imgs.py
@@ -7,8 +7,13 @@ choose an image to keep, or reject all. Also provides image rotation.
Chosen images are placed in another directory, and rejected ones are deleted.
"""
-import sys, re, os, time
+import argparse
+import sys
+import re
+import os
+import time
import sqlite3
+
import tkinter as tki
from tkinter import ttk
import PIL
@@ -17,7 +22,7 @@ from PIL import ImageTk, Image, ImageOps
IMG_DIR = 'imgs_for_review'
OUT_DIR = 'imgs'
EXTRA_INFO_DB = os.path.join('..', 'data.db')
-#
+
IMG_DISPLAY_SZ = 400
MAX_IMGS_PER_ID = 3
IMG_BG_COLOR = (88, 28, 135)
@@ -28,11 +33,13 @@ class EolImgReviewer:
def __init__(self, root, imgDir, imgList, extraInfoDb, outDir):
self.root = root
root.title('EOL Image Reviewer')
+
# Setup main frame
mainFrame = ttk.Frame(root, padding='5 5 5 5')
mainFrame.grid(column=0, row=0, sticky=(tki.N, tki.W, tki.E, tki.S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
+
# Set up images-to-be-reviewed frames
self.imgs = [PLACEHOLDER_IMG] * MAX_IMGS_PER_ID # Stored as fields for use in rotation
self.photoImgs = list(map(lambda img: ImageTk.PhotoImage(img), self.imgs)) # Image objects usable by tkinter
@@ -44,9 +51,11 @@ class EolImgReviewer:
label = ttk.Label(frame, image=self.photoImgs[i])
label.grid(column=0, row=0)
self.labels.append(label)
+
# Add padding
for child in mainFrame.winfo_children():
child.grid_configure(padx=5, pady=5)
+
# Add keyboard bindings
root.bind('<q>', self.quit)
root.bind('<Key-j>', lambda evt: self.accept(0))
@@ -59,6 +68,7 @@ class EolImgReviewer:
root.bind('<Key-A>', lambda evt: self.rotate(0, True))
root.bind('<Key-S>', lambda evt: self.rotate(1, True))
root.bind('<Key-D>', lambda evt: self.rotate(2, True))
+
# Initialise fields
self.imgDir = imgDir
self.imgList = imgList
@@ -67,13 +77,15 @@ class EolImgReviewer:
self.nextEolId = 0
self.nextImgNames: list[str] = []
self.rotations: list[int] = []
+
# For displaying extra info
self.extraInfoDbCon = sqlite3.connect(extraInfoDb)
self.extraInfoDbCur = self.extraInfoDbCon.cursor()
self.numReviewed = 0
self.startTime = time.time()
- #
+
self.getNextImgs()
+
def getNextImgs(self):
""" Updates display with new images to review, or ends program """
# Gather names of next images to review
@@ -95,6 +107,7 @@ class EolImgReviewer:
self.nextImgNames.append(imgName)
self.rotations.append(0)
self.imgListIdx += 1
+
# Update displayed images
idx = 0
while idx < MAX_IMGS_PER_ID:
@@ -113,16 +126,19 @@ class EolImgReviewer:
self.photoImgs[idx] = ImageTk.PhotoImage(self.imgs[idx])
self.labels[idx].config(image=self.photoImgs[idx])
idx += 1
+
# Restart if all image files non-recognisable
if not self.nextImgNames:
self.getNextImgs()
return
+
# Update title
firstImgIdx = self.imgListIdx - len(self.nextImgNames) + 1
lastImgIdx = self.imgListIdx
title = self.getExtraInfo(self.nextEolId)
title += f' (imgs {firstImgIdx} to {lastImgIdx} out of {len(self.imgList)})'
self.root.title(title)
+
def accept(self, imgIdx):
""" React to a user selecting an image """
if imgIdx >= len(self.nextImgNames):
@@ -142,12 +158,14 @@ class EolImgReviewer:
os.remove(inFile)
self.numReviewed += 1
self.getNextImgs()
+
def reject(self):
""" React to a user rejecting all images of a set """
for i in range(len(self.nextImgNames)):
os.remove(os.path.join(self.imgDir, self.nextImgNames[i]))
self.numReviewed += 1
self.getNextImgs()
+
def rotate(self, imgIdx, anticlockwise = False):
""" Respond to a user rotating an image """
deg = -90 if not anticlockwise else 90
@@ -155,6 +173,7 @@ class EolImgReviewer:
self.photoImgs[imgIdx] = ImageTk.PhotoImage(self.imgs[imgIdx])
self.labels[imgIdx].config(image=self.photoImgs[imgIdx])
self.rotations[imgIdx] = (self.rotations[imgIdx] + deg) % 360
+
def quit(self, e = None):
print(f'Number reviewed: {self.numReviewed}')
timeElapsed = time.time() - self.startTime
@@ -163,7 +182,7 @@ class EolImgReviewer:
print(f'Avg time per review: {timeElapsed/self.numReviewed:.2f} seconds')
self.extraInfoDbCon.close()
self.root.destroy()
- #
+
def resizeImgForDisplay(self, img):
""" Returns a copy of an image, shrunk to fit in it's frame (keeps aspect ratio), and with a background """
if max(img.width, img.height) > IMG_DISPLAY_SZ:
@@ -178,6 +197,7 @@ class EolImgReviewer:
int((IMG_DISPLAY_SZ - img.width) / 2),
int((IMG_DISPLAY_SZ - img.height) / 2)))
return bgImg
+
def getExtraInfo(self, eolId: int) -> str:
""" Used to display extra EOL ID info """
query = 'SELECT names.alt_name FROM' \
@@ -193,12 +213,14 @@ def reviewImgs(imgDir: str, outDir: str, extraInfoDb: str):
print('Checking output directory')
if not os.path.exists(outDir):
os.mkdir(outDir)
+
print('Getting input image list')
imgList = os.listdir(imgDir)
imgList.sort(key=lambda s: int(s.split(' ')[0]))
if not imgList:
print('No input images found')
sys.exit(0)
+
# Create GUI and defer control
print('Starting GUI')
root = tki.Tk()
@@ -206,8 +228,7 @@ def reviewImgs(imgDir: str, outDir: str, extraInfoDb: str):
root.mainloop()
if __name__ == '__main__':
- import argparse
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.parse_args()
- #
+
reviewImgs(IMG_DIR, OUT_DIR, EXTRA_INFO_DB)