-
Notifications
You must be signed in to change notification settings - Fork 1
/
hextrack.py
181 lines (148 loc) · 6.58 KB
/
hextrack.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
import cv2
import time
import yaml
import argparse
import logging
import pkg_resources
from pathlib import Path
import threading
import os
from tqdm import tqdm
from moviepy.editor import VideoFileClip
from src.tracker import Tracker
from src.preprocessing import timecorrect
from src.preprocessing import Linearization
from src.preprocessing import Homography
# If true, no tracking is performed, can only be used if pos_log_files are already available in the system
ONLY_ANALYSIS = True
# Grab frames and return captured frame
class Grabber:
def __init__(self, src):
self.src = src
self.capture = cv2.VideoCapture(src)
def next(self):
rt, frame = self.capture.read()
return frame
# Loops through frames, capturing them and applying tracking
class OfflineHextrack:
def __init__(self, cfg, src, n, LED_pos, LED_tresholds):
threading.current_thread().name = 'HexTrack'
self.cfg = cfg
self.frame_idx = 0
self.n = n
self.mask_init = True
self.made_mask = None
# Create path to csv log file for tracking mouse position and LED-light state
path = pkg_resources.resource_filename(__name__, "/data/interim/position_log_files/{}".format(src[len(src)-29:len(src)-10]))
if not os.path.exists(path):
try:
os.mkdir(path)
except OSError:
print("Creation of the directory %s failed, this path probably already exists" % path)
self.path = pkg_resources.resource_filename(__name__, '/data/interim/Position_log_files/{}/pos_log_file_{}.csv'
.format(src[len(src)-29:len(src)-10], n))
# Initiation of the Grabbers and Trackers and creation of csv log file
self.grabber = Grabber(src)
self.tracker = Tracker(cfg, pos_log_file=open(self.path, 'w'), name=__name__, LED_pos=LED_pos, LED_thresholds=LED_tresholds)
logging.debug('HexTrack initialization done!')
self.vid = VideoFileClip(src)
self.duration = self.vid.duration*15
# Loops through grabbing and tracking each frame of the video file
def loop(self):
pbar = tqdm(range(int(self.duration)))
# pbar = tqdm(range(2000))
for i in pbar:
frame = self.grabber.next()
if frame is None:
break
# Checks if the frame has a mask already, if not, it creates a new mask
if self.mask_init:
self.tracker.apply(frame, self.frame_idx, n=self.n)
elif not self.mask_init:
self.tracker.apply(frame, self.frame_idx, mask_frame=self.made_mask, n=self.n)
# At the second frame, show computer-generated mask
# If not sufficient, gives possibility to input user-generated mask
# if self.frame_idx == 1:
# path = pkg_resources.resource_filename(__name__, '/output/Masks/mask_{}.png'.format(n))
# mask = cv2.imread(path)
# plt.figure('Mask check')
# plt.imshow(mask)
# plt.show()
# mask_check = input("If the mask is sufficient, enter y: ")
# if mask_check != 'y':
# input('Please upload custom mask under the name new_mask.png to the output folder and press enter')
# self.made_mask = cv2.imread('new_mask.png', 0)
# self.mask_init = False
# self.frame_idx += 1
self.tracker.close()
pbar.close()
self.vid.reader.close()
# Redundant, might be deleted later
def process_events(self, display=False):
if not display:
return
# Event loop call
key = cv2.waitKey(1)
# Process Keypress Events
if key == ord('q'):
self.stop()
def stop(self):
self.tracker.close()
cv2.destroyAllWindows()
raise SystemExit
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sources', nargs='*', help='List of sources to read from')
parser.add_argument('-d', '--debug', action='store_true', help='Debug mode')
parser.add_argument('-c', '--config', help='Configuration file')
parser.add_argument('-n', '--nodes', help='Node location file')
cli_args = parser.parse_args()
logfile = Path.home() / "Videos/hextrack/{}_hextrack_log".format(
time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(time.time())))
# Construct the shared array to fit all frames
cfg_path = pkg_resources.resource_filename(__name__, '/src/resources/default/default_config.yml')
if cli_args.config is not None:
cfg_path = Path(cli_args.config)
if not cfg_path.exists():
raise FileNotFoundError('Config file not found!')
with open(cfg_path, 'r') as cfg_f:
cfg = yaml.load(cfg_f, Loader=yaml.FullLoader)
if cli_args.sources is not None:
cfg['frame_sources'] = cli_args.sources
# Initiate calculation of the homography matrix, directly corrects all node and LED positions
homography = Homography(__name__, sources=cfg['frame_sources'])
homography.homography_calc()
# Initiates OfflineHextrack to track mouse positions and save position log files
for n, src in enumerate(cfg['frame_sources']):
print('Source {} @ {} starting'.format(n, src))
if not ONLY_ANALYSIS:
LED_pos = homography.LEDfind(sources=cfg['frame_sources'], iterations=200)
LED_tresholds = homography.LED_thresh(sources=cfg['frame_sources'], iterations=50, LED_pos=LED_pos)
ht = OfflineHextrack(cfg=cfg, src=src, n=n, LED_pos=LED_pos, LED_tresholds=LED_tresholds)
ht.loop()
logging.debug('Position files acquired')
tcorrect = timecorrect(__name__, sources=cfg['frame_sources'])
tcorrect.correction()
linearization = Linearization(__name__, sources=cfg['frame_sources'])
linearization.lin()
# Option to skip time correction and linearization
# key = input('Enter y for time correction and linearization: ')
# if key == 'y':
# tcorrect = timecorrect(__name__, sources=cfg['frame_sources'])
# tcorrect.correction()
# linearization = Linearization(__name__, sources=cfg['frame_sources'])
# linearization.lin()
# else:
# pass
#
# # Option to skip analysis
# key = input('Enter y for analysis: ')
#
# if key == 'y':
# display = Display(__name__)
# display.path_display()
# display.lin_path_display()
# display.dwell_time()
# display.ground_truth()
# if key != 'y':
# pass