forked from msekatchev/pmt-annotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotate.py
118 lines (99 loc) · 3.75 KB
/
annotate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import cv2
import os
import numpy as np
#Set up callbacks for drawing circles on click and drag, bound to left and middle mouse
def draw_circle(event,x,y,flags,param):
global mouseX,mouseY
global ix, iy, drawing, rdrawing, mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
cv2.circle(img, (x, y), large_size,(255,0,0),-1)
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.circle(img, (x, y), large_size,(255,0,0),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if event == cv2.EVENT_MBUTTONDOWN:
rdrawing = True
ix,iy = x,y
cv2.circle(img, (x, y), small_size,(255,0,0),-1)
elif event == cv2.EVENT_MOUSEMOVE:
if rdrawing == True:
cv2.circle(img, (x, y), small_size,(255,0,0),-1)
elif event == cv2.EVENT_MBUTTONUP:
rdrawing = False
#Set up for a single image
def annotate_img(img_path, size1, size2) :
#Create window and put it in top left corner off screen
cv2.namedWindow('image')
cv2.moveWindow('image', 40, 30)
global drawing, rdrawing, large_size, small_size, img
large_size=size1
small_size=size2
img = cv2.imread(img_path)
cv2.setMouseCallback('image',draw_circle)
#Drawing and keyboard callbacks a to skip and delete, s to save image
drawing=False
rdrawing=False
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(20) & 0xFF
if k == ord('s'):
#cv2.destroyWindow('image')
break
#Make mask same colour as drawing and output binarised image
train_labels = img[:,:,:3]
lower = np.array([254,0,0], dtype = "uint16")
upper = np.array([255,0,0], dtype = "uint16")
mask = cv2.inRange(train_labels, lower, upper)
mask[mask < 250] = 0
mask[mask != 0 ] = 1
#save label in code directory
cv2.imwrite('label.png', mask )
cv2.destroyWindow('image')
return
#Set up for directory of images with file structure for image segmentation
def annotate_dir(img_dir, dataset, subset, size1, size2) :
#Create window and put it in top left corner off screen
cv2.namedWindow('image')
cv2.moveWindow('image', 40, 30)
global drawing, rdrawing, large_size, small_size, img
large_size=size1
small_size=size2
cv2.setMouseCallback('image',draw_circle)
#Array of names in directory to iterate over
f = []
for (dirpath, dirnames, filenames) in os.walk(f'{img_dir}{dataset}/{subset}_frames/{subset}'):
f.extend(filenames)
break
print(f)
for i in f :
skip = False
drawing = False
rdrawing = False
img = cv2.imread(f'{img_dir}{dataset}/{subset}_frames/{subset}/{str(i)}')
#Drawing and keyboard callbacks a to skip and delete, s to save image
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(20) & 0xFF
if k == ord('s'):
#cv2.destroyWindow('image')
break
elif k == ord('a'):
skip = True
#cv2.destroyWindow('image')
break
#Make mask same colour as drawing and output binarised image
train_labels = img[:,:,:3]
lower = np.array([254,0,0], dtype = "uint16")
upper = np.array([255,0,0], dtype = "uint16")
mask = cv2.inRange(train_labels, lower, upper)
mask[mask < 250] = 0
mask[mask != 0 ] = 1
#Save mask or delete image if it isnt good
if skip == False :
cv2.imwrite(f'{img_dir}{dataset}/{subset}_masks/{subset}/{str(i)}', mask )
else :
os.remove(f'{img_dir}{dataset}/{subset}_frames/{subset}/{str(i)}')
cv2.destroyWindow('image')