# Loading instances of Algorithm - derived classes

**URL:** <https://forum.opencv.org/t/loading-instances-of-algorithm-derived-classes/9763>\
**Category:** Python\
**Created:** [August 8, 2022, 9:57am UTC](https://forum.opencv.org/t/loading-instances-of-algorithm-derived-classes/9763 "2022-08-08T09:57:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SeanInko](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@SeanInko](https://forum.opencv.org/u/SeanInko)\
**Post date:** [August 8, 2022, 9:57am UTC](https://forum.opencv.org/t/loading-instances-of-algorithm-derived-classes/9763/1 "2022-08-08T09:57:37Z")

</div>

I am trying to save and load instances of the BackgroundSubtractorMOG2 class in Python. There is no problem with saving it, using the ‘save’ method derived from the Algorithm class. But I came across problems with loading it. It seems the Algorithm’s ‘load’ method does not work in python. I tried using read/write methods instead but without success. Is there any possibility to save/load instances of Algorithm derived classes in Python?  
I really appreciate any help you can provide.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 8, 2022, 11:49am UTC](https://forum.opencv.org/t/loading-instances-of-algorithm-derived-classes/9763/2 "2022-08-08T11:49:53Z")

</div>

> [@SeanInko](#):
>
> It seems the Algorithm’s ‘load’ method does not work in python.

indeed, that is not implemented.

> I tried using read/write methods instead

you should be able to do that (read it into a FileStorage, get the root() FileNode and read the bgmog data from there)

however, be aware, it only serializes the params (what you can get/set there), NOT the internal state, so if you start a new instance, it still needs to process a few thousand images to work properly, you cannot save that !

---

<div class="post-metadata">

**Author:** ![SeanInko](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@SeanInko](https://forum.opencv.org/u/SeanInko)\
**Post date:** [August 9, 2022, 9:51am UTC](https://forum.opencv.org/t/loading-instances-of-algorithm-derived-classes/9763/3 "2022-08-09T09:51:23Z")

</div>

Thank you very much for the explanation. Everything is clear now.  
It seems that using write/read is not what I really need. Anyway, I can rewrite the code in C++ (fortunately it is an option for me).
