Untitled

 avatar
unknown
python
4 years ago
6.6 kB
4
Indexable
LOCAL = True
couvette = False
results_to_analyse = []
import time
import random
RUN_GCP = False
pending = []
experiment_name = "TEST"  
today = time.strftime("%Y-%m-%d")
counter = 500000
custom_pupil = True
bridge_detection = True
slipp = None
pose = None
position = 0
TILT_TH = 3
NOSE_TH = 1
SLIP_0 = .1
SLIP_1 = .2
DISTANCE_TH_ACETATE = 6
NOSEPAD_NOSE_TH = 5
NOSEBRIDGE_NOSE_TH = 1.5
custom_pupil = True
poses = [-2, -1, 0, 1, 2]
poses = [None]
for pose in tqdm(poses):
    vid_counter = 0
    custom_prms = {'slipping': slipp, 
                   'pose': pose,
                   'slipping_info': {'NOSE_TH': NOSE_TH, 
                                     'SLIP_0': SLIP_0, 
                                     'SLIP_1': SLIP_1,
                                     'TILT_UB': TILT_TH,
                                     'NOSEPAD_NOSE_TH': NOSEPAD_NOSE_TH,
                                     'DISTANCE_TH_ACETATE': DISTANCE_TH_ACETATE}, 
                  'freedom_map': freedom_map}
    for vid in vid_list[7:8]:
        print('VideoID-->',vid)
        print('PERCENTAGE-->',np.round(vid_counter/len(vid_list[:])*100))
        vid_counter+=1
        if vid in ['d1f4cff8-bdc0-48c6-a00d-199d1120faeb']:  # Non ci sono le local
            continue
        if vid in ['b8f8c9b6-280e-4179-9b7d-b2ee95901cdb']:  # Fallisce il fitting in locale
            continue
        if vid in ['f0d0c35b-a66b-484f-bffe-499df3a9f13c']:  # Fallisce il fitting in locale
            continue    
        if vid in ['3ea8fe52-453f-4a5b-a4c0-48fdf6584060']:
            continue
        with open('metric_meshes/3d_eyes/%s.json' % vid) as f:
            eyes_3d = json.load(f)
        p_l = eyes_3d['frame7_beta_left_mm']
        p_r = eyes_3d['frame7_beta_right_mm']
        p_l[1] = -p_l[1]
        p_r[1] = -p_r[1]
        p_l[2] = -p_l[2]
        p_r[2] = -p_r[2]

        if custom_pupil:
            custom_prms['p_l'] = p_l
            custom_prms['p_r'] = p_r
            pupils = [p_l, p_r]
        else:
            pupils = None

        face_mesh_mother, face_pc_mother, pupils = head_load(vid, local=LOCAL, custom_pupils=pupils)
        for upc in list(df_kpis2D[df_kpis2D['video_id'] == vid]['upc'][:]):
            print('UPC-->',upc)
            if upc in (
                    ['8056597100090', '8056597108072_16'] + ['7895653228924', '713132838143', '8053672914887']):
                print("Continue", upc)
                continue  # Last 3 are semi-rim

            try:
                material = (
                    df_gt.loc[(df_gt['video_id'] == vid) & (df_gt['upc'] == upc), 'Material Front_x'].values[0])
            except Exception as e:
                print(e)
                print("Cannot find material ", vid, upc)
                continue

            if material != 'Acetate':
                print("skipping")
                continue
            # print(upc)
            to_analyse = {'upc': upc, 'vid': vid, 'slipp': slipp, 'pose':pose, 'material': material,
                          'slipping_info': custom_prms['slipping_info'],
                          'freedom_map': custom_prms['freedom_map'],
                          'experiment_name': experiment_name, 'bridge_detection': bridge_detection,
                          'date': today}

            if upc in upc_couvette:
                couvette = True
            else:
                couvette = False
            face_mesh = face_mesh_mother.copy()
            if bridge_detection:
                centroid_nose = nasion_finder_with_centroid(face_mesh_mother, nose_idxs=[NOSE_MID_IDXs])
                custom_prms['custom_LND_bridge'] = centroid_nose
            face_pc = deepcopy(face_pc_mother)
            for i in range(0, 4):
                try:
                    results, glasses_mesh, bound_left_mother, bound_right_mother = docking_dispatch(vid, 
                                                                                                    upc,
                                                                                                    params,
                                                                                                    custom_prms=dict(
                                                                                                    custom_prms),
                                                                                                    local=LOCAL,
                                                                                                    gcp=RUN_GCP,
                                                                                                    counter=counter)
                    my_res = {'to_analyse': to_analyse,
                              'bound_left_mother': bound_left_mother,
                              'bound_right_mother': bound_right_mother,
                              'df_gt': df_gt,
                              'vid': vid,
                              'upc': upc,
                              'couvette': couvette,
                              'df_kpis2D': df_kpis2D,
                              'face_mesh': face_mesh,
                              'face_pc': face_pc,
                              'results': results,
                              'counter': str(counter),
                              'TO_PRINT': not RUN_GCP,
                              'PICS': PICS,
                              'custom_pupils': custom_pupil,
                              'lnd': lnd,
                              'pupils': pupils,
                              'nasion': centroid_nose,
                              'glasses_mesh': glasses_mesh,
                              'experiment_name': experiment_name}
                    counter += 1

                    break
                except Exception as e:
                    print("EXCEPTION IN FOR LOOP")
                    print(i)
                    print(str(e))
                    time.sleep(1)
                if i == 3:
                    raise e

            if RUN_GCP:
                if random.random() < 0.01:
                    print("Running")
                time.sleep(0.05)
                # while list_queues() > 20:
                #    time.sleep(0.01)

                pending.append(my_res)
            else:
                check_all([my_res], results_to_analyse)  

    # break
# break
# break
if RUN_GCP:
    while list_queues() != 0:
        time.sleep(60)
    # All done
    print("Done, parsing results. len: %d" % len(pending))
    check_all(pending, results_to_analyse)

pd.DataFrame(results_to_analyse).to_csv("%s.csv" % experiment_name, index=False)
Editor is loading...