-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
231 lines (194 loc) · 7.3 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# pyinstaller main.py --name AILabel --onefile --hidden-import Shapely --hidden-import yaml --hidden-import="PIL.E
# xifTags" --hidden-import seaborn
import os
import cv2
import torch
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)
CLASSNUMBER = 0
LABELS = []
CLASSES = []
# NextImage = False
# Get classes from Classes.txt file
with open('Classes.txt') as f:
lines = f.readlines()
for line in lines:
CLASSES.append(line.rstrip())
# Updates the slider and text at the top right of the screen
def updateclass(input):
global CLASSNUMBER
global LABELS
CLASSNUMBER = input
drawImage(LABELS)
return
downcoords = None
upcoords = None
def mousefunction(event, x, y, flags, param):
if event == cv2.EVENT_RBUTTONDOWN:
global LABELS
Labels = LABELS
image = cv2.imread(CURRENTIMAGE)
NewLabels = []
# Checking if the location right clicked was in or near one of the bounded boxes
# If so, remove the label and redraw the boxes
for label in Labels:
point = Point(x, y)
slabel = label.split(" ")
height, width, channels = image.shape
x_center, y_center, w, h = float(slabel[1]) * width, float(slabel[2]) * height, float(
slabel[3]) * width, float(slabel[4]) * height
x1 = round(x_center - w / 2)
y1 = round(y_center - h / 2)
x2 = round(x_center + w / 2)
y2 = round(y_center + h / 2)
polygon = Polygon([(x1, y2), (x2, y2), (x2, y1), (x1, y1)])
if not polygon.buffer(20.0).contains(point):
NewLabels.append(label)
else:
print("Removing Label")
LABELS = NewLabels # Update Labels List
drawImage(NewLabels) # ReDraw Image with new labels
return
if event == cv2.EVENT_LBUTTONDOWN:
global downcoords
downcoords = [x, y]
elif event == cv2.EVENT_LBUTTONUP:
global upcoords
upcoords = [x, y]
Labels = LABELS
NewLabels = []
image = cv2.imread(CURRENTIMAGE)
inputX = image.shape[1]
inputY = image.shape[0]
x1 = downcoords[0]
y1 = downcoords[1]
x2 = upcoords[0]
y2 = upcoords[1]
width = (abs(x2 - x1))
height = (abs(y2 - y1))
yoloX = "{:.6f}".format(((x1 + x2) / 2) / inputX)
yoloY = "{:.6f}".format(((y1 + y2) / 2) / inputY)
yoloWidth = "{:.6f}".format(width / inputX)
yoloHeight = "{:.6f}".format(height / inputY)
global CLASSNUMBER
label = str(CLASSNUMBER) + " " + yoloX + " " + yoloY + " " + yoloWidth + " " + yoloHeight
Labels.append(label)
LABELS = Labels # Update labels list with added label
drawImage(Labels)
return
def drawImage(labels):
global CURRENTIMAGE
image = cv2.imread(CURRENTIMAGE)
for label in labels:
label = label.split(" ")
height, width, channels = image.shape
x_center, y_center, w, h = float(label[1])*width, float(label[2])*height, float(label[3])*width, float(label[4])*height
x1 = round(x_center-w/2)
y1 = round(y_center-h/2)
x2 = round(x_center+w/2)
y2 = round(y_center+h/2)
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 255), 2)
cv2.putText(
img=image,
text=CLASSES[int(label[0])],
org=(x1, y1 - 10),
fontFace=cv2.FONT_HERSHEY_DUPLEX,
fontScale=1.0,
color=(0, 0, 0),
thickness=3
)
cv2.putText(
img=image,
text=CLASSES[int(label[0])],
org=(x1, y1 - 10),
fontFace=cv2.FONT_HERSHEY_DUPLEX,
fontScale=1.0,
color=(255, 255, 255),
thickness=2
)
global CLASSNUMBER
cv2.rectangle(image, (0, 0), (400, 40), (255, 255, 255), -1)
im = cv2.putText(
img=image,
text=CLASSES[CLASSNUMBER],
org=(5, 25),
fontFace=cv2.FONT_HERSHEY_DUPLEX,
fontScale=1.0,
color=(0, 0, 0),
thickness=2
)
cv2.imshow("AI Label", image)
def AIDetections(filename):
directory = 'images'
im = cv2.imread(os.path.join(directory, filename))
inputX = im.shape[1]
inputY = im.shape[0]
Labels = []
results = model(os.path.join(directory, filename))
detections = results.xyxy[0]
for detection in detections:
x1 = int(detection[0].item())
y1 = int(detection[1].item())
x2 = int(detection[2].item())
y2 = int(detection[3].item())
width = (abs(x2 - x1))
height = (abs(y2 - y1))
yoloX = "{:.6f}".format(((x1 + x2) / 2) / inputX)
yoloY = "{:.6f}".format(((y1 + y2) / 2) / inputY)
yoloWidth = "{:.6f}".format(width / inputX)
yoloHeight = "{:.6f}".format(height / inputY)
classNum = int(detection[5].item())
label = str(classNum) + " " + yoloX + " " + yoloY + " " + yoloWidth + " " + yoloHeight
Labels.append(label)
return Labels, im
CURRENTIMAGE = ""
def main():
# CHANGE THIS TO THE DIRECTORY OF YOUR PHOTOS
FileList = []
# Check whether the specified path exists or not
isExist = os.path.exists('images')
if not isExist:
# Create a new directory because it does not exist
os.makedirs('images')
print("Images Directory Created")
directory = R'images'
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".png"):
FileList.append(filename)
cv2.namedWindow("AI Label", cv2.WINDOW_GUI_NORMAL)
cv2.createTrackbar('r', 'AI Label', 0, len(CLASSES)-1, updateclass)
for image in FileList:
global CURRENTIMAGE
CURRENTIMAGE = os.path.join(directory, image)
Labels, im = AIDetections(image)
drawImage(Labels)
global LABELS
LABELS = Labels
cv2.setMouseCallback('AI Label', mousefunction, param=[LABELS])
k = cv2.waitKey(0)
if k == 49: # 1 to Apply
print("APPLY")
filename = image
Labels = LABELS
path = 'labels' # SAVES LABELS TO THE LABELS FOLDER
# Check whether the specified path exists or not
isExist = os.path.exists(path)
if not isExist:
# Create a new directory because it does not exist
os.makedirs(path)
print("Labels Directory Created")
print("\nOUTPUT TO " + f'{filename[:-4]}.txt')
with open(os.path.join('labels/', f'{filename[:-4]}.txt'), 'w+') as f:
for label in Labels:
f.write(label + "\n")
print(label)
f.flush()
print('\n')
os.rename(CURRENTIMAGE, os.path.join('labels', image)) # Move labled image to labels folder
continue
elif k == ord('q'): # Press q to exit
print("Exiting")
exit()
if __name__ == "__main__":
main()