shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Read CascadeClassifier from string in memory (instead of file) #1591

Open alexsosa2000 opened 11 months ago

alexsosa2000 commented 11 months ago

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?

shimat commented 11 months ago

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
alexsosa2000 commented 11 months ago

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?