Skip to content

Commit

Permalink
Detector
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur198 committed Feb 10, 2019
1 parent 7ed7ae7 commit 2bff2d7
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 38 deletions.
37 changes: 32 additions & 5 deletions MotionDetectionSurvilance/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:telerikChart="using:Telerik.UI.Xaml.Controls.Chart"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*" MinWidth="301"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<Grid Margin="20">
<Grid Margin="20,20,20,20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>

Expand All @@ -32,12 +34,37 @@

<Button Name="BtnCapture" Content="Capture" HorizontalAlignment="Center" Click="BtnCapture_Click" Margin="10" IsEnabled="False"/>

<Slider Name="Threshold" Minimum="0" Maximum="200" Value="{x:Bind threshold,Mode=TwoWay}" Margin="10" Header="De-noise" StepFrequency="5" />

<Slider Header="Multiplier" Minimum="100" Maximum="5000" StepFrequency="100" Value="{x:Bind smooth,Mode=TwoWay}" Margin="10"/>

</StackPanel>

<TextBlock Grid.Row="1" Name="Status" Text="Hello" HorizontalAlignment="Center"/>
<telerikChart:RadCartesianChart Grid.Row="1" Name="ChartDiagnostic">
<telerikChart:RadCartesianChart.HorizontalAxis>
<telerikChart:CategoricalAxis/>
</telerikChart:RadCartesianChart.HorizontalAxis>
<telerikChart:RadCartesianChart.VerticalAxis>
<telerikChart:LinearAxis/>
</telerikChart:RadCartesianChart.VerticalAxis>
<telerikChart:LineSeries ItemsSource="{Binding}"
CombineMode="Stack"/>
<!--<telerikChart:LineSeries ItemsSource="{Binding Data2}"
CombineMode="Stack"/>-->

</telerikChart:RadCartesianChart>

<TextBlock Grid.Row="2" Name="Status" Text="Hello" HorizontalAlignment="Center"/>
</Grid>

<CaptureElement Grid.Column="1" Name="PreviewControl" Stretch="Uniform"/>
<Grid Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CaptureElement Name="PreviewControl" Stretch="Uniform"/>
<Image Grid.Row="1" Name="ImgPreview"/>
</Grid>


</Grid>
Expand Down
82 changes: 75 additions & 7 deletions MotionDetectionSurvilance/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Imaging;
using Windows.Media.Capture;
using Windows.Media.Capture.Frames;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
Expand All @@ -29,16 +34,24 @@ public sealed partial class MainPage : Page
{
private CameraSettings CameraSettings;
private SoftwareBitmap oldImg;

private MotionDataCollection MotionDataCollection;
private int threshold;
private int smooth;

public MainPage()
{
this.InitializeComponent();

threshold = 25;
smooth = 10;

CameraSettings = new CameraSettings(PreviewControl, Status, Dispatcher);
CameraSettings.ShowCameraListAsync();
CamerasList.SelectedIndex = 0;
CameraSettings.cameraPreview.PreviewStatusChanged += CameraPreview_PreviewStatusChanged;

MotionDataCollection = new MotionDataCollection();
ChartDiagnostic.DataContext = MotionDataCollection.MotionValue;
}

private void CameraPreview_PreviewStatusChanged(object sender, bool preview)
Expand All @@ -59,17 +72,72 @@ private void StartPreview_ClickAsync(object sender, RoutedEventArgs e)
private async void BtnCapture_Click(object sender, RoutedEventArgs e)
{
//TODO: make it to click image automatically
var image = await CameraSettings.cameraPreview.CaptureImage();

if (oldImg == null)
isMonitoring = !isMonitoring;
//await CaptureImage();


if (isMonitoring)
{
startCaptureImage();
}
}

private bool isMonitoring = false;

private async void startCaptureImage()
{
try
{
oldImg = image;
while (isMonitoring)
{
await CaptureImage();
//Thread.Sleep(1000);
}
}
else
catch (Exception e)
{
Debug.WriteLine(e.Message);
return;
}
}

private SoftwareBitmap image;

private async Task CaptureImage()
{
image = await CameraSettings.cameraPreview.CaptureImage();

if (image != null)
{
Status.Text = new MotionDetector().ComputeDifference(oldImg, image).ToString();
oldImg = image;
if (oldImg == null)
{
oldImg = image;
}

var result = await Task.Factory.StartNew(() => new MotionDetector().ComputeDifference(oldImg, image, threshold, smooth));
result.Difference = result.Difference / smooth;
Status.Text = result.Difference.ToString();

MotionDataCollection.AddMotion(Math.Abs(result.Difference));
//ChartDiagnostic.DataContext = MotionDataCollection.MotionValue;
//ChartDiagnostic.UpdateLayout();

oldImg = result.Image;

image = SoftwareBitmap.Convert(result.Image, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

var source = new SoftwareBitmapSource();

await source.SetBitmapAsync(image);

ImgPreview.Source = source;
}
}

private async Task runOnUIThread(DispatchedHandler d)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, d);
}
}
}
11 changes: 8 additions & 3 deletions MotionDetectionSurvilance/MotionCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
Expand Down Expand Up @@ -39,9 +40,9 @@ internal async Task<SoftwareBitmap> CaptureImage()
{
if (isPreviewing)
{
lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
CapturedPhoto capturedPhoto = null;

var capturedPhoto = await lowLagCapture.CaptureAsync();
capturedPhoto = await lowLagCapture.CaptureAsync();

var softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;

Expand Down Expand Up @@ -78,6 +79,7 @@ private async Task startPreviewAsync()
displayRequest = new DisplayRequest();
displayRequest.RequestActive();
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
}
catch (UnauthorizedAccessException)
{
Expand Down Expand Up @@ -128,7 +130,10 @@ private async Task CleanupCameraAsync()

await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await lowLagCapture.FinishAsync();
if (lowLagCapture != null)
{
await lowLagCapture.FinishAsync();
}
previewControl.Source = null;
if (displayRequest != null)
{
Expand Down
52 changes: 52 additions & 0 deletions MotionDetectionSurvilance/MotionData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MotionDetectionSurvilance
{
internal class MotionData
{
public int MotionValue { get; set; }

public DateTime DateTime { get; set; }

}

internal class MotionDataCollection
{
private int maxCount = 60;
public List<MotionData> motionDatas = new List<MotionData>();
private ObservableCollection<int> datas = new ObservableCollection<int>();

public void AddMotion(int MotionValue)
{
motionDatas.Add(new MotionData() { DateTime = DateTime.Now, MotionValue = MotionValue });
datas.Add(MotionValue);

motionDatas.RemoveAt(0);
datas.RemoveAt(0);
}

public MotionDataCollection()
{
for (int i = 0; i < maxCount; i++)
{
datas.Add(0);
motionDatas.Add(new MotionData());
}
}


public ObservableCollection<int> MotionValue
{
get
{
var arr = datas;
return arr;
}
}
}
}
4 changes: 4 additions & 0 deletions MotionDetectionSurvilance/MotionDetectionSurvilance.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="MotionCamera.cs" />
<Compile Include="MotionData.cs" />
<Compile Include="MotionDetector.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down Expand Up @@ -158,6 +159,9 @@
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.3</Version>
</PackageReference>
<PackageReference Include="Telerik.UI.for.UniversalWindowsPlatform">
<Version>1.0.1.3</Version>
</PackageReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
Expand Down
Loading

0 comments on commit 2bff2d7

Please sign in to comment.