def opencv_matrix_representer(dumper, mat):
    mapping = {'rows': mat.shape[0], 'cols': mat.shape[1], 'dt': 'd',
        'data': mat.reshape(-1).tolist()}
return dumper.represent_mapping('tag:yaml.org,2002:opencv-matrix', mapping)
yaml.add_representer(np.ndarray, opencv_matrix_representer)
with open('output.yaml', 'w') as f:
    yaml.dump({'a matrix': np.zeros((10, 10)), 'another_one': np.zeros((2, 
        4))}, f)
with open('output.yaml', 'r') as f:
    pass
FileStorage for OpenCV Python API