In [3]:
import subprocess, os, json

VIDEO_ROOT = "videos/"
VIDEO_FRAMES_ROOT = "video-frames/"

def get_immediate_subdirectories(a_dir):
    return [name for name in os.listdir(a_dir)
            if os.path.isdir(os.path.join(a_dir, name))]

def many_jsons_to_one_json(analysis_path):
    # many jsons to one json

    def get_frame_from_fn(fn_hi): 
        k = fn_hi.split("_")[0].split("/")[-1]
        return int(k)

    face_tracking_dir = analysis_path
    fns = [face_tracking_dir + f for f in os.listdir(face_tracking_dir) if "landmarks" in f]
    out_json = {}

    for fn in fns:

        with open(fn) as f: 
            j = json.load(f)
            res = j["result"]

            frame_number = str(get_frame_from_fn(fn))
            out_json[frame_number] = []

            for result in res:
                out_json[frame_number].append({
                        "face_minBBox": result["face_minBBox"],
                        "face_maxBBox": result["face_maxBBox"]
                    })
    with open(face_tracking_dir + "out.json", 'w') as f_out:
        json.dump(out_json, f_out)

In [2]:
many_jsons_to_one_json("analysis/face-tracking/equation/")

In [4]:
all_videos = [v for v in os.listdir(VIDEO_ROOT) if "small" not in v and v[0] !='.' if "arctic" not in v and "congo" not in v and "equation" not in v]

for video in all_videos:
    base = video.lower().split(".mp4")[0]
    video_path = VIDEO_ROOT + video
    video_frames_path = VIDEO_FRAMES_ROOT + base + "/"
    
    # delete any existing video frames
    sub_directory_paths = [VIDEO_FRAMES_ROOT + p + "/" for p in get_immediate_subdirectories(VIDEO_FRAMES_ROOT)]
    for d in sub_directory_paths: 
        for frame in os.listdir(d):
            os.remove(d + frame)
            
    # make a new directory
    if not os.path.exists(video_frames_path):
        os.mkdir(video_frames_path)
        
    analysis_path = "analysis/face-tracking/" + base + "/"
    if not os.path.exists(analysis_path): 
        os.mkdir(analysis_path)
        
        # split the video into frames
        # subprocess.call(["ffmpeg", "-i", video_path, video_frames_path + "%05d.png"])
        subprocess.call(["ffmpeg", "-i", video_path, "-vf", "fps=2", video_frames_path + "%05d.png"])

        # run facetracking analysis
        subprocess.call(["../../../../../resources/OpenFace/bin/FaceLandmarkImg", '-q', '-multi-view', '1',
                         '-wild', '-gaze', '-fdir', video_frames_path, "-ofdir", analysis_path])
        # pts to json
        subprocess.call(["node", "analysis/of2json.js", analysis_path])

        # many jsons to one json
        many_jsons_to_one_json(analysis_path)

In [13]:


In [11]:
out_json.keys()


Out[11]:
['344',
 '345',
 '346',
 '347',
 '340',
 '341',
 '342',
 '343',
 '348',
 '349',
 '298',
 '299',
 '296',
 '297',
 '294',
 '295',
 '292',
 '293',
 '290',
 '291',
 '270',
 '271',
 '272',
 '273',
 '274',
 '275',
 '276',
 '277',
 '278',
 '279',
 '108',
 '109',
 '102',
 '103',
 '100',
 '101',
 '106',
 '107',
 '104',
 '105',
 '1001',
 '1210',
 '99',
 '98',
 '91',
 '90',
 '93',
 '92',
 '95',
 '94',
 '97',
 '96',
 '559',
 '558',
 '555',
 '554',
 '557',
 '556',
 '551',
 '550',
 '553',
 '552',
 '1439',
 '1199',
 '1198',
 '1191',
 '1190',
 '1193',
 '1192',
 '1195',
 '1194',
 '1197',
 '1196',
 '1177',
 '1176',
 '1175',
 '1174',
 '1173',
 '1172',
 '1171',
 '1170',
 '1179',
 '1178',
 '511',
 '510',
 '513',
 '1285',
 '1284',
 '1287',
 '512',
 '1281',
 '1280',
 '1283',
 '1282',
 '1003',
 '879',
 '1289',
 '1288',
 '514',
 '689',
 '688',
 '685',
 '684',
 '687',
 '686',
 '681',
 '680',
 '683',
 '682',
 '458',
 '1226',
 '621',
 '873',
 '1224',
 '1223',
 '1222',
 '1221',
 '1220',
 '407',
 '406',
 '405',
 '1372',
 '1375',
 '1374',
 '1377',
 '400',
 '1379',
 '1378',
 '1219',
 '1342',
 '409',
 '408',
 '453',
 '454',
 '455',
 '1346',
 '1347',
 '379',
 '378',
 '371',
 '370',
 '373',
 '372',
 '375',
 '374',
 '377',
 '376',
 '393',
 '392',
 '391',
 '390',
 '397',
 '396',
 '395',
 '394',
 '399',
 '398',
 '895',
 '245',
 '244',
 '247',
 '246',
 '241',
 '240',
 '243',
 '242',
 '249',
 '248',
 '179',
 '178',
 '177',
 '176',
 '175',
 '174',
 '173',
 '172',
 '171',
 '170',
 '1502',
 '1503',
 '1500',
 '1501',
 '1506',
 '1507',
 '1504',
 '1505',
 '1227',
 '659',
 '1142',
 '1143',
 '1140',
 '1141',
 '1146',
 '1147',
 '1144',
 '1145',
 '1148',
 '1149',
 '692',
 '693',
 '690',
 '691',
 '696',
 '697',
 '694',
 '695',
 '698',
 '699',
 '542',
 '543',
 '540',
 '541',
 '546',
 '547',
 '544',
 '545',
 '548',
 '549',
 '414',
 '415',
 '416',
 '417',
 '410',
 '411',
 '412',
 '413',
 '1384',
 '1385',
 '1386',
 '1387',
 '1380',
 '1381',
 '1382',
 '1383',
 '368',
 '369',
 '366',
 '367',
 '364',
 '365',
 '362',
 '363',
 '360',
 '361',
 '380',
 '381',
 '382',
 '383',
 '384',
 '385',
 '386',
 '387',
 '388',
 '389',
 '258',
 '259',
 '252',
 '253',
 '250',
 '251',
 '256',
 '257',
 '254',
 '255',
 '168',
 '169',
 '164',
 '165',
 '166',
 '167',
 '160',
 '161',
 '162',
 '163',
 '678',
 '679',
 '670',
 '671',
 '809',
 '808',
 '803',
 '802',
 '801',
 '800',
 '807',
 '806',
 '805',
 '804',
 '608',
 '1159',
 '1158',
 '1155',
 '1154',
 '1157',
 '1156',
 '1151',
 '1150',
 '1153',
 '1152',
 '59',
 '58',
 '55',
 '54',
 '57',
 '56',
 '51',
 '50',
 '53',
 '52',
 '537',
 '536',
 '535',
 '534',
 '533',
 '532',
 '531',
 '530',
 '539',
 '538',
 '429',
 '428',
 '1399',
 '1398',
 '421',
 '420',
 '423',
 '422',
 '425',
 '424',
 '427',
 '426',
 '229',
 '228',
 '227',
 '226',
 '225',
 '224',
 '223',
 '222',
 '221',
 '220',
 '151',
 '150',
 '153',
 '152',
 '155',
 '154',
 '157',
 '156',
 '159',
 '158',
 '1524',
 '1526',
 '818',
 '819',
 '1527',
 '810',
 '811',
 '812',
 '813',
 '814',
 '815',
 '816',
 '817',
 '1523',
 '1490',
 '1397',
 '1492',
 '1493',
 '1494',
 '1495',
 '1496',
 '1396',
 '1498',
 '1499',
 '1395',
 '1394',
 '1393',
 '1392',
 '1391',
 '1390',
 '1128',
 '1129',
 '1120',
 '1121',
 '1122',
 '1123',
 '1124',
 '1125',
 '1126',
 '1127',
 '524',
 '525',
 '526',
 '527',
 '520',
 '521',
 '522',
 '523',
 '1014',
 '1015',
 '1016',
 '1017',
 '528',
 '529',
 '1012',
 '1013',
 '1234',
 '1235',
 '1236',
 '1237',
 '1230',
 '1231',
 '1232',
 '1233',
 '1238',
 '1239',
 '438',
 '439',
 '436',
 '437',
 '434',
 '435',
 '432',
 '433',
 '430',
 '431',
 '238',
 '239',
 '234',
 '235',
 '236',
 '237',
 '230',
 '231',
 '232',
 '233',
 '1',
 '146',
 '147',
 '144',
 '145',
 '142',
 '143',
 '140',
 '141',
 '148',
 '149',
 '939',
 '938',
 '933',
 '932',
 '931',
 '930',
 '937',
 '936',
 '935',
 '934',
 '829',
 '828',
 '825',
 '824',
 '827',
 '826',
 '821',
 '820',
 '823',
 '822',
 '1483',
 '1482',
 '1481',
 '1480',
 '1487',
 '1486',
 '1485',
 '1484',
 '1488',
 '797',
 '796',
 '795',
 '794',
 '793',
 '792',
 '791',
 '790',
 '799',
 '798',
 '1270',
 '1271',
 '1272',
 '1139',
 '1138',
 '1133',
 '1132',
 '1131',
 '1130',
 '1137',
 '1136',
 '1135',
 '1134',
 '1276',
 '1277',
 '519',
 '518',
 '1009',
 '1008',
 '1007',
 '1006',
 '1005',
 '1004',
 '515',
 '1002',
 '517',
 '1000',
 '623',
 '622',
 '1225',
 '620',
 '627',
 '626',
 '625',
 '624',
 '629',
 '628',
 '1229',
 '1228',
 '2',
 '1286',
 '11',
 '10',
 '13',
 '12',
 '15',
 '14',
 '17',
 '16',
 '19',
 '18',
 '201',
 '200',
 '203',
 '202',
 '205',
 '204',
 '207',
 '206',
 '209',
 '208',
 '928',
 '929',
 '920',
 '921',
 '922',
 '923',
 '924',
 '925',
 '926',
 '927',
 '832',
 '833',
 '830',
 '831',
 '836',
 '837',
 '834',
 '835',
 '838',
 '839',
 '3',
 '784',
 '785',
 '786',
 '787',
 '780',
 '781',
 '782',
 '783',
 '788',
 '789',
 '60',
 '61',
 '62',
 '63',
 '64',
 '65',
 '66',
 '67',
 '68',
 '69',
 '1371',
 '1370',
 '1373',
 '1038',
 '404',
 '508',
 '509',
 '1032',
 '403',
 '1030',
 '505',
 '502',
 '503',
 '500',
 '402',
 '1212',
 '1213',
 '632',
 '633',
 '1216',
 '401',
 '636',
 '637',
 '638',
 '639',
 '1218',
 '1376',
 '465',
 '1106',
 '1107',
 '1104',
 '1105',
 '1102',
 '1103',
 '1100',
 '1101',
 '1459',
 '1108',
 '1109',
 '216',
 '217',
 '214',
 '215',
 '212',
 '213',
 '210',
 '211',
 '218',
 '219',
 '4',
 '919',
 '918',
 '915',
 '917',
 '916',
 '910',
 '912',
 '847',
 '846',
 '845',
 '844',
 '843',
 '842',
 '841',
 '840',
 '849',
 '848',
 '663',
 '662',
 '1039',
 '753',
 '752',
 '751',
 '750',
 '757',
 '756',
 '755',
 '754',
 '759',
 '758',
 '506',
 '507',
 '504',
 '1025',
 '1024',
 '1027',
 '1031',
 '1021',
 '1020',
 '1023',
 '1022',
 '1036',
 '1029',
 '1028',
 '1037',
 '1034',
 '501',
 '605',
 '604',
 '607',
 '606',
 '601',
 '600',
 '603',
 '602',
 '1205',
 '1204',
 '1207',
 '1206',
 '609',
 '1200',
 '1203',
 '1202',
 '1211',
 '634',
 '1217',
 '1214',
 '1215',
 '1111',
 '1110',
 '1113',
 '1112',
 '1115',
 '1114',
 '1117',
 '464',
 '1119',
 '1118',
 '467',
 '1448',
 '466',
 '1357',
 '460',
 '1355',
 '489',
 '488',
 '487',
 '486',
 '485',
 '1354',
 '483',
 '482',
 '481',
 '480',
 '199',
 '198',
 '195',
 '194',
 '197',
 '196',
 '191',
 '190',
 '193',
 '192',
 '902',
 '903',
 '900',
 '901',
 '907',
 '904',
 '905',
 '1511',
 '909',
 '854',
 '855',
 '856',
 '857',
 '850',
 '851',
 '852',
 '853',
 '858',
 '859',
 '6',
 '740',
 '741',
 '742',
 '743',
 '744',
 '745',
 '746',
 '747',
 '748',
 '749',
 '1050',
 '1051',
 '1052',
 '1053',
 '1054',
 '1055',
 '1056',
 '1057',
 '1058',
 '1059',
 '1278',
 '1279',
 '618',
 '619',
 '612',
 '613',
 '610',
 '611',
 '616',
 '617',
 '614',
 '615',
 '1491',
 '1473',
 '1470',
 '1471',
 '1477',
 '1474',
 '1475',
 '1479',
 '1304',
 '1305',
 '1306',
 '1307',
 '1300',
 '1301',
 '1302',
 '1303',
 '1497',
 '1308',
 '1309',
 '498',
 '499',
 '494',
 '495',
 '496',
 '497',
 '490',
 '491',
 '492',
 '493',
 '24',
 '25',
 '26',
 '27',
 '20',
 '21',
 '22',
 '23',
 '28',
 '29',
 '7',
 '1087',
 '1086',
 '977',
 '976',
 '975',
 '974',
 '973',
 '972',
 '971',
 '970',
 '979',
 '978',
 '182',
 '183',
 '180',
 '181',
 '186',
 '187',
 '184',
 '185',
 '188',
 '189',
 '1464',
 '869',
 '868',
 '861',
 '860',
 '863',
 '862',
 '865',
 '864',
 '867',
 '866',
 '883',
 '882',
 '881',
 '880',
 '887',
 '886',
 '885',
 '884',
 '889',
 '888',
 '775',
 '774',
 '777',
 '776',
 '771',
 '770',
 '773',
 '772',
 '779',
 '778',
 '77',
 '76',
 '75',
 '74',
 '73',
 '72',
 '71',
 '70',
 '79',
 '78',
 '1043',
 '1042',
 '1041',
 '1040',
 '1047',
 '1046',
 '1045',
 '1044',
 '1049',
 '1048',
 '1269',
 '1268',
 '669',
 '668',
 '667',
 '1262',
 '665',
 '664',
 '1267',
 '1266',
 '1265',
 '1264',
 '1469',
 '1468',
 '1018',
 '1467',
 '1461',
 '1460',
 '1019',
 '1317',
 '1316',
 '1315',
 '1314',
 '1313',
 '1312',
 '1311',
 '1310',
 '1319',
 '1318',
 '1010',
 '1011',
 '319',
 '318',
 '313',
 '312',
 '311',
 '310',
 '317',
 '316',
 '315',
 '314',
 '1335',
 '1334',
 '1337',
 '1336',
 '1331',
 '1330',
 '1333',
 '1332',
 '630',
 '631',
 '1521',
 '964',
 '965',
 '966',
 '967',
 '960',
 '961',
 '962',
 '963',
 '968',
 '969',
 '635',
 '1241',
 '878',
 '1240',
 '876',
 '877',
 '874',
 '875',
 '872',
 '1243',
 '870',
 '871',
 '1242',
 '9',
 '1245',
 '1244',
 '890',
 '891',
 '892',
 '893',
 '894',
 '1247',
 '896',
 '897',
 '898',
 '899',
 '646',
 '1249',
 '648',
 '768',
 ...]

In [21]:
"bin/FaceLandmarkImg -q -multi-view 1 -wild -gaze -fdir ../../Sites/360-video-project/vrview/examples/orientations/video-frames/nocuts/ -ofdir ../../Sites/360-video-project/vrview/examples/orientations/analysis/face-tracking/nocuts/".split()


Out[21]:
['bin/FaceLandmarkImg',
 '-q',
 '-multi-view',
 '1',
 '-wild',
 '-gaze',
 '-fdir',
 '../../Sites/360-video-project/vrview/examples/orientations/video-frames/nocuts/',
 '-ofdir',
 '../../Sites/360-video-project/vrview/examples/orientations/analysis/face-tracking/nocuts/']

In [ ]:
many