text: import yaml
  import numpy as np
  
  # A yaml constructor is for loading from a yaml node.
  # This is taken from @misha 's answer: http://stackoverflow.com/a/15942429
  def opencv_matrix_constructor(loader, node):
      mapping = loader.construct_mapping(node, deep=True)
      mat = np.array(mapping["data"])
      if mapping["cols"] > 1:
          mat.resize(mapping["rows"], mapping["cols"])
      else:
          mat.resize(mapping["rows"], )
      return mat
  
  
  yaml.add_constructor(u"tag:yaml.org,2002:opencv-matrix", opencv_matrix_constructor)
  
  
  # A yaml representer is for dumping structs into a yaml node.
  # So for an opencv_matrix type (to be compatible with c++'s FileStorage) we save the rows, cols, type and flattened-data
  def opencv_matrix_representer(dumper, mat):
      if mat.ndim > 1:
          mapping = {'rows': mat.shape[0], 'cols': mat.shape[1], 'dt': 'd', 'data': mat.reshape(-1).tolist()}
      else:
          mapping = {'rows': mat.shape[0], 'cols': 1, 'dt': 'd', 'data': mat.tolist()}
      return dumper.represent_mapping(u"tag:yaml.org,2002:opencv-matrix", mapping)
  
  
  yaml.add_representer(np.ndarray, opencv_matrix_representer)
  