-
Notifications
You must be signed in to change notification settings - Fork 39
/
imgLabel.py
165 lines (149 loc) · 4.8 KB
/
imgLabel.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
import os
import cv2
import sys, getopt
from parser import parser
from utils import save_info, load_info, go2frame, show_image
args = parser.parse_args()
video_path = args.label_video_path
if not os.path.isfile(video_path) or not video_path.endswith('.mp4'):
print("Not a valid video path! Please modify path in parser.py --label_video_path")
sys.exit(1)
# create information record dictionary
# Frame: index of frame
# Ball : 0 for no ball or not clearly visible, 1 for having ball
# x: x position of ball center
# y: y position of ball center
csv_path = args.csv_path
load_csv = False
if os.path.isfile(csv_path) and csv_path.endswith('.csv'):
load_csv = True
else:
print("Not a valid csv file! Please modify path in parser.py --csv_path")
# acquire video info
cap = cv2.VideoCapture(video_path)
fps = int(cap.get(cv2.CAP_PROP_FPS))
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
if load_csv:
info = load_info(csv_path)
if len(info) != n_frames:
print("Number of frames in video and dictionary are not the same!")
print("Fail to load, create new dictionary instead.")
info = {
idx:{
'Frame': idx,
'Ball': 0,
'x': -1,
'y': -1
} for idx in range(n_frames)
}
else:
print("Load labeled dictionary successfully.")
else:
print("Create new dictionary")
info = {
idx:{
'Frame': idx,
'Ball': 0,
'x': -1,
'y': -1
} for idx in range(n_frames)
}
# # # # # # # # # # # # # # # #
# e: exit program #
# s: save info #
# n: next frame #
# p: previous frame #
# f: to first frame #
# l: to last frame #
# >: fast forward 36 frames #
# <: fast backward 36 frames #
# # # # # # # # # # # # # # # #
def ball_label(event, x, y, flags, param):
global frame_no, info, image
if event == cv2.EVENT_LBUTTONDOWN:
h, w, _ = image.shape
info[frame_no]['x'] = x/w
info[frame_no]['y'] = y/h
info[frame_no]['Ball'] = 1
elif event == cv2.EVENT_MBUTTONDOWN:
info[frame_no]['x'] = -1
info[frame_no]['y'] = -1
info[frame_no]['Ball'] = 0
saved_success = False
frame_no = 0
_, image = cap.read()
show_image(image, 0, info[0]['x'], info[0]['y'])
while True:
leave = 'y'
cv2.imshow('imgLabel', image)
cv2.setMouseCallback('imgLabel', ball_label)
key = cv2.waitKey(1) & 0xFF
if key == ord('e'):
if not saved_success:
print("You forget to save file!")
while True:
leave = str(input("Really want to leave without saving? [Y/N]"))
leave = leave.lower()
if leave != 'y' and leave != 'n':
print("Please type 'y/Y' or 'n/N'")
continue
elif leave == 'y':
cap.release()
cv2.destroyAllWindows()
print("Exit label program")
sys.exit(1)
elif leave == 'n':
break
if leave == 'y':
cap.release()
cv2.destroyAllWindows()
print("Exit label program")
sys.exit(1)
elif key == ord('s'):
saved_success = save_info(info, video_path)
elif key == ord('n'):
if frame_no >= n_frames-1:
print("This is the last frame")
continue
frame_no += 1
image = go2frame(cap, frame_no, info)
print("Frame No.{}".format(frame_no))
elif key == ord('p'):
if frame_no == 0:
print("This is the first frame")
continue
frame_no -= 1
image = go2frame(cap, frame_no, info)
print("Frame No.{}".format(frame_no))
elif key == ord('f'):
if frame_no == 0:
print("This is the first frame")
continue
frame_no = 0
image = go2frame(cap, frame_no, info)
print("Frame No.{}".format(frame_no))
elif key == ord('l'):
if frame_no == n_frames-1:
print("This is the last frame")
continue
frame_no = n_frames-1
image = go2frame(cap, frame_no, info)
print("Frame No.{}".format(frame_no))
elif key == ord('>'):
if frame_no + 36 >= n_frames-1:
print("Reach last frame")
frame_no = n_frames-1
else:
frame_no += 36
image = go2frame(cap, frame_no, info)
print("Frame No.{}".format(frame_no))
elif key == ord('<'):
if frame_no - 36 <= 0:
print("Reach first frame")
frame_no = 0
else:
frame_no -= 36
image = go2frame(cap, frame_no, info)
print("Frame No.{}".format(frame_no))
else:
image = go2frame(cap, frame_no, info)