I was trying to work with OCRHMMDecoder class to perform some OCR by following the example 'webcam_demo' provided in 'modules/text/samples/'.
I tried to load the files 'OCRHMM_knn_model_data.xml' and 'OCRHMM_transition_table.xml' as shown in the example. For loading the transition probabilities, the code uses the FileStorage object to read the transition table xml and store the 'transition_probabilities' into a variable 'transition_p' by using the code:
fs["transition_probabilities"] >> transition_p
I tried to change this to a python code by :
transition_p = fs['transition_probabilities']
But this doesn't work, because in C++, if a function is defined within a class as operator, then it enables the subscripting of that class object. but in python it is has to __getitem__() (https://docs.python.org/3.5/library/operator.html).
I used the help command in python to check what functions were available in the cv2.FileStorage object (http://docs.opencv.org/3.1.0/da/d56/classcv_1_1FileStorage.html) and to get a specific node a function 'operator[]' is defined.
Here, is the output of the help function:
Help on FileStorage object:
class FileStorage(builtins.object)
| Methods defined here:
|
| __new__(_args, *_kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __repr__(self, /)
| Return repr(self).
|
| getFirstTopLevelNode(...)
| getFirstTopLevelNode() -> retval
|
| isOpened(...)
| isOpened() -> retval
|
| open(...)
| open(filename, flags[, encoding]) -> retval
|
| operator
| operator -> retval
|
| release(...)
| release() -> None
|
| releaseAndGetString(...)
| releaseAndGetString() -> retval
|
| root(...)
| root([, streamidx]) -> retval
(END)Steps to reproduce
Here is my code
import cv2
import numpy as np
if __name__ == '__main__':
# print(cv2.__version__)
clas = cv2.text.loadOCRHMMClassifierNM('OCRHMM_knn_model_data.xml')
tt = cv2.FileStorage('OCRHMM_transition_table.xml',cv2.FILE_STORAGE_READ)
transition_p = tt.['transition_probabilities']
#trans = tt.root(1)
print(help(tt))
tt.release()
emmission_p = np.eye(62,dtype=np.float64)
vocab = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
OCR = cv2.text.OCRHMMDecoder_create(clas, vocab, transition_p, emmission_p)
tt.getNode("transition_probabilities").mat() (requires opencv master)
There is no method getNode('string') in my OpenCV installation. Do I need to rebuild using the master branch of this repo.?
yes
if the node is a single integer, how to get it?
Let's say:
tt.getNode("whatever") is a single integer, and I do NOT expect it to be converted into a mat(), how to do that?
Thank you
tt.getNode("whatever").real()
Reading list like this numbers: [0, 4, 6, 3, 0, 8] requires:
numbers = fs.getNode('numbers')
num_list = []
for i in range(0, numbers.size()):
num_list.append(numbers.at(i).real())
print(num_list)
Hi, I am just a beginner of openCV 3.1.0. I am using Spyder with python 3.5. I am having this problem 'cv2.FileStorage' object has no attribute 'operator'. And it does not have the attribute of 'getnode' either in my CV installation. However, when I tried help(fs), there is operator attribute. Could anyone help? Many thanks. Besides, where could I download the master branch of openCV?
Most helpful comment
tt.getNode("transition_probabilities").mat()(requires opencv master)