복붙노트

[PYTHON] OpenCV Python API 용 FileStorage

PYTHON

OpenCV Python API 용 FileStorage

현재 OpenCV C ++ API를 사용하여 행렬 XML / YAML을 저장하기 위해 FileStorage 클래스를 사용하고 있습니다.

그러나 XML / YAML 파일을 읽는 Python 스크립트를 작성해야합니다.

OpenCV C ++ API에 의해 생성 된 XML / YAML 파일을 읽을 수있는 기존 OpenCV Python API를 찾고 있습니다.

해결법

  1. ==============================

    1.PyYAML을 사용하여 YAML 파일을 구문 분석 할 수 있습니다.

    PyYAML을 사용하여 YAML 파일을 구문 분석 할 수 있습니다.

    PyYAML은 OpenCV 데이터 형식을 이해하지 못하기 때문에로드하려는 각 OpenCV 데이터 형식에 대해 생성자를 지정해야합니다. 예 :

    import yaml
    def opencv_matrix(loader, node):
        mapping = loader.construct_mapping(node, deep=True)
        mat = np.array(mapping["data"])
        mat.resize(mapping["rows"], mapping["cols"])
        return mat
    yaml.add_constructor(u"tag:yaml.org,2002:opencv-matrix", opencv_matrix)
    

    일단 그렇게하면 yaml 파일을 로딩하는 것은 간단합니다.

    with open(file_name) as fin:
        result = yaml.load(fin.read())
    

    결과는 dict 일 것이며, 여기서 키는 YAML에 저장 한 이름입니다.

  2. ==============================

    2.@ misha의 응답 외에도 OpenCV YAML은 Python과 다소 호환되지 않습니다.

    @ misha의 응답 외에도 OpenCV YAML은 Python과 다소 호환되지 않습니다.

    비호 환성에 대한 몇 가지 이유는 다음과 같습니다.

    다음 함수는 다음을 제공합니다.

    import yaml
    import re
    def readYAMLFile(fileName):
        ret = {}
        skip_lines=1    # Skip the first line which says "%YAML:1.0". Or replace it with "%YAML 1.0"
        with open(scoreFileName) as fin:
            for i in range(skip_lines):
                fin.readline()
            yamlFileOut = fin.read()
            myRe = re.compile(r":([^ ])")   # Add space after ":", if it doesn't exist. Python yaml requirement
            yamlFileOut = myRe.sub(r': \1', yamlFileOut)
            ret = yaml.load(yamlFileOut)
        return ret
    
    outDict = readYAMLFile("file.yaml")
    

    참고 : 위의 응답은 yaml에만 적용됩니다. XML은 문제의 자신의 몫을 가지고 있습니다. 내가 완전히 탐구하지 않은 것입니다.

  3. ==============================

    3.OpenCV 3.2에서 제공되는 FileStorage 기능을 사용하여이 기능을 성공에 사용했습니다.

    OpenCV 3.2에서 제공되는 FileStorage 기능을 사용하여이 기능을 성공에 사용했습니다.

    import cv2
    fs = cv2.FileStorage("calibration.xml", cv2.FILE_STORAGE_READ)
    fn = fs.getNode("Camera_Matrix")
    print (fn.mat())
    
  4. ==============================

    4.파이썬에서 FileStorage 호환 YAML을 읽고 쓰는 작은 스 니펫을 작성했습니다.

    파이썬에서 FileStorage 호환 YAML을 읽고 쓰는 작은 스 니펫을 작성했습니다.

    # 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"])
        mat.resize(mapping["rows"], mapping["cols"])
        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):
        mapping = {'rows': mat.shape[0], 'cols': mat.shape[1], 'dt': 'd', 'data': mat.reshape(-1).tolist()}
        return dumper.represent_mapping(u"tag:yaml.org,2002:opencv-matrix", mapping)
    yaml.add_representer(np.ndarray, opencv_matrix_representer)
    
    #examples 
    
    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:
        print yaml.load(f)
    
  5. ==============================

    5.@Roy_Shilkrot의 이전 대답을 향상시키기 위해 행렬뿐 아니라 numpy 벡터에 대한 지원을 추가했습니다.

    @Roy_Shilkrot의 이전 대답을 향상시키기 위해 행렬뿐 아니라 numpy 벡터에 대한 지원을 추가했습니다.

    # 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)
    

    예:

    with open('output.yaml', 'w') as f:
        yaml.dump({"a matrix": np.zeros((10,10)), "another_one": np.zeros((5,))}, f)
    
    with open('output.yaml', 'r') as f:
        print yaml.load(f)
    

    산출:

    a matrix: !!opencv-matrix
      cols: 10
      data: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0]
      dt: d
      rows: 10
    another_one: !!opencv-matrix
      cols: 1
      data: [0.0, 0.0, 0.0, 0.0, 0.0]
      dt: d
      rows: 5
    

    비록 내가 행, 열, dt, 데이터의 순서를 제어 할 수는 없지만.

  6. from https://stackoverflow.com/questions/11141336/filestorage-for-opencv-python-api by cc-by-sa and MIT license