cureos / accord

Machine learning, computer vision, statistics and general scientific computing for .NET
http://code.google.com/p/accord/
Other
12 stars 2 forks source link

Windows Phone 8 HaarCascadeDetector #4

Closed DRybochkin closed 10 years ago

DRybochkin commented 10 years ago

Hi Anders,

I have the exception "Handle is not pinned" at last row of the next code

Exception

at System.Runtime.InteropServices.GCHandle.AddrOfPinnedObject() at Accord.Imaging.IntegralImage2..ctor(Int32 width, Int32 height, Boolean computeTilted) at Accord.Imaging.IntegralImage2.FromBitmap(UnmanagedImage image, Int32 channel, Boolean computeTilted) at Accord.Vision.Detection.HaarObjectDetector.ProcessFrame(UnmanagedImage image)

Code:

System.IO.Stream stream = System.IO.File.OpenRead("Resources/haarcascade_frontalface_default.xml");
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.XmlResolver = null;
settings.DtdProcessing = System.Xml.DtdProcessing.Parse;
HaarCascade cascade = HaarCascade.FromXml(stream);

HaarObjectDetector detector = new HaarObjectDetector(cascade);
detector.SearchMode = ObjectDetectorSearchMode.Default;
detector.ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller;
detector.ScalingFactor = 1.5f;
detector.UseParallelProcessing = false;
System.Drawing.Rectangle[] rects = detector.ProcessFrame(img2);

Thanks in advance Dmitry

cureos commented 10 years ago

@DRybochkin I am looking into this issue right now, although I am currently struggling with a parsing problem... (I can workaround the parsing problem for now, but I want to flag this separate issue as well.)

@cesarsouza Have you noticed that the Haar Cascade XML files in Open CV has recently changed their format? Please see this commit.

cureos commented 10 years ago

I have now debugged the code, and the GCHandle is successfully allocated with type Pinned, but then the AddrOfPinnedObject method still interprets the handle as not Pinned?

I need to investigate further whether this is a bug in the PCL/Phone adaptation of Accord, or whether it is an issue with Windows Phone's own class libraries. Stay tuned :-)

DRybochkin commented 10 years ago

@cureos this code works on pure WP8, but don't work in PCL lib

        this.nSumImage = new int[nHeight, nWidth];
        this.nSumHandle = GCHandle.Alloc(nSumImage, GCHandleType.Pinned);
        IntPtr ip = nSumHandle.AddrOfPinnedObject();
DRybochkin commented 10 years ago

I think it somehow connected with redefining System.Runtime.InteropService

cureos commented 10 years ago

Yes, this is most likely the issue. I am currently searching the PCL version for inconsistencies, I'll get back as soon as I know more.

DRybochkin commented 10 years ago

Change GCHandleType.cs in Portable.System on next

public enum GCHandleType
{
    Weak = 0,
    WeakTrackResurrection = 1,
    Normal = 2,
    Pinned = 3,
}

and it will work

cureos commented 10 years ago

That was exactly what I was testing when I saw your latest post :-)

I will provide the update and new binaries as soon as possible. Thanks for all your help with reporting and investigating this.

Best regards, Anders

DRybochkin commented 10 years ago

You are welcome! Did you know the library in C# automatic generation haarcascade.xml from positive & negative images?

cureos commented 10 years ago

Now I have added the correction to the GCHandleType enum in the aforge repository, and I have also replaced the Accord binaries for version 2.12.0.1 with binaries built with the Pinned correction in place (I hope this should be enough).

Again, a big thank you for pointing out the issue and helping me solve it! Anders

PS. I was not aware of the XML generation functionality. I have not yet explored Accord in all the details. But it's good to know. Thanks!