techwingslab / yolov5-net

YOLOv5 object detection with C#, ML.NET, ONNX
MIT License
351 stars 104 forks source link

Refactor to remove System.Drawing #73

Closed Webreaper closed 2 years ago

Webreaper commented 2 years ago

Hi,

I'm trying to refactor this to remove System.Drawing, as it's no longer supported on Linux in .Net 7. But I think the tensor setup isn't right. Can you have a look, and possibly upgrade this example to use ImageSharp? I think the code should look something like this, but it's not quite working..... for a simple image it comes back with 32,000 matches when it should be about 5.

    // Load the image first
    using var image = Image.Load<Rgb48>( fullPath );

   // Call Predict() and Inference() etc
// This is the important bit
private Tensor<float> ExtractPixels( Image<Rgb48> image )
        {
            if( image.Height > _model.Height || image.Width > _model.Width )
            {
                image.Mutate( x =>
                    {
                        x.Resize( new ResizeOptions
                        {
                            Size = new Size( _model.Width, _model.Height ),
                            Mode = ResizeMode.Stretch
                        } );
                    } );
            }
            var tensor = new DenseTensor<float>( new[] { 1, 3, image.Height, image.Width } );

            image.ProcessPixelRows( pixelAccessor =>
            {
                for( var y = 0; y < pixelAccessor.Height; y++ )
                {
                    var row = pixelAccessor.GetRowSpan( y );

                    for( var x = 0; x < row.Length; x++ )
                    {
                        tensor[0, 0, y, x] = row[x].R / 255.0F;
                        tensor[0, 1, y, x] = row[x].G / 255.0F;
                        tensor[0, 2, y, x] = row[x].B / 255.0F;
                    }
                }
            } );

            return tensor;
        }
Webreaper commented 2 years ago

Actually, I think it is working after all!