Skip to content

Commit 13c0a2c

Browse files
authored
Merge pull request devarshi16#3 from devarshi16/dev
Create Rectangle Feature
2 parents a1ac0eb + 5676e0a commit 13c0a2c

File tree

7 files changed

+406
-47
lines changed

7 files changed

+406
-47
lines changed

.draw_rect.py.swp

12 KB
Binary file not shown.

config.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#WINDOW DIMENSIONS
12
INIT_WIDTH = 1366
23
INIT_HEIGHT = 768
34
TOP_FRAME_HEIGHT = 20
45
BOTTOM_FRAME_HEIGHT = INIT_HEIGHT - TOP_FRAME_HEIGHT
56
BUTTON_WIDTH = 20
6-
RADIUS = 5
7+
8+
#TOOL MODIFIABLES
79
SUPPORTED_FORMATS = ["jpg","png","jpeg","JPEG","tiff"]
810
TYPE_CHOICES = ["None",
911
"Printed Key", "Written Key",
@@ -17,6 +19,25 @@
1719
LOGGING = True
1820
DEBUGGING = True
1921
ALLOWED_DEBUG_LEVEL = 3
22+
23+
#POINTS DIMENSIONS
24+
RADIUS = 5
2025
SMALL_RADIUS = 1
2126
BIG_RADIUS = 5
2227

28+
#BUTTON POSITIONS
29+
OPEN_FOLDER_ROW,OPEN_FOLDER_COL = 0,0
30+
PREV_ROW,PREV_COL = 1,0
31+
NEXT_ROW,NEXT_COL = 1,1
32+
SAVE_ROW,SAVE_COL = 2,0
33+
DEL_SELECTED_ROW,DEL_SELECTED_COL = 3,0
34+
DROP_DOWN_ROW,DROP_DOWN_COL = 4,0
35+
SAVE_TYPE_ROW,SAVE_TYPE_COL = 4,1
36+
DESELECT_ALL_ROW,DESELECT_ALL_COL = 5,0
37+
SELECT_ALL_ROW, SELECT_ALL_COL = 6,0
38+
DRAW_POLY_ROW, DRAW_POLY_COL = 7,0
39+
DRAW_RECT_ROW, DRAW_RECT_COL = 8,0
40+
DELETE_ALL_ROW, DELETE_ALL_COL = 9,0
41+
SHOW_TYPE_ROW,SHOW_TYPE_COL = 10,0
42+
HIDE_TYPE_ROW,HIDE_TYPE_COL = 10,1
43+

draw_poly.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self,root,canvas,img_on_cnv,radius = 4):
1111
self.img_on_cnv = img_on_cnv
1212
self.points = []
1313
self.pt_coords = []
14+
self.polygon = None
1415
self.radius = SMALL_RADIUS
1516
self.canvas.update()
1617
self.canvas.bind('<ButtonRelease-1>',self.draw_point)
@@ -52,3 +53,11 @@ def chkup_rmb_point(self,event):
5253
self.canvas.delete(pt)
5354
self.points.pop(index_pt)
5455
self.pt_coords.pop(index_pt)
56+
57+
def delete_self(self):
58+
#self.canvas.delete(self.polygon)
59+
for pt in self.points:
60+
self.canvas.delete(pt)
61+
self.pt_coords = []
62+
self.polygon = None
63+
self.canvas.update()

draw_rect.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from tkinter import Tk,Canvas
2+
import tkinter as tk
3+
from config import *
4+
from log_debug import *
5+
import os
6+
7+
class DrawRect():
8+
def __init__(self,root,canvas,img_on_cnv,radius = 4):
9+
self.root = root
10+
self.canvas = canvas
11+
self.img_on_cnv = img_on_cnv
12+
self.points = []
13+
self.pt_coords = []
14+
self.radius = SMALL_RADIUS
15+
self.polygon = None
16+
self.canvas.update()
17+
self.canvas.bind("<ButtonPress-1>",self.lmb_down)
18+
self.canvas.bind("<ButtonRelease-1>",self.lmb_up)
19+
20+
def lmb_down(self,event):
21+
x,y = event.x,event.y
22+
23+
self.create_point(x,y)
24+
self.create_point(x,y)
25+
self.create_point(x,y)
26+
self.create_point(x,y)
27+
self.polygon = self.canvas.create_polygon(
28+
*self.flatten(),
29+
outline = 'blue',
30+
fill = '',
31+
tag = 'Rect')
32+
self.canvas.bind('<Motion>',self.mouse_motion)
33+
34+
def mouse_motion(self,event):
35+
x,y = event.x, event.y
36+
self.update_point(self.points[3],x,self.pt_coords[0][1])
37+
self.update_point(self.points[1],self.pt_coords[0][0],y)
38+
self.update_point(self.points[2],x,y)
39+
self.canvas.coords(self.polygon,*self.flatten())
40+
self.canvas.update()
41+
42+
def lmb_up(self,event):
43+
self.canvas.unbind('<Motion>')
44+
self.canvas.unbind('<ButtonRelease-1>')
45+
self.canvas.unbind('<ButtonPress-1>')
46+
47+
def flatten(self):
48+
l = self.pt_coords[:]
49+
return [item for sublist in l for item in sublist]
50+
51+
def create_point(self,x,y):
52+
self.pt_coords.append([x,y])
53+
self.points.append(
54+
self.canvas.create_oval(
55+
x-self.radius,y-self.radius,
56+
x+self.radius,y+self.radius,
57+
fill = "green",
58+
tag = "Point"
59+
)
60+
)
61+
62+
def update_point(self,point_id,x,y):
63+
self.canvas.coords(
64+
point_id,
65+
x-self.radius,y-self.radius,
66+
x+self.radius,y+self.radius
67+
)
68+
self.pt_coords[self.points.index(point_id)] = [x,y]
69+
70+
def delete_self(self):
71+
self.canvas.delete(self.polygon)
72+
for pt in self.points:
73+
self.canvas.delete(pt)
74+
self.pt_coords = []
75+
self.polygon = None
76+
self.canvas.update()

gui.py

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
from canvas import ImageOnCanvas
1111
from draw_poly import DrawPoly
12+
from draw_rect import DrawRect
1213
from PIL import Image,ImageTk
1314
from log_debug import logger,debug
1415

@@ -32,51 +33,58 @@ def __init__(self):
3233
self.bottom_frame = Frame(self.root,width = INIT_WIDTH - BUTTON_WIDTH)
3334

3435
self.load_image_directory_button = Button(self.top_frame1,text = 'Open Folder',command=self.load_directory,width = int(BUTTON_WIDTH), style ="Bold.TButton")
35-
self.load_image_directory_button.grid(row = 0,columnspan = 2,sticky = tk.W+tk.E)
36+
self.load_image_directory_button.grid(row = OPEN_FOLDER_ROW,columnspan = 2,sticky = tk.W+tk.E)
3637

3738
self.prev_img_button = Button(self.top_frame1,text = '← Prev',command=self.previous_img,state = tk.DISABLED,width = int(BUTTON_WIDTH/2), style ="Bold.TButton")
38-
self.prev_img_button.grid(row= 1, column = 0,sticky = tk.W+tk.E)
39+
self.prev_img_button.grid(row= PREV_ROW, column = 0,sticky = tk.W+tk.E)
3940

4041
self.next_img_button = Button(self.top_frame1,text = 'Next → ',command=self.next_img,width = int(BUTTON_WIDTH/2), style ="Bold.TButton")
41-
self.next_img_button.grid(row=1,column=1,sticky = tk.W+tk.E)
42+
self.next_img_button.grid(row=NEXT_COL,column=1,sticky = tk.W+tk.E)
4243

4344
self.save_image_button = Button(self.top_frame1, text = 'Save ', command = self.saver,width = int(BUTTON_WIDTH), style ="Bold.TButton")
44-
self.save_image_button.grid(row = 2, columnspan = 2,sticky = tk.W+tk.E)
45+
self.save_image_button.grid(row = SAVE_ROW, columnspan = 2,sticky = tk.W+tk.E)
4546

4647
self.delete_poly_button = Button(self.top_frame,text = 'Delete Selected',command = self.delete_selected,width = int(BUTTON_WIDTH), style ="Bold.TButton")
47-
self.delete_poly_button.grid(row=0,columnspan =2,sticky = tk.W+tk.E)
48+
self.delete_poly_button.grid(row=DEL_SELECTED_ROW,columnspan =2,sticky = tk.W+tk.E)
4849

4950
self.type_choices = TYPE_CHOICES
5051
self.variable = StringVar(self.top_frame)
5152
self.variable.set(self.type_choices[0])
5253
self.type_options = OptionMenu(self.top_frame,self.variable,*self.type_choices,style ="Bold.TButton")
5354
self.type_options.config(width = int(BUTTON_WIDTH/2))
54-
self.type_options.grid(row = 1,column = 0)
55+
self.type_options.grid(row = DROP_DOWN_ROW,column = 0)
5556

5657
self.save_type_button = Button(self.top_frame, text = 'Save Type', command = self.save_type,width = int(BUTTON_WIDTH/2), style ="Bold.TButton")
57-
self.save_type_button.grid(row = 1, column = 1,sticky = tk.W+tk.E)
58+
self.save_type_button.grid(row = SAVE_TYPE_ROW, column = 1,sticky = tk.W+tk.E)
5859

5960
self.deselect_all_button = Button(self.top_frame,text = 'Deselect All',command = self.deselect_all,width = BUTTON_WIDTH, style ="Bold.TButton")
60-
self.deselect_all_button.grid(row = 2,columnspan = 2,sticky = tk.W+tk.E)
61+
self.deselect_all_button.grid(row = DESELECT_ALL_ROW,columnspan = 2,sticky = tk.W+tk.E)
6162

6263
self.select_all_button = Button(self.top_frame,text = 'Select All', command = self.select_all, width = BUTTON_WIDTH, style ="Bold.TButton")
63-
self.select_all_button.grid(row = 3,columnspan = 2,sticky = tk.W+tk.E)
64+
self.select_all_button.grid(row = SELECT_ALL_ROW,columnspan = 2,sticky = tk.W+tk.E)
6465

6566
self.draw_poly_button = Button(self.top_frame,text = 'Draw Poly',command = self.draw_poly_func,width = BUTTON_WIDTH, style ="Bold.TButton")
66-
self.draw_poly_button.grid(row = 4,columnspan = 2,sticky = tk.W+tk.E)
67+
self.draw_poly_button.grid(row = DRAW_POLY_ROW,columnspan = 2,sticky = tk.W+tk.E)
68+
69+
self.draw_rect_button = Button(self.top_frame,text = 'Draw Rectangle',command = self.draw_rect_func,width = BUTTON_WIDTH, style ="Bold.TButton")
70+
self.draw_rect_button.grid(row = DRAW_RECT_ROW,columnspan = 2,sticky = tk.W+tk.E)
6771

6872
self.delete_all_button = Button(self.top_frame,text = 'Delete All',command = self.delete_all,width = BUTTON_WIDTH, style ="Bold.TButton")
69-
self.delete_all_button.grid(row = 5,columnspan = 2,sticky = tk.W+tk.E)
73+
self.delete_all_button.grid(row = DELETE_ALL_ROW,columnspan = 2,sticky = tk.W+tk.E)
7074

7175
self.save_poly_button = Button(self.top_frame,text = 'Save Poly',command = self.save_drawing,width = int(BUTTON_WIDTH/2), style ="Bold.TButton")
7276

7377
self.discard_poly_button = Button(self.top_frame,text = 'Discard Poly',command = self.discard_drawing,width = int(BUTTON_WIDTH/2), style ="Bold.TButton")
7478

79+
self.save_rect_button = Button(self.top_frame,text = 'Save Rect',command = self.save_drawing,width = int(BUTTON_WIDTH/2), style ="Bold.TButton")
80+
81+
self.discard_rect_button = Button(self.top_frame,text = 'Discard Rect',command = self.discard_drawing,width = int(BUTTON_WIDTH/2), style ="Bold.TButton")
82+
7583
self.show_type_button = Button(self.top_frame, text = 'Show Type', command = self.show_type, width = int(BUTTON_WIDTH/2), style = "Bold.TButton")
76-
self.show_type_button.grid(row = 6, column =0, columnspan = 1, sticky = tk.W+tk.E)
84+
self.show_type_button.grid(row = SHOW_TYPE_ROW, column =0, columnspan = 1, sticky = tk.W+tk.E)
7785

7886
self.hide_type_button = Button(self.top_frame, text = 'Hide Type', command = self.hide_type, width = int(BUTTON_WIDTH/2), style = "Bold.TButton")
79-
self.hide_type_button.grid(row = 6, columnspan = 1, column = 1, sticky = tk.W+tk.E)
87+
self.hide_type_button.grid(row = HIDE_TYPE_ROW, columnspan = 1, column = 1, sticky = tk.W+tk.E)
8088

8189
self.canvas = Canvas(self.bottom_frame,width = INIT_WIDTH - BUTTON_WIDTH, height = INIT_HEIGHT, borderwidth = 1)
8290
self.image_name = None
@@ -126,6 +134,8 @@ def show_buttons(self):
126134
self.delete_all_button.config(state = "normal")
127135
self.show_type_button.config(state = "normal")
128136
self.hide_type_button.config(state = "normal")
137+
self.draw_poly_button.config(state = "normal")
138+
self.draw_rect_button.config(state = "normal")
129139

130140
def select_all(self):
131141
for poly in self.img_cnv.polygons:
@@ -167,12 +177,12 @@ def load_new_img(self):
167177
self.img_cnv = None
168178
path = os.path.join(self.image_dir, self.image_name)
169179
self.img_cnv = ImageOnCanvas(self.root,self.canvas,path)
170-
logger("LOADING: "+self.img_cnv.image_path)
180+
logger("LOADED: "+self.img_cnv.image_path)
171181

172182
def load_directory(self):
173183
while True:
174184
selection = filedialog.askdirectory()
175-
if selection == ():
185+
if selection == () or selection == '':
176186
return
177187
self.root.directory = selection
178188
self.image_dir = self.root.directory
@@ -246,11 +256,22 @@ def draw_poly_func(self):
246256
self.deselect_all()
247257
self.img_cnv.drawing_polygon = True
248258
self.draw_poly_button.grid_forget()
249-
self.save_poly_button.grid(row = 4, column = 0,sticky = tk.W+tk.E)
250-
self.discard_poly_button.grid(row = 4, column = 1,sticky = tk.W+tk.E)
259+
self.save_poly_button.grid(row = DRAW_POLY_ROW, column = 0,sticky = tk.W+tk.E)
260+
self.discard_poly_button.grid(row = DRAW_POLY_ROW, column = 1,sticky = tk.W+tk.E)
251261
self.hide_buttons()
262+
self.draw_rect_button.config(state=tk.DISABLED)
252263
self.drawing_obj = DrawPoly(self.bottom_frame,self.canvas,self.img_cnv,RADIUS)
253264

265+
def draw_rect_func(self):
266+
self.deselect_all()
267+
self.img_cnv.drawing_polygon = True
268+
self.draw_rect_button.grid_forget()
269+
self.save_rect_button.grid(row = DRAW_RECT_ROW, column = 0,sticky = tk.W+tk.E)
270+
self.discard_rect_button.grid(row = DRAW_RECT_ROW, column = 1,sticky = tk.W+tk.E)
271+
self.hide_buttons()
272+
self.draw_poly_button.config(state=tk.DISABLED)
273+
self.drawing_obj = DrawRect(self.bottom_frame,self.canvas,self.img_cnv,RADIUS)
274+
254275
def save_drawing(self):
255276
self.show_buttons()
256277
self.img_cnv.drawing_polygon = False
@@ -265,21 +286,33 @@ def save_drawing(self):
265286
self.img_cnv.add_poly(new_poly_pts)
266287
#self.img_cnv.bbs.append(new_poly_pts)
267288
#self.img_cnv.draw_bbs([self.img_cnv.bbs[-1]])
268-
289+
#debug (1, str(type(self.drawing_obj)))
290+
if isinstance(self.drawing_obj,DrawRect):
291+
self.save_rect_button.grid_forget()
292+
self.discard_rect_button.grid_forget()
293+
self.draw_rect_button.grid(row = DRAW_RECT_ROW, columnspan = 2, sticky = tk.W + tk.E)
294+
elif isinstance(self.drawing_obj,DrawPoly):
295+
self.save_poly_button.grid_forget()
296+
self.discard_poly_button.grid_forget()
297+
self.draw_poly_button.grid(row = DRAW_POLY_ROW, columnspan = 2, sticky = tk.W + tk.E)
298+
self.drawing_obj.delete_self()
269299
self.drawing_obj = None
270-
self.save_poly_button.grid_forget()
271-
self.discard_poly_button.grid_forget()
272-
self.draw_poly_button.grid(row = 4,columnspan = 2,sticky = tk.W+tk.E)
273300

274301
def discard_drawing(self):
275302
self.show_buttons()
276303
self.img_cnv.drawing_polygon = False
277-
for pt in self.drawing_obj.points:
278-
self.canvas.delete(pt)
304+
#for pt in self.drawing_obj.points:
305+
# self.canvas.delete(pt)
306+
self.drawing_obj.delete_self()
307+
if isinstance(self.drawing_obj,DrawRect):
308+
self.save_rect_button.grid_forget()
309+
self.discard_rect_button.grid_forget()
310+
self.draw_rect_button.grid(row = DRAW_RECT_ROW, columnspan = 2, sticky = tk.W + tk.E)
311+
elif isinstance(self.drawing_obj,DrawPoly):
312+
self.save_poly_button.grid_forget()
313+
self.discard_poly_button.grid_forget()
314+
self.draw_poly_button.grid(row = DRAW_POLY_ROW, columnspan = 2, sticky = tk.W + tk.E)
279315
self.drawing_obj = None
280-
self.save_poly_button.grid_forget()
281-
self.discard_poly_button.grid_forget()
282-
self.draw_poly_button.grid(row = 4,columnspan = 2, sticky = tk.W+tk.E)
283316

284317

285318
gui = GUI()

imgs/007.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@
4747
{
4848
"poly_points": [
4949
[
50-
1001,
50+
1000,
5151
1307
5252
],
5353
[
5454
3017,
5555
1328
5656
],
5757
[
58-
2965,
59-
1146
58+
2979,
59+
1201
6060
],
6161
[
62-
1001,
62+
1000,
6363
1146
6464
]
6565
],
@@ -69,20 +69,20 @@
6969
{
7070
"poly_points": [
7171
[
72-
2644,
73-
2113
72+
2650,
73+
2133
7474
],
7575
[
76-
3609,
77-
2113
76+
3620,
77+
2063
7878
],
7979
[
80-
3609,
81-
1975
80+
3597,
81+
1963
8282
],
8383
[
84-
2644,
85-
1975
84+
2620,
85+
2013
8686
]
8787
],
8888
"id": "3",
@@ -113,20 +113,20 @@
113113
{
114114
"poly_points": [
115115
[
116-
2367,
117-
1969
116+
2372,
117+
2009
118118
],
119119
[
120-
3567,
121-
1969
120+
3570,
121+
1932
122122
],
123123
[
124124
3567,
125125
1819
126126
],
127127
[
128-
2367,
129-
1819
128+
2361,
129+
1870
130130
]
131131
],
132132
"id": "5",
@@ -169,8 +169,8 @@
169169
648
170170
],
171171
[
172-
555,
173-
648
172+
560,
173+
610
174174
]
175175
],
176176
"id": "7",

0 commit comments

Comments
 (0)