Created
November 12, 2019 12:32
-
-
Save devarshi16/bb1df243777f0c17055a74d7ff4d1b25 to your computer and use it in GitHub Desktop.
Quickly annotate cropped text images for training your OCR-model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from tkinter import * | |
from tkinter import ttk | |
import sys | |
import pandas as pd | |
from PIL import ImageTk,Image | |
base = Tk() | |
base.geometry("500x500") | |
base.resizable(width = True, height = True) | |
LARGE_FONT= ("Verdana", 12) | |
NORM_FONT= ("Verdana", 10) | |
SMALL_FONT= ("Verdana", 8) | |
def popupmsg(msg): | |
popup = Tk() | |
popup.wm_title("!") | |
label = ttk.Label(popup, text=msg, font=NORM_FONT) | |
label.pack(side="top", fill="x", pady=10) | |
B1 = ttk.Button(popup, text="Okay", command = popup.destroy) | |
B1.pack() | |
popup.mainloop() | |
def load_image(): | |
global base, img, image_names,image_dir, images_dir,image_name,cimg,box_entry | |
try: | |
image_name = image_names.pop() | |
except: | |
popupmsg('No images Left to Annotate!') | |
pass | |
box_entry.delete(0,END) | |
image_dir = os.path.join(images_dir,image_name) | |
img = ImageTk.PhotoImage(Image.open(image_dir)) | |
canvas.delete("all") | |
canvas.create_image(250,250,image=img,anchor=CENTER) | |
def save_data(): | |
global base, img, image_names, image_dir, images_dir, cimg, box_entry | |
data = box_entry.get() | |
with open(images_dir+".txt","a") as f: | |
f.write(image_name+","+data+"\n") | |
load_image() | |
def on_enter_key(event): | |
save_data() | |
def initialize_file(): | |
global image_names | |
if os.path.exists(images_dir+'.txt'): | |
# For removing duplicates in the existing csv file | |
labels_file = pd.read_csv(images_dir+'.txt') | |
labels_file.columns = ["image name","label"] | |
labels_file = labels_file.drop_duplicates(keep='first') | |
labels_file = labels_file.drop_duplicates(subset="image name",keep=False) | |
labels_file.to_csv(images_dir+'.txt',columns=["image name","label"],index=False) | |
# No need to do files which have already been done | |
col1 = labels_file["image name"].unique() | |
for imagei in image_names[:]: | |
if imagei in col1: | |
image_names.remove(imagei) | |
submit_button = Button(base,text = 'Submit', command = save_data).grid(row = 0, column = 0) | |
skip_button = Button(base,text = 'Skip',command = load_image).grid(row = 0, column = 1) | |
annotation_label = Label(base, text = 'Annotation').grid(row = 1, column = 0) | |
box_entry = Entry(base) | |
box_entry.grid(row=1,column=1) | |
if len(sys.argv)<2: | |
images_dir = 'saved' | |
else: | |
images_dir = sys.argv[1] | |
image_names = os.listdir(images_dir) | |
image_names.sort() | |
initialize_file() | |
#print(image_names) | |
canvas = Canvas(base, width = 500, height = 500) | |
try: | |
image_name = image_names.pop() | |
except: | |
print("No images left to annotate in the specified folder") | |
exit(0) | |
popupmsg("No images left to annotate in the specified folder!") | |
image_dir = os.path.join(images_dir,image_name) | |
img = ImageTk.PhotoImage(Image.open(image_dir)) | |
cimg = canvas.create_image(250,250,image = img,anchor=CENTER) | |
canvas.grid(row=2,columnspan=2) | |
box_entry.bind('<Return>',on_enter_key) | |
box_entry.focus() | |
base.mainloop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment