-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
186 lines (139 loc) · 4.97 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
import cv2
import json
import os
directory = 'imgs'
global orig_img
global img
global cache
global points_list
#define the events for the mouse_click
def mouse_click(event, x, y, flags, param):
global orig_img
global cache
global points_list
height, width, channels = cache.shape
# to check if left mouse button was clicked
if event == cv2.EVENT_LBUTTONDOWN:
#img = cache.copy()
# font for left click event
LB = 'Left Button'
# draw cirlce for joint annotation:
# Center coordinates
center_coordinates = (x, y)
# Radius of circle
radius = 7
# Blue color in BGR
color = (0, 0, 255)
# Line thickness of 2 px
thickness = -1
# Draw a blue cirlce on image and Normalize Coordinates(0, 1)
cv2.circle(cache, center_coordinates, radius, color, thickness)
cv2.imshow('main_window', cache)
normalized_center_coordinates = (center_coordinates[0]/width, center_coordinates[1]/height)
points_list.append(normalized_center_coordinates)
# to check if right mouse button was clicked
if event == cv2.EVENT_RBUTTONDOWN:
pass
# Create list with path/to/image and file names
img_path_list = list()
file_list = list()
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
file_root = filename.split(sep='.')
file_list.append(file_root[0])
img_path_list.append(os.path.join(directory, filename))
print(os.path.join(directory, filename))
else:
continue
# Annotate, Iterate through each image, and write results
i = len(file_list)
print(i)
first_read = True
while i > 0:
global orig_img
global cache
if first_read:
print('first read')
points_list = list()
# read image
orig_img = cv2.imread(img_path_list[-i])
height, width, channels = orig_img.shape
#print(height, width)
# Resize image
#img = cv2.resize(img, (960, 550))
orig_img = cv2.resize(orig_img, (0,0), fx=0.3, fy=0.3)
cache = orig_img.copy()
# show image and set Mouse Callback
cv2.imshow('main_window', cache)
cv2.setMouseCallback('main_window', mouse_click)
first_read = False
else:
cv2.imshow('main_window', cache)
print('not first read')
cv2.setMouseCallback('main_window', mouse_click)
key = cv2.waitKey(0)
### ASCII Table can be found at: asciitable.com ###
# If "p" is pressed - undo last annotation
if key == 101:
cache = orig_img.copy()
#cv2.destroyAllWindows()
print("p was pressed")
if points_list != []:
points_list.pop()
for point in points_list:
height, width, channels = cache.shape
print(point)
# Radius of circle
radius = 7
# Blue color in BGR
color = (0, 0, 255)
# Line thickness of 2 px
thickness = -1
center_coordinates = (int(point[0]*width), int(point[1]*height))
print(center_coordinates)
cv2.circle(cache, center_coordinates, radius, color, thickness)
cv2.imshow('main_window', cache)
# If spacebar is pressed - Skip Joint
# if key == 32:
# img_2 = orig_img.copy()
# height, width, channels = img_2.shape
# img_2 = cv2.resize(img_2, (0, 0), fx=0.3, fy=0.3)
# print('space')
# print(points_list)
# if points_list != []:
# print(points_list.pop())
# print(points_list)
# for point in points_list:
# print(point)
# # Radius of circle
# radius = 7
# # Blue color in BGR
# color = (0, 255, 0)
# # Line thickness of 2 px
# thickness = -1
# center_coordinates = (int(point[0]*width), int(point[1]*height))
# cv2.circle(img_2, center_coordinates, radius, color, thickness)
# cv2.imshow('main_window', img_2)
# If Enter key is pressed - Next Image
elif key == 13:
# Create Key Point Dictionary and Write JSON file
key_pts = dict()
for idx, point in enumerate(points_list):
key_pts[idx] = point
with open('results/' + '{}'.format(file_list[-i]) + '.json', 'w', encoding='utf-8') as f:
json.dump(key_pts, f, ensure_ascii=False, indent=4)
f.close()
# Continue on to next image
i -= 1
first_read = True
#continue
# If backspace is pressed - Previous Image
# elif key == 8:
# print('backspace mf')
# cv2.setMouseCallback('main_window', mouse_click)
# if escape key is pressed - Exit Program
elif key == 27:
break
# close all the opened windows
cv2.destroyAllWindows()
print(key_pts)