opencvsharp icon indicating copy to clipboard operation
opencvsharp copied to clipboard

Read CascadeClassifier from string in memory (instead of file)

Open alexsosa2000 opened this issue 2 years ago • 2 comments

Summary of your issue

I'm trying to do face detection in Blazor WebAssembly by using CascadeClassifier. The problem is it seems to only be able to initialize from a file (which can't be done in the browser). Therefore, I'm trying to figure out how to initialize CascadeClassifier from a string in memory (hardcoded XML).

I've seen other examples of people doing this like (C++ example):

CascadeClassifier cc;
FileStorage fs( the_whole_cascade_in_a_string, FileStorage::READ | FileStorage::MEMORY);
cc.read(fs.getFirstTopLevelNode());

Ref: https://answers.opencv.org/question/27970/cascadeclassifierload-from-memory/

The problem is there's no CascadeClassifer.Read() method that allows me to pass in FileStorage.GetFirstTopLevelNode().

Any idea on how to be able to initialize the CascadeClassifier from a string in memory?

alexsosa2000 avatar Jul 10 '23 17:07 alexsosa2000

Please try the following code:

using var fs = new FileStorage(yaml_string, FileStorage.Modes.Read | FileStorage.Modes.Memory);
using var node = fs.GetFirstTopLevelNode();

using var cascade = new CascadeClassifier();
bool ret = cascade.Read(node); // should return true

shimat avatar Jul 11 '23 04:07 shimat

Thank you @shimat. It turned out I needed to use v4.6 to get the Read() method.

Unfortunately cascade.Read() is returning false. I'm using this haarcascade_frontalface_default.xml as the string.

Any idea how to get it working with this model?

Also, in your expert opinion, would it be better to use DNN in Blazor WebAssembly? If so, is that able to read from an in-memory file and model?

alexsosa2000 avatar Jul 11 '23 14:07 alexsosa2000