question

YRK-33345 avatar image
0 Votes"
YRK-33345 asked Elmar-4711 edited

Where am I wrong?

I'm trying to identify objects in an image and I encounter an error "Object reference not set to an instance of an object" I am using Alturos.Yolo version 2.2.2

And I also use Alturos.YoloV2TinyVocData version 1.0.0

This is the code:


 using Alturos.Yolo;
 using Alturos.Yolo.Model;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Drawing.Imaging;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
    
 namespace OBD
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
    
            
    
         private void btnDetect_Click(object sender, EventArgs e)
         {
             var configurationDetector = new ConfigurationDetector();
             var config = configurationDetector.Detect();
                
             using (YoloWrapper yoloWrapper = new YoloWrapper(config))
             {
                 using(MemoryStream ms = new MemoryStream())
                 {
                     pictureBox1.Image.Save(ms, ImageFormat.Png);
                     var Items = yoloWrapper.Detect(ms.ToArray());
                     yoloItemBindingSource.DataSource = Items;
                     //dataGridView1.DataSource = Items;
    
    
                     //var Itemi = yoloWrapper.Detect(ms.ToArray()).ToList();
                     //Draw(Itemi);
    
                 }
                    
                 //var items = yoloWrapper.Detect(@"image.jpg");
                 //items[0].Type -> "Person, Car, ..."
                 //items[0].Confidence -> 0.0 (low) -> 1.0 (high)
                 //items[0].X -> bounding box
                 //items[0].Y -> bounding box
                 //items[0].Width -> bounding box
                 //items[0].Height -> bounding box
             }
         }
    
         public void Draw(List<YoloItem> sItems)
         {
                
             foreach (var item in sItems)
             {
    
             }
         }
    
            
    
         private void btnOpen_Click(object sender, EventArgs e)
         {
             OpenFileDialog ofd = new OpenFileDialog() { Filter = "PNG|*.Png|JEPG|*.Jepg"};
    
             if(ofd.ShowDialog() == DialogResult.OK)
             {
                 pictureBox1.Image = new Bitmap(ofd.FileName);
             }
         }
     }
 }


dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Elmar-4711 avatar image
0 Votes"
Elmar-4711 answered Elmar-4711 edited

I'm not familiar with Alturos.Yolo. But from what I've seen here, the method

 public IEnumerable<YoloItem> Detect(byte[] imageData)
 {
     if (!this._imageAnalyzer.IsValidImageFormat(imageData))
     {
         throw new Exception("Invalid image data, wrong image format");
     }
    
     var container = new BboxContainer();
     var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
     var pnt = Marshal.AllocHGlobal(size);
    
     var count = 0;
     try
     {
         // Copy the array to unmanaged memory.
         Marshal.Copy(imageData, 0, pnt, imageData.Length);
         switch (this.DetectionSystem)
         {
             case DetectionSystem.CPU:
                 count = DetectImageCpu(pnt, imageData.Length, ref container);
                 break;
             case DetectionSystem.GPU:
                 count = DetectImageGpu(pnt, imageData.Length, ref container);
                 break;
         }
     }
     catch (Exception)
     {
         return null;
     }
     finally
     {
         // Free the unmanaged memory.
         Marshal.FreeHGlobal(pnt);
     }
    
     if (count == -1)
     {
         throw new NotImplementedException("C++ dll compiled incorrectly");
     }
    
     return this.Convert(container);
 }

is somewhat special, because it returns null at line 29, when it internally throws an exception. In that case your variable Items would not get properly initialized and the next line of your code would throw a NullReferenceException("Object reference not set to ...").

You should take a look at this!








5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

When posting about an error please identify the exception type, exception message, call stack and the line the error is occurring on otherwise we have no way of helping. An "object reference" means you are calling a member on a null instance. But your code is littered with these calls so we cannot identify which line is failing and therefore where the problem is.

My gut instinct is that it is this line: yoloItemBindingSource.DataSource = Items;

I suspect that yoloItemBindingSource is null because you never initialized it. But that is a complete guess. I could also see pictureBox1.Image.Save failing if there is no image. The rest of the code seems to be using initialized data. But the exception could be coming from one of the calls you're making (the stack trace would clearly identify this issue) so we have no visibility into that.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.