void mgr_OnLoadProgress(object sender, LoadArgs e)
 {
     if (m_bCancel)
     {
         e.Cancel = true;
         return;
     }
 }
Example #2
0
        public uint ConvertData(string strImageFile, string strLabelFile, string strDBPath, string strDBPathMean, bool bCreateImgMean, bool bGetItemCountOnly = false, int nChannels = 1)
        {
            string             strExt;
            List <SimpleDatum> rgImg = new List <SimpleDatum>();

            strExt = Path.GetExtension(strImageFile).ToLower();
            if (strExt == ".gz")
            {
                m_log.WriteLine("Unpacking '" + strImageFile + "'...");
                strImageFile = expandFile(strImageFile);
            }

            strExt = Path.GetExtension(strLabelFile).ToLower();
            if (strExt == ".gz")
            {
                m_log.WriteLine("Unpacking '" + strLabelFile + "'...");
                strLabelFile = expandFile(strLabelFile);
            }

            BinaryFile image_file = new BinaryFile(strImageFile);
            BinaryFile label_file = new BinaryFile(strLabelFile);

            try
            {
                uint magicImg = image_file.ReadUInt32();
                uint magicLbl = label_file.ReadUInt32();

                if (magicImg != 2051)
                {
                    if (m_log != null)
                    {
                        m_log.FAIL("Incorrect image file magic.");
                    }

                    if (OnLoadError != null)
                    {
                        OnLoadError(this, new LoadErrorArgs("Incorrect image file magic."));
                    }
                }

                if (magicLbl != 2049)
                {
                    if (m_log != null)
                    {
                        m_log.FAIL("Incorrect label file magic.");
                    }

                    if (OnLoadError != null)
                    {
                        OnLoadError(this, new LoadErrorArgs("Incorrect label file magic."));
                    }
                }

                uint num_items  = image_file.ReadUInt32();
                uint num_labels = label_file.ReadUInt32();

                if (num_items != num_labels)
                {
                    if (m_log != null)
                    {
                        m_log.FAIL("The number of items must equal the number of labels.");
                    }

                    throw new Exception("The number of items must equal the number of labels." + Environment.NewLine + "  Label File: '" + strLabelFile + Environment.NewLine + "  Image File: '" + strImageFile + "'.");
                }

                if (bGetItemCountOnly)
                {
                    return(num_items);
                }

                uint rows = image_file.ReadUInt32();
                uint cols = image_file.ReadUInt32();

                int nSrcId = m_factory.AddSource(strDBPath, nChannels, (int)cols, (int)rows, false, 0, true);
                m_factory.Open(nSrcId, 500, Database.FORCE_LOAD.FROM_FILE); // use file based data.
                m_factory.DeleteSourceData();

                // Storing to db
                byte[] rgLabel;
                byte[] rgPixels;

                Datum datum = new Datum(false, nChannels, (int)cols, (int)rows);

                if (m_log != null)
                {
                    m_log.WriteHeader("LOADING " + strDBPath + " items.");
                    m_log.WriteLine("A total of " + num_items.ToString() + " items.");
                    m_log.WriteLine("Rows: " + rows.ToString() + " Cols: " + cols.ToString());
                }

                if (OnLoadStart != null)
                {
                    OnLoadStart(this, new LoadStartArgs((int)num_items));
                }

                for (int item_id = 0; item_id < num_items; item_id++)
                {
                    rgPixels = image_file.ReadBytes((int)(rows * cols));
                    rgLabel  = label_file.ReadBytes(1);

                    List <byte> rgData = new List <byte>(rgPixels);

                    if (nChannels == 3)
                    {
                        rgData.AddRange(new List <byte>(rgPixels));
                        rgData.AddRange(new List <byte>(rgPixels));
                    }

                    datum.SetData(rgData, (int)rgLabel[0]);

                    if (m_bmpTargetOverlay != null)
                    {
                        datum = createTargetOverlay(datum);
                    }

                    m_factory.PutRawImageCache(item_id, datum);

                    if (bCreateImgMean)
                    {
                        rgImg.Add(new SimpleDatum(datum));
                    }

                    if ((item_id % 1000) == 0)
                    {
                        if (m_log != null)
                        {
                            m_log.WriteLine("Loaded " + item_id.ToString("N") + " items...");
                            m_log.Progress = (double)item_id / (double)num_items;
                        }

                        if (OnLoadProgress != null)
                        {
                            LoadArgs args = new LoadArgs(item_id);
                            OnLoadProgress(this, args);

                            if (args.Cancel)
                            {
                                break;
                            }
                        }
                    }
                }

                m_factory.ClearImageCache(true);
                m_factory.UpdateSourceCounts();

                if (bCreateImgMean)
                {
                    if (strDBPath != strDBPathMean)
                    {
                        m_factory.CopyImageMean(strDBPathMean, strDBPath);
                    }
                    else
                    {
                        m_log.WriteLine("Creating image mean...");
                        SimpleDatum dMean = SimpleDatum.CalculateMean(m_log, rgImg.ToArray(), new WaitHandle[] { new ManualResetEvent(false) });
                        m_factory.PutRawImageMean(dMean, true);
                    }
                }

                if (OnLoadProgress != null)
                {
                    LoadArgs args = new LoadArgs((int)num_items);
                    OnLoadProgress(this, args);
                }

                return(num_items);
            }
            finally
            {
                image_file.Dispose();
                label_file.Dispose();
            }
        }