Esempio n. 1
0
        public void Evaluate(int SpreadMax)
        {
            this.FOutValid.SliceCount  = 1;
            this.FStreamOut.SliceCount = 1;

            if (this.FRead[0])
            {
                this.FTextureIn.Sync();
                if (this.FTextureIn.IsConnected)
                {
                    if (this.RenderRequest != null)
                    {
                        this.RenderRequest(this, this.FHost);
                    }

                    if (this.AssignedContext == null)
                    {
                        this.FOutValid.SliceCount = 0; return;
                    }

                    var context = this.AssignedContext;



                    if (this.FTextureIn[0].Contains(this.AssignedContext))
                    {
                        var texture = this.FTextureIn[0][context];
                        var staging = new DX11StagingTexture2D(context, texture.Width, texture.Height, texture.Format);
                        staging.CopyFrom(texture);

                        var db = staging.LockForRead();

                        strideOut[0] = db.RowPitch;

                        if (this.lastStream != null)
                        {
                            if (this.lastStream.Length != db.Data.Length)
                            {
                                this.lastStream.Dispose();
                                this.lastStream = null;
                            }
                        }

                        if (this.lastStream == null)
                        {
                            this.lastStream = new DataStream((int)db.Data.Length, true, true);
                        }

                        this.lastStream.Position = 0;

                        int pixelStride = formatHelper.GetSize(texture.Format);
                        int dstStride   = pixelStride * texture.Width;

                        if (FApplyStride[0])
                        {
                            if (this.binter.Length != db.RowPitch)
                            {
                                this.binter = new byte[db.RowPitch];
                            }

                            byte *destPointer = (byte *)this.lastStream.DataPointer.ToPointer();
                            byte *srcPointer  = (byte *)db.Data.DataPointer.ToPointer();

                            for (int i = 0; i < texture.Height; i++)
                            {
                                memcpy(destPointer, srcPointer, dstStride);
                                destPointer += dstStride;
                                srcPointer  += db.RowPitch;
                            }
                        }
                        else
                        {
                            db.Data.CopyTo(this.lastStream);
                        }
                        this.lastStream.Position = 0;

                        staging.UnLock();
                        staging.Dispose();

                        FStreamOut[0] = this.lastStream;
                        FStreamOut.Flush(true);
                        this.FOutValid[0] = true;
                    }
                    else
                    {
                        this.FOutValid[0] = false;
                    }
                }
                else
                {
                    this.FOutValid[0] = false;
                }
            }
        }
Esempio n. 2
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FTextureIn.PluginIO.IsConnected && FEnabled[0] == true)
            {
                if (this.RenderRequest != null)
                {
                    this.RenderRequest(this, this.FHost);
                }
                if (this.AssignedContext == null)
                {
                    this.SetNull(); return;
                }
                DX11RenderContext context = this.AssignedContext;

                try
                {
                    if (this.FTextureIn[0].Contains(context))
                    {
                        DX11StagingTexture2D sTex = new DX11StagingTexture2D(context, this.FTextureIn[0][context].Width, this.FTextureIn[0][context].Height, this.FTextureIn[0][context].Format);
                        sTex.CopyFrom(FTextureIn[0][context]);

                        DataBox db = sTex.LockForRead();
                        FOutput.SliceCount = Convert.ToInt32(sTex.Width * sTex.Height);

                        // our array we want to fill with texture data
                        arr = new int[sTex.Height, sTex.Width];

                        // we need these pitch values to correctly fill our array
                        int pitchedWidth = db.RowPitch / 4;
                        int pitchDiff    = pitchedWidth - sTex.Width;

                        // go through the texture data
                        while (db.Data.Position < db.Data.Length)
                        {
                            int arrPos = Convert.ToInt32(db.Data.Position) / 4; // this is the position in our array
                            int intCol = db.Data.Read <int>();                  // read the color

                            // the texture data comes from a pitched resource! so we have to be aware of longer row lengths!
                            int y = arrPos / pitchedWidth; // this is the actual rowNumber in our pitched texture
                                                           // now check if the current arrayPosition lies inside the actual texture width
                            if (arrPos < y * pitchedWidth + sTex.Width)
                            {
                                arrPos = arrPos - y * pitchDiff;                                        // we have to subtract rowNumber * pitchDiff to compensate the pitch
                                arr[(arrPos / sTex.Width) % sTex.Height, arrPos % sTex.Width] = intCol; // now we add the packed integer color
                            }
                        }

                        cols = sTex.Width;
                        rows = sTex.Height;

                        for (int j = 0; j < rows; j++)
                        {
                            for (int i = 0; i < cols; i++)
                            {
                                FOutput[cols * j + i] = arr[j, i];
                            }
                        }

                        // unlock and dispose the resource
                        sTex.UnLock();
                        sTex.Dispose();


                        // INIT LABELS
                        FLabels.SliceCount = Convert.ToInt32(sTex.Width * sTex.Height);
                        for (int i = 0; i < FLabels.SliceCount; i++)
                        {
                            FLabels[i] = 0;
                        }

                        // INIT COMPONENT COUNTER
                        this.FComponentsCount.SliceCount = 1;
                        this.objectCounts.Clear();

                        // DO THE COMPONENT LABELING
                        ConnectedComponentLabeling();

                        // OUTPUT THE LABELS
                        for (int i = 0; i < FLabels.SliceCount; i++)
                        {
                            if (FLabels[i] == -1)
                            {
                                FLabels[i] = 0;
                            }
                        }

                        // SET BIN SIZES
                        this.FComponentsBins.SliceCount = this.objectCounts.Count;
                        this.FCentroids.SliceCount      = this.objectCounts.Count * 2;

                        // fill binsize and centroid spreads
                        for (int i = 0; i < this.objectCounts.Count; i++)
                        {
                            // binsize
                            FComponentsBins[i] = this.objectCounts[i + 1];
                            // centroid XY
                            FCentroids[i * 2]     = (this.xMin[i + 1] + this.xMax[i + 1]) / 2;
                            FCentroids[i * 2 + 1] = (this.yMin[i + 1] + this.yMax[i + 1]) / 2;
                        }
                    }
                }
                catch (Exception ex)
                {
                    FLogger.Log(LogType.Error, ex.Message);
                }
            }
        }
Esempio n. 3
0
        public void Evaluate(int SpreadMax)
        {
            this.FError.SliceCount   = SpreadMax;
            FOutput.SliceCount       = SpreadMax;
            ImgCache.SliceCount      = SpreadMax;
            FOutput.Stream.IsChanged = false;

            if (this.FTextureIn.PluginIO.IsConnected)
            {
                if (this.RenderRequest != null)
                {
                    this.RenderRequest(this, this.FHost);
                }

                if (this.AssignedContext == null)
                {
                    this.FError.SliceCount = 0; return;
                }
                //Do NOT cache this, assignment done by the host
                FQuery[0] = this;
                BeginQuery?.Invoke(AssignedContext);

                for (int i = 0; i < SpreadMax; i++)
                {
                    if (this.FTextureIn[i].Contains(this.AssignedContext) && this.FEnabled[i])
                    {
                        try
                        {
                            if (FOutput[i] == null)
                            {
                                FOutput[i] = new FaceTrackerContext();
                            }

                            FOutput[i].Scale          = (float)FScale[i];
                            FOutput[i].MinNeighbors   = FMinNeighbours[i];
                            FOutput[i].MinObjectWidth = FMinObjWidth[i];
                            FOutput[i].MaxObjectWidth = FMaxObjWidth[i];
                            FOutput[i].DoLandmarks    = FDoLandmarks[i];

                            if (this.FTextureIn[i][this.AssignedContext].Format != Format.R8_UNorm)
                            {
                                FError[i] = "Texture is not R8_UNorm";
                                continue;
                            }

                            int w = this.FTextureIn[i][this.AssignedContext].Width;
                            int h = this.FTextureIn[i][this.AssignedContext].Height;
                            if (ImgCache[i] == null)
                            {
                                ImgCache[i] = new byte[w * h];
                            }
                            if (ImgCache[i].Length != w * h)
                            {
                                ImgCache[i] = new byte[w * h];
                            }
                            var texture = this.FTextureIn[0][AssignedContext];
                            var staging = new DX11StagingTexture2D(AssignedContext, texture.Width, texture.Height, texture.Format);

                            staging.CopyFrom(texture);
                            var db = staging.LockForRead();
                            db.Data.Read(ImgCache[i], 0, w * h);

                            staging.UnLock();
                            staging.Dispose();

                            switch (FEngine[i])
                            {
                            case FaceTrackerMode.Frontal:
                                FOutput[i].DetectFrontal(ImgCache[i], w, h);
                                break;

                            case FaceTrackerMode.FrontalSurveillance:
                                FOutput[i].DetectFrontalSurveillance(ImgCache[i], w, h);
                                break;

                            case FaceTrackerMode.MultiView:
                                FOutput[i].DetectMultiView(ImgCache[i], w, h);
                                break;

                            case FaceTrackerMode.MultiViewReinforce:
                                FOutput[i].DetectMultiViewReinforce(ImgCache[i], w, h);
                                break;
                            }

                            this.FError[i] = "";
                        }
                        catch (Exception ex)
                        {
                            FLogger.Log(ex);
                            this.FError[i] = ex.Message + Environment.NewLine + ex.StackTrace;
                        }
                    }
                    else
                    {
                        this.FError[i] = "!";
                    }
                }
                EndQuery?.Invoke(AssignedContext);

                FOutput.Stream.IsChanged = true;
            }
            else
            {
                this.FError.SliceCount = 0;
            }
        }