-
Notifications
You must be signed in to change notification settings - Fork 1
/
xrommtools.py
607 lines (520 loc) · 28.2 KB
/
xrommtools.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
"""
XROMM Tools for DeepLabCut
Developed by J.D. Laurence-Chasen
Functions:
xma_to_dlc: create DeepLabCut training dataset from data tracked in XMALab
analyze_xromm_videos: Predict 2D points for novel trials
dlc_to_xma: convert output of DeepLabCut to XMALab format 2D points file
add_frames: Add new frames corrected/tracked in XMALab to an existing training dataset
"""
import os
import pandas as pd
import numpy as np
import cv2
import random
from deeplabcut.pose_estimation_tensorflow.predict_videos import analyze_videos
def xma_to_dlc(path_config_file,data_path,dataset_name,scorer,nframes,nnetworks = 1, path_config_file_cam2 = []):
config = path_config_file[:-12]
cameras = [1,2]
picked_frames = []
dfs = []
idx = []
pnames = []
subs =[["c01","c1","C01","C1","Cam1","cam1","Cam01","cam01","Camera1","camera1"],["c02","c2","C02","C2","Cam2","cam2","Cam02","cam02","Camera2","camera2"]]
trialnames = [folder for folder in os.listdir(data_path) if os.path.isdir(os.path.join(data_path, folder)) and not folder.startswith('.')]
### PART 1: Pick frames for dataset
for trial in trialnames:
# Read 2D points file
contents = os.listdir(data_path+"/"+trial)
filename = [x for x in contents if ".csv" in x] # csv filename
df1=pd.read_csv(data_path+"/"+trial+"/"+filename[0], sep=',',header=None)
# read pointnames from header row
pointnames = df1.loc[0,::4].astype(str).str[:-7].tolist()
pnames.append(pointnames)
df1 = df1.loc[1:,].reset_index(drop=True) # remove header row
# temp_idx = rows where fewer than half of columns are NaN
ncol = df1.shape[1]
temp_idx = list(df1.index.values[(~pd.isnull(df1)).sum(axis = 1) >= ncol/2])
# randomize frames w/i each trial and append to index list
random.shuffle(temp_idx)
idx.append(temp_idx)
dfs.append(df1)
# a couple errors
if sum(len(x) for x in idx) < nframes:
raise ValueError('nframes is bigger than number of detected frames')
#if pointnames aren't the same across trials
if any(pnames[0] != x for x in pnames):
raise ValueError('Make sure point names are consistent across trials')
# pick frames to extract (NOTE this is random currently)
# current code iteratively picks one frame at a time from each shuffled trial until # of picked_frames hits nframes
# There is a much neater way to do this
count = 0
while sum(len(x) for x in picked_frames) < nframes:
for trialnum in range(len(idx)):
if sum(len(x) for x in picked_frames) < nframes:
if count == 0:
picked_frames.insert(trialnum,[idx[trialnum][count]])
elif count < len(idx[trialnum]):
picked_frames[trialnum] = picked_frames[trialnum]+[idx[trialnum][count]]
count += 1
### Part 2: Extract images and 2D point data
if nnetworks == 2:
configs = [path_config_file[:-12], path_config_file_cam2[:-12]]
for camera in cameras:
print("Extracting camera %d trial images and 2D points..."%camera)
relnames = []
data = pd.DataFrame()
# new training dataset folder
newpath = configs[camera-1]+"/labeled-data/"+dataset_name+"_cam"+str(camera)
h5_save_path = newpath+"/CollectedData_"+scorer+".h5"
csv_save_path = newpath+"/CollectedData_"+scorer+".csv"
if os.path.exists(newpath):
contents = os.listdir(newpath)
if contents:
raise ValueError('There are already data in the camera %d training dataset folder' %camera)
else:
os.makedirs(newpath) # make new folder
for trialnum,trial in enumerate(trialnames):
# get video file
file = []
contents = os.listdir(data_path+"/"+trial)
for name in contents:
if any(x in name for x in subs[camera-1]):
file = name
if not file:
raise ValueError('Cannot locate %s video file or image folder' %trial)
# if video file is actually folder of frames
if os.path.isdir(data_path+"/"+trial+"/"+file):
imgpath = data_path+"/"+trial+"/"+file
imgs = os.listdir(imgpath)
relpath = "labeled-data/"+dataset_name+"_cam"+str(camera)+"/"
frames = picked_frames[trialnum]
frames.sort()
for count,img in enumerate(imgs):
if count in frames:
image = cv2.imread(imgpath+"/"+img)
relname = relpath + trial + "_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(newpath + "/" + trial + "_%s.png" % str(count+1).zfill(4), image) # save frame
else:
# file is actually a file
# extract frames from video and convert to png
video = data_path+"/"+trial+"/"+file
relpath = "labeled-data/"+dataset_name+"_cam"+str(camera)+"/"
frames = picked_frames[trialnum]
frames.sort()
cap = cv2.VideoCapture(video)
success,image = cap.read()
count = 0
while success:
if count in frames:
relname = relpath + trial + "_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(newpath + "/" + trial + "_%s.png" % str(count+1).zfill(4), image) # save frame
success,image = cap.read()
count += 1
cap.release()
# extract 2D points data
df1= dfs[trialnum]
xpos = df1.iloc[frames,0+(camera-1)*2::4]
ypos = df1.iloc[frames,1+(camera-1)*2::4]
temp_data = pd.concat([xpos,ypos],axis=1).sort_index(axis=1)
data = pd.concat([data,temp_data])
### Part 3: Complete final structure of datafiles
dataFrame = pd.DataFrame()
temp = np.empty((data.shape[0],2,))
temp[:] = np.nan
for i,bodypart in enumerate(pointnames):
index = pd.MultiIndex.from_product([[scorer], [bodypart], ['x', 'y']],names=['scorer', 'bodyparts', 'coords'])
frame = pd.DataFrame(temp, columns = index, index = relnames)
frame.iloc[:,0:2] = data.iloc[:, 2*i:2*i+2].values.astype(float)
dataFrame = pd.concat([dataFrame, frame],axis=1)
dataFrame.replace('', np.nan, inplace=True)
dataFrame.replace(' NaN', np.nan, inplace=True)
dataFrame.replace(' NaN ', np.nan, inplace=True)
dataFrame.replace('NaN ', np.nan, inplace=True)
dataFrame.apply(pd.to_numeric)
dataFrame.to_hdf(h5_save_path, key="df_with_missing", mode="w")
dataFrame.to_csv(csv_save_path,na_rep='NaN')
print("...done.")
else:
relnames = []
data = pd.DataFrame()
# new training dataset folder
newpath = config+"/labeled-data/"+dataset_name
h5_save_path = newpath+"/CollectedData_"+scorer+".h5"
csv_save_path = newpath+"/CollectedData_"+scorer+".csv"
if os.path.exists(newpath):
contents = os.listdir(newpath)
if contents:
raise ValueError('There are already data in the camera %d training dataset folder' %camera)
else:
os.makedirs(newpath) # make new folder
for camera in cameras:
print("Extracting camera %d trial images and 2D points..."%camera)
for trialnum,trial in enumerate(trialnames):
# get video file
file = []
contents = os.listdir(data_path+"/"+trial)
for name in contents:
if any(x in name for x in subs[camera-1]):
file = name
if not file:
raise ValueError('Cannot locate %s video file or image folder' %trial)
# if video file is actually folder of frames
if os.path.isdir(data_path+"/"+trial+"/"+file):
imgpath = data_path+"/"+trial+"/"+file
imgs = os.listdir(imgpath)
relpath = "labeled-data/"+dataset_name+"/"
frames = picked_frames[trialnum]
frames.sort()
for count,img in enumerate(imgs):
if count in frames:
image = cv2.imread(imgpath+"/"+img)
relname = relpath + trial + "_cam"+str(camera)+ "_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(newpath + "/" + trial + "_cam"+str(camera)+ "_%s.png" % str(count+1).zfill(4), image) # save frame
else:
# file is actually a file
# extract frames from video and convert to png
video = data_path+"/"+trial+"/"+file
relpath = "labeled-data/"+dataset_name+"/"
frames = picked_frames[trialnum]
frames.sort()
cap = cv2.VideoCapture(video)
success,image = cap.read()
count = 0
while success:
if count in frames:
relname = relpath + trial + "_cam"+str(camera)+ "_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(newpath + "/" + trial + "_cam"+str(camera)+ "_%s.png" % str(count+1).zfill(4), image) # save frame
success,image = cap.read()
count += 1
cap.release()
# extract 2D points data
df1= dfs[trialnum]
xpos = df1.iloc[frames,0+(camera-1)*2::4]
ypos = df1.iloc[frames,1+(camera-1)*2::4]
temp_data = pd.concat([xpos,ypos],axis=1).sort_index(axis=1)
temp_data.columns = range(temp_data.shape[1])
data = pd.concat([data,temp_data])
### Part 3: Complete final structure of datafiles
dataFrame = pd.DataFrame()
temp = np.empty((data.shape[0],2,))
temp[:] = np.nan
for i,bodypart in enumerate(pointnames):
index = pd.MultiIndex.from_product([[scorer], [bodypart], ['x', 'y']],names=['scorer', 'bodyparts', 'coords'])
frame = pd.DataFrame(temp, columns = index, index = relnames)
frame.iloc[:,0:2] = data.iloc[:, 2*i:2*i+2].values.astype(float)
dataFrame = pd.concat([dataFrame, frame],axis=1)
dataFrame.replace('', np.nan, inplace=True)
dataFrame.replace(' NaN', np.nan, inplace=True)
dataFrame.replace(' NaN ', np.nan, inplace=True)
dataFrame.replace('NaN ', np.nan, inplace=True)
dataFrame.apply(pd.to_numeric)
dataFrame.to_hdf(h5_save_path, key="df_with_missing", mode="w")
dataFrame.to_csv(csv_save_path,na_rep='NaN')
print("...done.")
print("Training data extracted to projectpath/labeled-data. Now use deeplabcut.create_training_dataset")
def dlc_to_xma(cam1data,cam2data,trialname,savepath):
h5_save_path = savepath+"/"+trialname+"-Predicted2DPoints.h5"
csv_save_path = savepath+"/"+trialname+"-Predicted2DPoints.csv"
if isinstance(cam1data, str): #is string
if ".csv" in cam1data:
cam1data=pd.read_csv(cam1data, sep=',',header=None)
cam2data=pd.read_csv(cam2data, sep=',',header=None)
pointnames = list(cam1data.loc[1,1:].unique())
# reformat CSV / get rid of headers
cam1data = cam1data.loc[3:,1:]
cam1data.columns = range(cam1data.shape[1])
cam1data.index = range(cam1data.shape[0])
cam2data = cam2data.loc[3:,1:]
cam2data.columns = range(cam2data.shape[1])
cam2data.index = range(cam2data.shape[0])
elif ".h5" in cam1data:# is .h5 file
cam1data = pd.read_hdf(cam1data)
cam2data = pd.read_hdf(cam2data)
pointnames = list(cam1data.columns.get_level_values('bodyparts').unique())
else:
raise ValueError('2D point input is not in correct format')
else:
pointnames = list(cam1data.columns.get_level_values('bodyparts').unique())
# make new column names
nvar = len(pointnames)
pointnames = [item for item in pointnames for repetitions in range(4)]
post = ["_cam1_X", "_cam1_Y", "_cam2_X", "_cam2_Y"]*nvar
cols = [m+str(n) for m,n in zip(pointnames,post)]
# remove likelihood columns
cam1data = cam1data.drop(cam1data.columns[2::3],axis=1)
cam2data = cam2data.drop(cam2data.columns[2::3],axis=1)
# replace col names with new indices
c1cols = list(range(0,cam1data.shape[1]*2,4)) + list(range(1,cam1data.shape[1]*2,4))
c2cols = list(range(2,cam1data.shape[1]*2,4)) + list(range(3,cam1data.shape[1]*2,4))
c1cols.sort()
c2cols.sort()
cam1data.columns = c1cols
cam2data.columns = c2cols
df = pd.concat([cam1data,cam2data],axis=1).sort_index(axis=1)
df.columns = cols
df.to_hdf(h5_save_path, key="df_with_missing", mode="w")
df.to_csv(csv_save_path,na_rep='NaN',index=False)
def analyze_xromm_videos(path_config_file,path_data_to_analyze,iteration,nnetworks = 1, path_config_file_cam2 = []):
# assumes you have cam1 and cam2 videos as .avi in their own seperate trial folders
# assumes all folders w/i new_data_path are trial folders
# convert jpg stacks?
# analyze videos
cameras = [1,2]
config = path_config_file
configs = [path_config_file, path_config_file_cam2]
subs =[["c01","c1","C01","C1","Cam1","cam1","Cam01","cam01","Camera1","camera1"],["c02","c2","C02","C2","Cam2","cam2","Cam02","cam02","Camera2","camera2"]]
trialnames = os.listdir(path_data_to_analyze)
for trialnum,trial in enumerate(trialnames):
trialpath = path_data_to_analyze + "/" + trial
contents = os.listdir(trialpath)
savepath = trialpath + "/" + "it%d"%iteration
if os.path.exists(savepath):
temp = os.listdir(savepath)
if temp:
raise ValueError('There are already predicted points in iteration %d subfolders' %iteration)
else:
os.makedirs(savepath) # make new folder
# get video file
for camera in cameras:
file = []
for name in contents:
if any(x in name for x in subs[camera-1]):
file = name
if not file:
raise ValueError('Cannot locate %s video file or image folder' %trial)
video = trialpath + "/" + file
#analyze video
if nnetworks == 1:
analyze_videos(config,[video],destfolder = savepath,save_as_csv=True)
else:
analyze_videos(configs[camera-1],[video],destfolder = savepath,save_as_csv=True)
# get filenames and read analyzed data
contents = os.listdir(savepath)
datafiles = [s for s in contents if '.h5' in s]
if not datafiles:
raise ValueError('Cannot find predicted points. Some wrong with DeepLabCut?')
cam1data = pd.read_hdf(savepath+"/"+datafiles[0])
cam2data = pd.read_hdf(savepath+"/"+datafiles[1])
dlc_to_xma(cam1data,cam2data,trial,savepath)
def add_frames(path_config_file, data_path, iteration, frames, nnetworks = 1, path_config_file_cam2 = "enterpathofcam2config"):
#input: config file paths, path of data to add to trainingdataset, frames-csv file where first col is trialnames and following cols are frame numbers
# will look for 2D points file based on name (if there are multiple csv files)
configs = [path_config_file[:-12], path_config_file_cam2[:-12]]
cameras = [1,2]
subs =[["c01","c1","C01","C1","Cam1","cam1","Cam01","cam01","Camera1","camera1"],["c02","c2","C02","C2","Cam2","cam2","Cam02","cam02","Camera2","camera2"]]
pts = ["2Dpts","2dpts","2DPts","2dPts","pts2D","Pts2D","pts2d","points2D","Points2d","points2d","2Dpoints","2dpoints","2DPoints"]
corr = ["correct","Correct","corrected","Corrected"]
# read frames from csv
if '.csv' in frames:
f = pd.read_csv(frames,header=None)
trialnames = list(f.iloc[:,0]) # first row of frames file must be trialnames
picked_frames = []
# this is disgusting code
for row in range(f.shape[0]):
picked_frames.append(list(f.loc[row,1:]))
for count,row in enumerate(picked_frames):
picked_frames[count] = [x for x in row if str(x) != 'nan'] # remove nans
for count,row in enumerate(picked_frames):
picked_frames[count] = [int(x) for x in row] # convert to int
else:
raise ValueError('frames must be a .csv file with trialnames and frame numbers')
if nnetworks == 2:
for camera in cameras:
contents = os.listdir(configs[camera-1]+'/'+'labeled-data')
if len(contents) == 1:
dataset_name = contents[0]
labeleddata_path = configs[camera-1] + '/' + 'labeled-data/' + dataset_name
else:
raise ValueError('There must be only one data set in the labeled-data folder')
contents = os.listdir(labeleddata_path)
h5file = [x for x in contents if '.h5' in x]
csvfile = [x for x in contents if '.csv' in x]
data = pd.read_hdf(labeleddata_path+'/'+h5file[0]) # read old point labels
## Extract selected frames from videos
for trialnum,trial in enumerate(trialnames):
# get video file
file = []
relnames = []
contents = os.listdir(data_path+"/"+trial)
for name in contents:
if any(x in name for x in subs[camera-1]):
file = name
if not file:
raise ValueError('Cannot locate %s video file or image folder' %trial)
# if video file is actually folder of frames
if os.path.isdir(data_path+"/"+trial+"/"+file):
imgpath = data_path+"/"+trial+"/"+file
imgs = os.listdir(imgpath)
relpath = "labeled-data/"+dataset_name+"/"
frames = picked_frames[trialnum]
frames.sort()
for count,img in enumerate(imgs):
if count+1 in frames: # ASSUMES FRAMES PROVIDED ARE 1 index
image = cv2.imread(imgpath+"/"+img)
relname = relpath + trial + "_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(labeleddata_path + "/" + trial + "_%s.png" % str(count+1).zfill(4), image) # save frame
else:
# file is actually a file
# extract frames from video and convert to png
video = data_path+"/"+trial+"/"+file
relpath = "labeled-data/"+dataset_name+"/"
frames = picked_frames[trialnum]
frames.sort()
cap = cv2.VideoCapture(video)
success,image = cap.read()
count = 0
while success:
if count+1 in frames:
relname = relpath + trial + "_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(labeleddata_path + "/" + trial + "_%s.png" % str(count+1).zfill(4), image) # save frame
success,image = cap.read()
count += 1
cap.release()
# get 2D points file / data
# extract 2D points data
contents = os.listdir(data_path+"/"+trial+"/"+"it"+str(iteration))
pointsfile = [x for x in contents if '.csv' in x]
if not pointsfile:
raise ValueError('Cannot locate %s 2D points file' %trial)
# if multiple csv files, look for "2Dpoints" in the name
if len(pointsfile) > 1:
t= []
for q in pointsfile:
if any(x in q for x in pts):
t = t + [q]
# if there are multiple 2D points files, look for "corrected" in the name
if len(t) > 1:
for r in pointsfile:
if any(x in r for x in corr):
file = r
else:
file = t[0]
else:
file = pointsfile[0]
print("Reading and adding the following frames from " + data_path+'/'+trial+"/"+"it"+str(iteration)+'/'+file)
df = pd.read_csv(data_path+'/'+trial+"/"+"it"+str(iteration)+'/'+file,sep=',',header=None)
df = df.loc[1:,].reset_index(drop=True)
print(frames)
frames = [x - 1 for x in frames] # account for zero index in python
xpos = df.iloc[frames,0+(camera-1)*2::4]
ypos = df.iloc[frames,1+(camera-1)*2::4]
temp_data = pd.concat([xpos,ypos],axis=1).sort_index(axis=1)
if temp_data.shape[1] > data.shape[1]:
raise ValueError('There are %d extra points in the corrected points file'%((temp_data.shape[1] - data.shape[1])/2))
if temp_data.shape[1] < data.shape[1]:
raise ValueError('There are %d missing points in the corrected points file'%((data.shape[1] - temp_data.shape[1])/2))
temp_data.index = relnames
temp_data.columns = data.columns
data = pd.concat([data,temp_data])
data.replace(' NaN', np.nan, inplace=True)
data.replace(' NaN ', np.nan, inplace=True)
data.replace('NaN ', np.nan, inplace=True)
data = data.astype('float')
data = data.round(2)
data = data.apply(pd.to_numeric)
data.to_hdf(labeleddata_path+'/'+h5file[0], key="df_with_missing", mode="w")
data.to_csv(labeleddata_path+'/'+csvfile[0],na_rep='NaN')
else: # default, one network for both videos
config = path_config_file[:-12]
contents = os.listdir(config+'/'+'labeled-data')
if len(contents) == 1:
dataset_name = contents[0]
labeleddata_path = config + '/' + 'labeled-data/' + dataset_name
else:
raise ValueError('There must be only one data set in the labeled-data folder')
contents = os.listdir(labeleddata_path)
h5file = [x for x in contents if '.h5' in x]
csvfile = [x for x in contents if '.csv' in x]
data = pd.read_hdf(labeleddata_path+'/'+h5file[0]) # read old point labels
for camera in cameras:
## Extract selected frames from videos
for trialnum,trial in enumerate(trialnames):
# get video file
relnames = []
file = []
contents = os.listdir(data_path+"/"+trial)
for name in contents:
if any(x in name for x in subs[camera-1]):
file = name
if not file:
raise ValueError('Cannot locate %s video file or image folder' %trial)
# if video file is actually folder of frames
if os.path.isdir(data_path+"/"+trial+"/"+file):
imgpath = data_path+"/"+trial+"/"+file
imgs = os.listdir(imgpath)
relpath = "labeled-data/"+dataset_name+"/"
frames = picked_frames[trialnum]
frames.sort()
for count,img in enumerate(imgs):
if count+1 in frames: # ASSUMES FRAMES PROVIDED ARE 1 index
image = cv2.imread(imgpath+"/"+img)
relname = relpath + trial + "_cam"+str(camera) +"_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(labeleddata_path + "/" + trial + "_cam"+str(camera) + "_%s.png" % str(count+1).zfill(4), image) # save frame
else:
# file is actually a file
# extract frames from video and convert to png
video = data_path+"/"+trial+"/"+file
relpath = "labeled-data/"+dataset_name+"/"
frames = picked_frames[trialnum]
frames.sort()
cap = cv2.VideoCapture(video)
success,image = cap.read()
count = 0
while success:
if count+1 in frames:
relname = relpath + trial + "_cam"+str(camera) + "_%s.png" % str(count+1).zfill(4)
relnames = relnames + [relname]
cv2.imwrite(labeleddata_path + "/" + trial + "_cam"+str(camera)+ "_%s.png" % str(count+1).zfill(4), image) # save frame
success,image = cap.read()
count += 1
cap.release()
# get 2D points file / data
# extract 2D points data
contents = os.listdir(data_path+"/"+trial+"/"+"it"+str(iteration))
pointsfile = [x for x in contents if '.csv' in x]
if not pointsfile:
raise ValueError('Cannot locate %s 2D points file' %trial)
# if multiple csv files, look for "2Dpoints" in the name
if len(pointsfile) > 1:
t= []
for q in pointsfile:
if any(x in q for x in pts):
t = t + [q]
# if there are multiple 2D points files, look for "corrected" in the name
if len(t) > 1:
for r in pointsfile:
if any(x in r for x in corr):
pointsfile = r
else:
pointsfile = pointsfile[0]
if isinstance(pointsfile,str) != True:
raise ValueError('Please check the points files in trial '+trial+' iteration '+str(iteration)+' folder')
df = pd.read_csv(data_path+'/'+trial+"/"+"it"+str(iteration)+'/'+pointsfile,sep=',',header=None)
df = df.loc[1:,].reset_index(drop=True)
xpos = df.iloc[frames,0+(camera-1)*2::4]
ypos = df.iloc[frames,1+(camera-1)*2::4]
temp_data = pd.concat([xpos,ypos],axis=1).sort_index(axis=1)
temp_data.index = relnames
if temp_data.shape[1] > data.shape[1]:
raise ValueError('There are %d extra points in the corrected points file'%((temp_data.shape[1] - data.shape[1])/2))
if temp_data.shape[1] < data.shape[1]:
raise ValueError('There are %d missing points in the corrected points file'%((data.shape[1] - temp_data.shape[1])/2))
temp_data.columns = data.columns
data = pd.concat([data,temp_data])
data.replace(' NaN', np.nan, inplace=True)
data.replace(' NaN ', np.nan, inplace=True)
data.replace('NaN ', np.nan, inplace=True)
data = data.astype('float')
data = data.round(2)
data = data.apply(pd.to_numeric)
data.to_hdf(labeleddata_path+'/'+h5file[0], key="df_with_missing", mode="w")
data.to_csv(labeleddata_path+'/'+csvfile[0],na_rep='NaN')
print('Frames from %d trials successfully added to training dataset'%len(trialnames))