diff --git a/compute_features.py b/compute_features.py index c7ed79e057555bdf3a88c4dfa5a38fd3dfdf9c4a..72894b8999e8c844082b81efccfd0129ee34b231 100644 --- a/compute_features.py +++ b/compute_features.py @@ -62,97 +62,99 @@ def process_compute(data_name, camera, Nb_camera, Nb_world, list_categories, occ for categories in list_categories: - if len(catergories_occ_array[categories]) == 1 : + if categories in catergories_occ_array.keys(): - meta = {} + if len(catergories_occ_array[categories]) == 1 : - if categories in list_count_categories.keys(): - list_count_categories[categories] += 1 - else: - list_count_categories[categories] = 1 + meta = {} - meta['id_generated'] = list_count_categories[categories] - meta['id_original'] = p - meta['id_category'] = catergories_label_to_id[categories] - meta['id_instance'] = catergories_occ_array[categories][0] - meta['id_dataset'] = 1 - meta['world'] = i - meta['camera'] = f"grabber_{j}" - meta['occlusion'] = occ_target - meta['Nb_instance_category'] = 1 - - - if not os.path.isfile(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json'): - with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json', mode='w') as f: - feeds = {} - feeds[meta['id_generated']]=meta - f.write(json.dumps(feeds, indent=2)) - else: - with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json') as feedsjson: - feeds = json.load(feedsjson) - feeds[meta['id_generated']]=meta - with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json', mode='w') as f: - f.write(json.dumps(feeds, indent=4)) - - # with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json', "a") as meta_file: - # json.dump(meta, meta_file, indent=4) - - for k in range(len(data_3D_pose)): - - if data_3D_pose[k]['id'] == catergories_occ_array[categories][0]: - rpy = data_3D_pose[k]['pose']['rpy'] - rot = convert2(rpy) - R_exp = transformation @ rot - R_exp = np.array(R_exp) - - xyz = data_3D_pose[k]['pose']['xyz'] - T_exp = transformation @ xyz - T_exp = np.array(T_exp) - num_arr = np.c_[R_exp, T_exp[0]] - np.save(f'{data_name}/Generated/Pose_transformed/{categories}/{p}.npy', num_arr) # save + if categories in list_count_categories.keys(): + list_count_categories[categories] += 1 else: - continue - - if data_Bbox_2d[k]['id'] == catergories_occ_array[categories][0]: - bbox = bbox_2d(data_Bbox_2d[k]) - np.savetxt(f'{data_name}/Generated/Bbox/{categories}/{p}.txt', np.array(bbox).reshape((1, 4))) # save + list_count_categories[categories] = 1 + + meta['id_generated'] = list_count_categories[categories] + meta['id_original'] = p + meta['id_category'] = catergories_label_to_id[categories] + meta['id_instance'] = catergories_occ_array[categories][0] + meta['id_dataset'] = 1 + meta['world'] = i + meta['camera'] = f"grabber_{j}" + meta['occlusion'] = occ_target + meta['Nb_instance_category'] = 1 + + + if not os.path.isfile(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json'): + with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json', mode='w') as f: + feeds = {} + feeds[meta['id_generated']]=meta + f.write(json.dumps(feeds, indent=2)) else: - continue - - id = catergories_occ_array[categories][0] - img = cv2.imread(f"{data_name}/Instance_Segmentation/{p}.png", cv2.IMREAD_UNCHANGED) # plt.imread(path) - - instance_img = instance(img, id) - cv2.imwrite(f"{data_name}/Generated/Instance_Mask/{categories}/{p}.png", 255*instance_img) - - - img = image.imread(f"{data_name}/RGB/{p}.png") - - np.set_printoptions(precision=15) - pose = np.load(f'{data_name}/Generated/Pose_transformed/{categories}/{p}.npy') - #print(pose) - R_exp = pose[0:3, 0:3] - tVec = pose[0:3, 3] - - #print(tVec) - # camera = np.matrix([[1386.4138492513919, 0.0, 960.5], - # [0.0, 1386.4138492513919, 540.5], - # [0.0, 0.0, 1.0]]) - - - fps_points = np.loadtxt(f'{data_name}/Generated/FPS/{categories}_fps_3d.txt') - # process(pcd_bbox, pcd, R_exp, tVec, camera, img) - points = process2(fps_points, R_exp, tVec, camera, img, vis) - out = np.zeros((1, 401)) - - out[0] = catergories_occ_array[categories] #obj_id #len have to be 1 !! - ind = 1 - for point in points: - out[0][ind] = point[0][0] / img.shape[1] - out[0][ind + 1] = point[0][1] / img.shape[0] - ind += 2 - np.savetxt(f'{data_name}/Generated/FPS/{categories}/{p}.txt', out) - #print("stop") + with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json') as feedsjson: + feeds = json.load(feedsjson) + feeds[meta['id_generated']]=meta + with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json', mode='w') as f: + f.write(json.dumps(feeds, indent=4)) + + # with open(f'{data_name}/Generated/Meta_Gen/{categories}/{categories}.json', "a") as meta_file: + # json.dump(meta, meta_file, indent=4) + + for k in range(len(data_3D_pose)): + + if data_3D_pose[k]['id'] == catergories_occ_array[categories][0]: + rpy = data_3D_pose[k]['pose']['rpy'] + rot = convert2(rpy) + R_exp = transformation @ rot + R_exp = np.array(R_exp) + + xyz = data_3D_pose[k]['pose']['xyz'] + T_exp = transformation @ xyz + T_exp = np.array(T_exp) + num_arr = np.c_[R_exp, T_exp[0]] + np.save(f'{data_name}/Generated/Pose_transformed/{categories}/{p}.npy', num_arr) # save + else: + continue + + if data_Bbox_2d[k]['id'] == catergories_occ_array[categories][0]: + bbox = bbox_2d(data_Bbox_2d[k]) + np.savetxt(f'{data_name}/Generated/Bbox/{categories}/{p}.txt', np.array(bbox).reshape((1, 4))) # save + else: + continue + + id = catergories_occ_array[categories][0] + img = cv2.imread(f"{data_name}/Instance_Segmentation/{p}.png", cv2.IMREAD_UNCHANGED) # plt.imread(path) + + instance_img = instance(img, id) + cv2.imwrite(f"{data_name}/Generated/Instance_Mask/{categories}/{p}.png", 255*instance_img) + + + img = image.imread(f"{data_name}/RGB/{p}.png") + + np.set_printoptions(precision=15) + pose = np.load(f'{data_name}/Generated/Pose_transformed/{categories}/{p}.npy') + #print(pose) + R_exp = pose[0:3, 0:3] + tVec = pose[0:3, 3] + + #print(tVec) + # camera = np.matrix([[1386.4138492513919, 0.0, 960.5], + # [0.0, 1386.4138492513919, 540.5], + # [0.0, 0.0, 1.0]]) + + + fps_points = np.loadtxt(f'{data_name}/Generated/FPS/{categories}_fps_3d.txt') + # process(pcd_bbox, pcd, R_exp, tVec, camera, img) + points = process2(fps_points, R_exp, tVec, camera, img, vis) + out = np.zeros((1, 401)) + + out[0] = catergories_occ_array[categories] #obj_id #len have to be 1 !! + ind = 1 + for point in points: + out[0][ind] = point[0][0] / img.shape[1] + out[0][ind + 1] = point[0][1] / img.shape[0] + ind += 2 + np.savetxt(f'{data_name}/Generated/FPS/{categories}/{p}.txt', out) + #print("stop") print(list_count_categories)