Beispiel #1
0
        private void btnStep_Click(object sender, EventArgs e)
        {
            if (m_taskCmd != null)
            {
                m_cmd = COMMAND.STEP;
                m_evtCmdReady.Set();
                m_evtCmdDone.WaitOne();
                return;
            }

            m_webCam.Step(1);
        }
        /// <summary>
        /// Load a batch of data in the background (this is run on an internal thread within the BasePrefetchingDataLayer class).
        /// </summary>
        /// <param name="batch">Specifies the Batch of data to load.</param>
        protected override void load_batch(Batch <T> batch)
        {
            m_log.CHECK(batch.Data.count() > 0, "There is no space allocated for data!");
            int nBatchSize = (int)m_param.data_param.batch_size;

            // Reshape batch according to the batch size.
            m_rgTopShape[0] = nBatchSize;
            batch.Data.Reshape(m_rgTopShape);

            T[] rgTopLabel = null;
            if (m_bOutputLabels)
            {
                rgTopLabel = batch.Label.mutable_cpu_data;
            }

            if (m_param.data_param.display_timing)
            {
                m_swTimerBatch.Restart();
                m_dfReadTime  = 0;
                m_dfTransTime = 0;
            }

            int    nSkipFrames = m_nSkipFrames;
            int    nDim        = 0;
            Bitmap bmp         = null;

            for (int i = 0; i < nBatchSize; i++)
            {
                if (m_param.data_param.display_timing)
                {
                    m_swTimerTransaction.Restart();
                }

                lock (m_objSync)
                {
                    if (m_videoType == VideoDataParameter.VideoType.WEBCAM)
                    {
                        if (m_webcam == null)
                        {
                            return;
                        }

                        m_webcam.GetImage();
                        if (!m_evtSnapshotReady.WaitOne(1000))
                        {
                            m_log.FAIL("Failed to get web-cam snapshot!");
                        }

                        bmp = m_bmpSnapshot;
                    }
                    else if (m_videoType == VideoDataParameter.VideoType.VIDEO)
                    {
                        if (m_webcam == null)
                        {
                            return;
                        }

                        if (nSkipFrames > 0)
                        {
                            nSkipFrames--;
                        }

                        if (nSkipFrames > 0)
                        {
                            m_webcam.Step(nSkipFrames);
                        }

                        m_webcam.GetImage();

                        if (!m_evtSnapshotReady.WaitOne(1000))
                        {
                            m_log.FAIL("Failed to get video file snapshot!");
                        }

                        if (m_webcam.IsAtEnd)
                        {
                            m_webcam.SetPosition(0);
                        }

                        bmp = m_bmpSnapshot;
                    }
                    else
                    {
                        m_log.FAIL("Unknown video type!");
                    }

                    m_log.CHECK(bmp != null, "Could not load image!");

                    // Resize the image if needed.
                    if (bmp.Width != m_nVideoWidth || bmp.Height != m_nVideoHeight)
                    {
                        Bitmap bmpNew = ImageTools.ResizeImage(bmp, m_nVideoWidth, m_nVideoHeight);
                        bmp.Dispose();
                        bmp = bmpNew;
                    }

                    if (m_param.data_param.display_timing)
                    {
                        m_dfReadTime += m_swTimerTransaction.Elapsed.TotalMilliseconds;
                        m_swTimerTransaction.Restart();
                    }

                    SimpleDatum datum = null;

                    if (i == 0)
                    {
                        datum = ImageData.GetImageDataD(bmp, batch.Data.channels, false, 0);

                        // Reshape according to the first datum of each batch
                        // on single input batches allows for inputs of varying dimension.
                        // Use data trabnsformer to infer the expected blob shape for datum.
                        List <int> rgTopShape = m_transformer.InferBlobShape(datum);

                        // Reshape batch according to the batch size.
                        rgTopShape[0] = nBatchSize;
                        batch.Data.Reshape(rgTopShape);

                        nDim = 1;
                        for (int k = 1; k < rgTopShape.Count; k++)
                        {
                            nDim *= rgTopShape[k];
                        }

                        int nTopLen = nDim * nBatchSize;
                        if (m_rgTopData == null || m_rgTopData.Length != nTopLen)
                        {
                            m_rgTopData = new T[nTopLen];
                        }
                    }

                    nSkipFrames = m_nSkipFrames;

                    if (datum == null)
                    {
                        datum = ImageData.GetImageDataD(bmp, batch.Data.channels, false, 0);
                    }

                    // Apply transformations (mirror, crop...) to the image.
                    T[] rgTrans = m_transformer.Transform(datum);
                    Array.Copy(rgTrans, 0, m_rgTopData, nDim * i, nDim);

                    // Copy label.
                    if (m_bOutputLabels)
                    {
                        rgTopLabel[i] = (T)Convert.ChangeType(datum.Label, typeof(T));
                    }

                    if (m_param.data_param.display_timing)
                    {
                        m_dfTransTime += m_swTimerTransaction.Elapsed.TotalMilliseconds;
                    }
                }
            }

            batch.Data.SetCPUData(m_rgTopData);

            if (m_bOutputLabels)
            {
                batch.Label.SetCPUData(rgTopLabel);
            }

            if (m_param.data_param.display_timing)
            {
                m_swTimerBatch.Stop();
                m_swTimerTransaction.Stop();
                m_log.WriteLine("Prefetch batch: " + m_swTimerBatch.ElapsedMilliseconds.ToString() + " ms.", true);
                m_log.WriteLine("     Read time: " + m_dfReadTime.ToString() + " ms.", true);
                m_log.WriteLine("Transform time: " + m_dfTransTime.ToString() + " ms.", true);
            }
        }