Ejemplo n.º 1
0
 /// <summary>
 /// Resize the image to the destination width and height
 /// </summary>
 public static WritableLockBitImage Transform(WritableLockBitImage sourceImage, int width, int height)
 {
     using (WritableLockBitImage copyOfSourceImage = new WritableLockBitImage(sourceImage))
     {
         copyOfSourceImage.Lock();
         return(new WritableLockBitImage(Transform(copyOfSourceImage.GetImage(), width, height)));
     }
 }
Ejemplo n.º 2
0
        private void RunQueue()
        {
            int currentFrame = 0;
            int frameSize    = 3 * _width * _height;

            byte[] frameBuffer  = new byte[frameSize];
            int    currentIndex = 0;

            try
            {
                foreach (byte[] rawBytes in _rawByteQueue.GetConsumingEnumerable())
                {
                    // Raw bytes don't overflow frame
                    if (rawBytes.Length < frameSize - currentIndex)
                    {
                        Buffer.BlockCopy(rawBytes, 0, frameBuffer, currentIndex, rawBytes.Length);
                        currentIndex += rawBytes.Length;
                    }
                    // Raw bytes overflow frame
                    else
                    {
                        int numBytesToCopy = frameSize - currentIndex;
                        Buffer.BlockCopy(rawBytes, 0, frameBuffer, currentIndex, numBytesToCopy);

                        // Frame is now full. Create image and ship it off
                        WritableLockBitImage frame = new WritableLockBitImage(_width, _height, frameBuffer);
                        frame.Lock();

                        _videoIndexer.SubmitVideoFrame(frame, currentFrame);
                        currentFrame++;

                        // Write overflow stuff now
                        Buffer.BlockCopy(rawBytes, numBytesToCopy, frameBuffer, 0, rawBytes.Length - numBytesToCopy);
                        currentIndex = rawBytes.Length - numBytesToCopy;
                    }

                    // Reduce the amount of bytes
                    long currentMemoryLevels = Interlocked.Add(ref _currentMemoryLevel, -rawBytes.Length);

                    // Check capacity and set or reset the barrier
                    if (currentMemoryLevels < _maxCapacity)
                    {
                        _capacityBarrier.Set();
                    }
                    else
                    {
                        _capacityBarrier.Reset();
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Shutdown();
            }
        }
Ejemplo n.º 3
0
        private static WritableLockBitImage GetFrameFromVideo(VideoFingerPrintWrapper video, int frameNumber)
        {
            using (MediaInfoProcess mediaInfoProcess = new MediaInfoProcess(video.FilePath))
            {
                MediaInfo mediaInfo = mediaInfoProcess.Execute();
                if (mediaInfo.GetFramerate().Numerator == 0)
                {
                    throw new Exception("Did not get valid frame rate");
                }

                int width  = mediaInfo.GetWidth();
                int height = mediaInfo.GetHeight();
                var ffmpegProcessSettings = new FFMPEGProcessVideoSettings(
                    video.FilePath,
                    mediaInfo.GetFramerate().Numerator,
                    mediaInfo.GetFramerate().Denominator * 4,
                    FFMPEGMode.SeekFrame
                    );

                ffmpegProcessSettings.TargetFrame = frameNumber;
                byte[] frameBytes = new byte[width * height * 3];
                int    offset     = 0;
                using (
                    var ffmpegProcess = new FFMPEGProcess(
                        ffmpegProcessSettings,
                        (stdoutBytes, numBytes) =>
                {
                    Buffer.BlockCopy(stdoutBytes, 0, frameBytes, offset, numBytes);
                    offset += numBytes;
                },
                        false
                        )
                    )
                {
                    ffmpegProcess.Execute();
                    if (offset != 3 * width * height)
                    {
                        throw new Exception("Did not get all bytes to produce valid frame");
                    }

                    WritableLockBitImage videoFrame = new WritableLockBitImage(width, height, frameBytes);
                    videoFrame.Lock();
                    return(videoFrame);
                }
            }
        }
        private void RunConsumer()
        {
            try
            {
                foreach (WorkItem item in _buffer.GetConsumingEnumerable())
                {
                    using (WritableLockBitImage frame = item.Frame)
                    {
                        frame.Lock();
                        Tuple <ulong, byte[]> fingerPrintHashTuple = FrameIndexer.IndexFrame(item.Frame.GetImage());
                        _fingerPrints.Add(new FrameFingerPrintWrapper
                        {
                            PHashCode          = fingerPrintHashTuple.Item1,
                            FrameNumber        = item.FrameNumber,
                            EdgeGrayScaleThumb = fingerPrintHashTuple.Item2,
                        });

                        // Reduce the current memory level
                        long currentMemoryLevels = Interlocked.Add(ref _currentMemoryLevel, -frame.MemorySize);

                        // Check capacity
                        if (currentMemoryLevels < _maxCapacity)
                        {
                            // If we have space, set event
                            _capacityBarrier.Set();
                        }
                        else
                        {
                            // Otherwise, reset the event
                            _capacityBarrier.Reset();
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Shutdown();
            }
        }