0

I am able to write the cv2.aruco.Dictionary data to a YAML file on disk. But im unable to recreate this object using this yaml file. Currently im writing the aruco dictionary to disk like this.

aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_100)
file_storage = cv2.FileStorage(str(file_path), cv2.FILE_STORAGE_WRITE)
aruco_dict.writeDictionary(file_storage)
file_storage.release()

Doing this creates me a file that looks like this

%YAML:1.0
---
nmarkers: 100
markersize: 4
maxCorrectionBits: 1
marker_0: "1011010100110010"
marker_1: "0000111110011010"
marker_2: "0011001100101101"
marker_3: "1001100101000110"
marker_4: "0101010010011110"
marker_5: "0111100111001101"
marker_6: "1001111000101110"
marker_7: "1100010011110010"
marker_8: "1111111011011010"
marker_9: "1100111101010110"
marker_10: "1111100110010001"
...

I have tried a lot of different ways to load this in. Here is my current attempt

file_storage = cv2.FileStorage(str(file_path), cv2.FILE_STORAGE_READ)
file_node = file_storage.getFirstTopLevelNode()
dictionary = cv2.aruco.Dictionary.readDictionary(file_node)
file_storage.release()

here i get this error

    dictionary = cv2.aruco.Dictionary.readDictionary(file_node)
TypeError: descriptor 'readDictionary' for 'cv2.aruco.Dictionary' objects doesn't apply to a 'cv2.FileNode' object

Im running opencv 4.6.0 https://docs.opencv.org/4.6.0/d5/d0b/classcv_1_1aruco_1_1Dictionary.html#a69ec5ae547b01be07b7ce8c437ad1db4

colyton
  • 53
  • 6

1 Answers1

0

I have been mucking around with this. And it looks like I can get around this by doing this for now. I still would like to use the read and write from opencv tho


aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_100)

with open(file_path, "w") as FILE:
    FILE.write(json.dumps({
        "marker_size" : aruco_dict.markerSize,
        "bytes_list": aruco_dict.bytesList.tolist(),
        "max_corr": aruco_dict.maxCorrectionBits,
    }, indent=4))


with open(file_path, "r") as FILE:
    data = json.loads(FILE.read())
    loaded_bytes = np.array(data["bytes_list"], dtype=np.uint8)
    loaded_dict = cv2.aruco.Dictionary(
        len(loaded_bytes),
        data["marker_size"],
        data["max_corr"]
    )
    loaded_dict.bytesList = loaded_bytes

colyton
  • 53
  • 6